This example shows how to publish with WHIPClient and use an RTCDataChannel for three outbound payload styles:
- RPC-style message fan-out via
publisher.send(...) - direct JSON messages over the DataChannel
- binary audio (
ArrayBuffer) over the DataChannel
It also includes UI controls for dataChannelConfiguration so you can test ordered/reliability behavior.
- enabling DataChannel support during
WHIPClient.init(...) - configuring channel options (
name,ordered,maxRetransmits,maxPacketLifeTime) - detecting readiness with
WebRTC.DataChannel.Available - sending text/JSON payloads
- sending binary payloads from a recorded microphone snippet
DataChannel is enabled by adding two fields to publisher init:
await publisher.init({
endpoint,
streamName,
mediaElementId: 'publisher-video',
connectionParams,
includeDataChannel: true,
dataChannelConfiguration, // name + reliability/ordering options
})The UI section DataChannel Settings maps directly to dataChannelConfiguration.
The example sends RPC with:
await publisher.send('incomingNotification', {
message: trimmedText,
timestamp: Date.now(),
})In this repo, this is wrapped by sendRpcToSubscribers(...).
The example gets the channel and uses dataChannel.send(...):
const dc = publisher.getDataChannel()
dc.send(JSON.stringify({ message, sender_id, timestamp: Date.now() }))In this repo, this is wrapped by sendJsonDataChannelMessage(...).
The example records ~5 seconds from the active microphone, creates an ArrayBuffer, then sends with:
await publisher.sendData?.(arrayBuffer)In this repo, recording/sending is handled by startBinaryRecording(...).
- send UI is enabled only after publish starts and DataChannel is open
WebRTC.DataChannel.Availablemarks channel-ready state- sends are rejected when the channel is missing or not
open - channel lifecycle events are logged:
WebRTC.DataChannel.AvailableWebRTC.DataChannel.CloseWebRTC.DataChannel.ErrorWebRTC.DataChannel.Message
const publisher = new red5prosdk.WHIPClient()
await publisher.init({
endpoint,
streamName,
mediaElementId: 'publisher-video',
includeDataChannel: true,
dataChannelConfiguration: {
name: 'red5pro',
ordered: true,
},
})
await publisher.publish()
// RPC-style message
await publisher.send('incomingNotification', { message: 'hello', timestamp: Date.now() })
// JSON DataChannel message
const dc = publisher.getDataChannel()
if (dc && dc.readyState === 'open') {
dc.send(JSON.stringify({ kind: 'chat', message: 'hello json' }))
}
// Binary DataChannel message
const bytes = new Uint8Array([1, 2, 3, 4]).buffer
await publisher.sendData?.(bytes)- init + DataChannel config wiring:
startPublish() - RPC send path:
sendRpcMessage()andsendRpcToSubscribers() - JSON send path:
sendJsonMessage()andsendJsonDataChannelMessage() - binary recording/send path:
startBinaryMessageRecording()andstartBinaryRecording() - DataChannel readiness/events:
onPublisherEvent()
Pair this with whep-data-channel to see how RPC, JSON, and binary messages are received and rendered on subscribe.