@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.
Before you start
- Turn chat on for the project in the dashboard: Built-in voice and text chat. While it is off,
stream.voiceTokenisundefinedandVoiceChat.for()rejects. - On a password project,
stream.voiceTokenis alsoundefinedwhile the stream waits in the stateawaitingPassword. The chat token arrives when the password is accepted: create the chat after that. - You need a running stream: chat is created from a
streamobject. See Quick start. - If your site sends a Content-Security-Policy, allow the chat server. See Content-Security-Policy.
Add chat to your page
Install the package
Create the chat for a stream
VoiceChat.for() loads the chat code and prepares the chat. It does not connect yet.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.Listen, then join
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.Turn the microphone on from a button
roomId for a separate room inside the project’s room.
When the stream ends, the chat leaves the room by itself.
A complete example
A complete example
Streampixel.create().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.
Methods
Events
from is the sender’s id, or 'You' for your own message.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.
Showing a participant's camera
Showing a participant's camera
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.roster, remove their <video> from the page.Sound from the other participants
Sound from the other participants
What the SDK sets for you
What the SDK sets for you
Chat the app asks for
Some apps put the viewer into chat themselves, by sending acomms message. The SDK does not join by itself: listen for the message and join.
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 theconnect-src directive you already have:
script-src needs nothing extra. The full list is in Installation.
Troubleshooting
VoiceChat.for() rejects: the project has chat disabled
VoiceChat.for() rejects: the project has chat disabled
awaitingPassword: the token arrives when the password is accepted, so call VoiceChat.for() after that. Check stream.voiceToken before you call VoiceChat.for().VoiceChat.for() rejects with EngineLoadError
VoiceChat.for() rejects with EngineLoadError
join() rejects
join() rejects
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.The second browser does not see the first, or I am dropped when someone joins
The second browser does not see the first, or I am dropped when someone joins
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.toggleMic(true) resolves to false
toggleMic(true) resolves to false
deviceError event says which. toggleMic() also resolves to false while you are not in the room, for example before join().I hear nobody until I click
I hear nobody until I click
I use the script-tag build
I use the script-tag build
Next steps
Shared sessions
Talk to your Unreal app
The engine and updates
Stream API
voiceToken and everything else on the stream object.