← All demos

Pub/Sub channels

A data-only publisher sends messages tagged with a channel name. Each viewer subscribes to the channels it wants and only sees those, using the SDK's subscribe(), publishToChannel() and channelMessage event.

  1. Start publishing in the Publisher panel.
  2. Start subscribing in the Subscriber panel, or open the share link in another tab or device and start it there.
  3. Pick channels and send messages. A subscriber only shows channels it subscribed to.

Channels are labels, not access control. publishToChannel() sends every message to every connected viewer. Each viewer's SDK emits channelMessage only for channels it subscribed to and quietly drops the rest. Don't send anything on a channel that other viewers must not receive.

Publisher

Not publishing
    Messages sent0
    Viewers0

    Subscriber

    Not connected

    You can change subscriptions before or after starting.

    Messages received0
    Channels0
    Key SDK calls in this demo
    // Publisher: data-only, no camera or mic
    const pub = new VDONinjaSDK();
    await pub.connect();
    await pub.joinRoom({ room });
    await pub.announce({ streamID });
    pub.publishToChannel('alerts', { message: 'Hi' }); // goes to every viewer
    pub.getPeerSubscriptions(viewerUUID);              // what a viewer asked for
    
    // Subscriber
    const sub = new VDONinjaSDK();
    sub.addEventListener('dataChannelOpen', () => sub.subscribe(['general']));
    sub.addEventListener('channelMessage', (e) => show(e.detail.channel, e.detail.data));
    await sub.connect();
    await sub.joinRoom({ room });
    await sub.view(streamID, { audio: false, video: false });