Javascript API Reference
    Preparing search index...

    Class pubSub

    Index
      • This method can be used to publish a message to a specified topic within the meeting.

      Parameters

      • topic: string

        The topic to which the message will be published.

      • message: string

        The message content to be published. This value must be a string.

      • Optionaloptions: { persist?: boolean; sendOnly?: string[] }
        • Optionalpersist?: boolean

          When set to true, the message is stored for the entire session and is available to newly joined participants.

        • OptionalsendOnly?: string[]

          An array of participantId that should receive the message. If not provided, the message is broadcast to all participants.

      • Optionalpayload: object

        Additional data to be sent along with the message.

      Returns Promise<void>

      const topic = "CHAT";

      const sendMessage = async (message) => {
      try {
      await meeting?.pubSub.publish(topic, message, { persist: true });
      } catch (error) {
      console.error("Error while sending message:", error);
      }
      };

      sendMessage("Hello world!");
      • This method can be used to subscribe to a specific topic and receive messages published under that topic.

      • The provided listeners exposes multiple callbacks so you can handle realtime messages, batched realtime messages, old messages, and message-drop events independently.

      Parameters

      • topic: string

        The topic to subscribe to.

      • listeners: {
            onBatchReceived?: (messages: message[]) => void;
            onMessageDrop?: (info: { droppedCount: number }) => void;
            onMessageReceived?: (message: message) => void;
            onOldMessagesReceived?: (
                messages: message[],
                info: { isLast: boolean },
            ) => void;
        }

        An object of callbacks that handle incoming messages for the subscribed topic.

        • OptionalonBatchReceived?: (messages: message[]) => void

          Fires with an array of realtime messages — the same set surfaced one-by-one through onMessageReceived, grouped for bulk handling.

        • OptionalonMessageDrop?: (info: { droppedCount: number }) => void

          Called when messages are dropped because your internet is slow or your device (CPU) can't keep up with the incoming rate. Receives an info object describing how many messages were dropped.

        • OptionalonMessageReceived?: (message: message) => void

          Fires once per realtime message as it arrives.

        • OptionalonOldMessagesReceived?: (messages: message[], info: { isLast: boolean }) => void

          Called with old messages when message persistence was enabled for the topic. Receives (messages, { isLast })isLast is true on the final batch of old messages.

      • Optionaloptions: {
            maxQueue?: number;
            newMessageLimit?: number;
            oldMessageLimit?: number;
            realtimeOverflow?: "queue" | "drop";
        }
        • OptionalmaxQueue?: number

          Maximum number of message batches to queue when your internet is slow or your device can't keep up with the incoming rate. Increase this value to retain more messages and catch up after recovering. Default: 70. Max: 200.

        • OptionalnewMessageLimit?: number

          Maximum number of realtime messages to receive per 500 ms.

        • OptionaloldMessageLimit?: number

          How many old messages to receive. Pass 0 to receive none. If omitted, all old messages are delivered. Default: all.

        • OptionalrealtimeOverflow?: "queue" | "drop"

          Behavior when your internet is slow or your device can't keep up with the incoming rate. RealtimeOverflow.QUEUE (default) queues the remaining messages so you receive them once you catch up; RealtimeOverflow.DROP drops them instead.

      Returns Promise<void>

      const topic = "CHAT";

      const listeners = {
      onMessageReceived: (message) => {
      console.log("realtime message:", message);
      },
      onBatchReceived: (messages) => {
      console.log("realtime batch:", messages);
      },
      onOldMessagesReceived: (messages, { isLast }) => {
      console.log("old messages:", messages, "isLast:", isLast);
      },
      onMessageDrop: (info) => {
      console.log("dropped messages:", info);
      },
      };

      const options = {
      oldMessageLimit: 50,
      realtimeOverflow: RealtimeOverflow.QUEUE,
      maxQueue: 100,
      newMessageLimit: 20,
      };

      const subscribe = async () => {
      try {
      await meeting.pubSub.subscribe(topic, listeners, options);
      } catch (err) {
      console.log("Error in subscribe", err);
      }
      };
      • This method can be used to unsubscribe from a previously subscribed topic and stop receiving messages for that topic.

      Parameters

      • topic: string

        The topic from which to unsubscribe.

      • listeners: {
            onBatchReceived?: (messages: message[]) => void;
            onMessageDrop?: (info: { droppedCount: number }) => void;
            onMessageReceived?: (message: message) => void;
            onOldMessagesReceived?: (
                messages: message[],
                info: { isLast: boolean },
            ) => void;
        }

        The same listeners object that was provided when subscribing to the topic.

      Returns Promise<void>

      const listeners = {
      onMessageReceived: (message) => {
      console.log(message);
      },
      onBatchReceived: (messages) => {
      console.log(messages);
      },
      onOldMessagesReceived: (messages, { isLast }) => {
      console.log(messages, isLast);
      },
      onMessageDrop: (info) => {
      console.log(info);
      },
      };

      try {
      await meeting.pubSub.unsubscribe("CHAT", listeners);
      } catch (err) {
      console.log("Error in unsubscribe", err);
      }