This example demonstrates a standalone messaging client using PubNubClient from the Red5 Pro WebRTC SDK.
It is a chat-style workflow without media streaming: the client connects to PubNub, subscribes to a channel, and sends/receives messages.
- using
PubNubClientindependently ofWHIPClient/WHEPClient - PubNub auth setup options:
- cloud endpoint token generation
- backend service token generation
- direct auth token input
- channel subscribe + message publish flow
- basic chat UI with sent/received message rendering
- Configure PubNub keys, user, channel, and auth mode.
- Initialize client with
pubnubClient.init(config). - Subscribe with
pubnubClient.subscribe(channelId). - Publish messages with
pubnubClient.publishMessage(channelId, message). - Receive messages through
PubNubEventTypes.MESSAGE_RECEIVED. - Destroy client with
pubnubClient.destroy().
const { PubNubClient } = window.red5prosdk
const pubnubClient = new PubNubClient()
pubnubClient.on('*', (event) => {
console.log(`[PubNub] ${event.type}`, event.data)
})
await pubnubClient.init({
publishKey: 'pub-c-xxxx',
subscribeKey: 'sub-c-xxxx',
userId: 'user-1234',
channelId: 'red5',
// choose exactly one auth mode:
// authToken: 'pre-issued-token'
// OR cloudEndpoint: 'userid-1234-abcd.cloud.red5.net'
// OR backendUrl: 'https://your-backend-service'
})
await pubnubClient.subscribe('red5')
await pubnubClient.publishMessage('red5', 'hello from PubNubClient')The example form maps to the common init fields:
publishKey: PubNub publish keysubscribeKey: PubNub subscribe keyuserId: PubNub user identitychannelId: target PubNub channel- authentication mode (exactly one is required):
authToken: pre-issued tokencloudEndpoint: Red5 Cloud endpoint for token generationbackendUrl: custom backend token service
Auth behavior:
authTokenmode: client uses provided token directly.cloudEndpointmode: SDK requests/generates token from Red5 Cloud endpoint.backendUrlmode: SDK requests token from your custom backend service.- Only one of these modes should be defined for init.
Useful events surfaced in this example include:
AUTH_TOKEN_GENERATEDAUTH_TOKEN_GENERATION_ERRORCONNECTEDDISCONNECTEDSUBSCRIBE_SUCCESSSUBSCRIBE_FAILUREMESSAGE_RECEIVEDERROR
Unlike message-channel, this example uses PubNub transport integration rather than an SDK-managed WebRTC data channel session.
Use pubnub-client when you want PubNub-backed messaging only; use message-channel when you want messaging over negotiated WebRTC DataChannel.
- init + subscribe flow:
startSubscribe() - publish flow:
sendPubNubMessage() - event handling:
onPubNubEvent() - auth/settings form parsing:
src/lib/pubnub-configuration.ts - client teardown:
destroyClient()
Use this example as the baseline for lightweight PubNub chat integration through the Red5 Pro SDK without coupling messaging to media streaming.