Skip to main content
Advanced For developers who want the people on a stream to talk, type or see each other. After this page you can join the project’s chat room from your own page and build your own chat interface on its events. Chat is a separate entry point of the same package: @streampixel/web-sdk/voice. A page that does not import it downloads no chat code. The SDK draws no chat interface: it gives you methods and events, and you draw the people, the messages and the buttons.
npm only. The script-tag build does not include chat.

Before you start

  • Turn chat on for the project in the dashboard: Built-in voice and text chat. While it is off, stream.voiceToken is undefined and VoiceChat.for() rejects.
  • On a password project, stream.voiceToken is also undefined while the stream waits in the state awaitingPassword. The chat token arrives when the password is accepted: create the chat after that.
  • You need a running stream: chat is created from a stream object. See Quick start.
  • If your site sends a Content-Security-Policy, allow the chat server. See Content-Security-Policy.

Add chat to your page

1

Install the package

Chat is in the package you already have. There is nothing extra to install.
2

Create the chat for a stream

VoiceChat.for() loads the chat code and prepares the chat. It does not connect yet.
On a public project the wait is skipped: the token is there as soon as create() resolves.userName is the name the others see and also this viewer’s ID in the room. Give every viewer a different one: when a viewer joins with a name that is already in the room, the viewer who had it is removed from the room.
3

Listen, then join

Attach your listeners first, so you see the first list of people. Then call join().
roster fires with yourself as localParticipant. Open the page in a second browser: each page sees the other in remoteParticipants. The two pages must have different userName values, as in the sample above.
4

Turn the microphone on from a button

The microphone starts off. Turn it on when the viewer asks for it. The browser shows its permission prompt the first time.
Everyone on the same project who joins is in the same room. Pass roomId for a separate room inside the project’s room. When the stream ends, the chat leaves the room by itself.
A page with a list of people, a message list, a text field and a microphone button. It leaves out styling and the error handling around Streampixel.create().
chat.ts

VoiceChat.for(stream, options)

Returns a Promise of the chat object. It rejects when the stream has no chat token (the project has chat off, or a password project is still waiting for its password), with a TypeError when userName is missing or empty, and with an EngineLoadError when the chat code cannot be loaded.
Streampixel
required
The stream this chat belongs to. The chat uses the stream’s project and its chat token.
object
required

Methods

Events

{ from, text, avatar? }
A chat message arrived, or you sent one. from is the sender’s id, or 'You' for your own message.
{ localParticipant, remoteParticipants }
Someone joined or left, started or stopped speaking, or changed microphone or camera. Redraw your list of people from it.
{ from, avatar? }
Someone is typing.
{ name, kind?, message }
The microphone or camera could not be opened. message is a short sentence: permission denied, no device found, the device is in use by another app, or, for any other failure, the device is unavailable. name is the browser’s error name. kind, when present, is 'audioinput' for the microphone and 'videoinput' for the camera.
localParticipant is null when you are not in the room: before join(), after leave(), and when the chat ended without leave(). The chat can end without leave(): the chat connection could not be restored, or the chat server removed this viewer, for example because someone joined with the same userName. The chat connection first retries by itself, which can take up to about a minute, and during that time no event fires. Then roster fires once with localParticipant: null and no remote participants. The SDK does not join again by itself: call join() on the same chat object to come back.
videoTrack is the track object of the chat library (LiveKit). The SDK types it as unknown, so cast it. Keep one <video> element per participant and attach the track to it only when the track changes: roster also fires each time someone starts or stops speaking, and attaching again on every event would create a new video element each time. This is a pattern: test it in your page.
When a participant is no longer in roster, remove their <video> from the page.
Remote audio plays by itself: you do not attach audio elements. Browsers block sound until the viewer interacts with the page, so the SDK starts the audio on the viewer’s first click, tap or key press.
The capture sizes are small on purpose, so that sending a camera does not take bandwidth from the stream.

Chat the app asks for

Some apps put the viewer into chat themselves, by sending a comms message. The SDK does not join by itself: listen for the message and join.
Coming from Epic’s Pixel Streaming library? Its method names work on the stream object too: emitUIInteraction(), emitConsoleCommand() and addResponseEventListener(). They are the same calls as send(), consoleCommand() and on('ueMessage'). Use whichever you know. See Talk to your Unreal app.
The app sends { "message": { "type": "comms", "value": { "name": "...", "roomId": "...", "pfpUrl": "..." } } } to join a room, and "value": "disconnect" to leave. name is the name shown to the others. roomId and pfpUrl (an avatar URL) are optional. The name becomes the viewer’s ID in the room, so the app must send a different name to each viewer. Without a name, VoiceChat.for() rejects. The sample handles one request at a time, in the order the app sent them, and keeps a chat only after it joined, so the app can ask again after a failure.

Chat in a shared session

Chat works the same for the host and the viewers of a shared session. Each page creates its own chat from its own stream.

Content-Security-Policy

Chat connects to the chat server on LiveKit Cloud. If your site sends a Content-Security-Policy, add these two sources to the connect-src directive you already have:
The chat code itself is downloaded from the same two hosts as the stream engine, with the same checks, so script-src needs nothing extra. The full list is in Installation.

Troubleshooting

The stream has no chat token. Either the project’s chat is off: turn it on in the dashboard, then start a new stream. Or the project has a password and the stream is still in the state awaitingPassword: the token arrives when the password is accepted, so call VoiceChat.for() after that. Check stream.voiceToken before you call VoiceChat.for().
The chat code could not be downloaded or verified. It is loaded like the stream engine, in the same version as the stream’s engine. See the reasons in Errors and disconnects.
The platform refused the chat token, or the chat server could not be reached. Check your Content-Security-Policy first. On a dedicated or on-premises deployment, pass platformHost to VoiceChat.for(): it is not taken from the stream.An AbortError is not a failure: leave() was called, or the stream ended, before join() finished.
Both pages joined with the same userName. It is the viewer’s ID in the room, and a room holds each ID once: the later one replaces the earlier one. Pass a different userName for every viewer.
The browser did not give the microphone: the viewer refused the prompt, there is no device, or another app is using it. The deviceError event says which. toggleMic() also resolves to false while you are not in the room, for example before join().
Browsers block sound until the viewer interacts with the page. The SDK starts the audio on the first click, tap or key press.
Chat is not in the script-tag build. Install the package with npm and use a bundler.

Next steps

Shared sessions

One app for many people: the natural place for chat.

Talk to your Unreal app

Messages between your page and the app.

The engine and updates

One level up: how the stream engine and the chat code are loaded and verified.

Stream API

Reference: voiceToken and everything else on the stream object.