Javascript API Reference
    Preparing search index...

    Class pubSub

    Index

    Constructors

    Methods

      • 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>

      Throws an error if:

      • message is not a string
      • payload is not an object
      • the publish request fails
      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 callback function is executed whenever a new message is received for the subscribed topic.

      Parameters

      • topic: string

        The topic to subscribe to.

      • callback: (message: message) => void

        A function that handles incoming messages for the subscribed topic.

      Returns Promise<message[]>

      A promise that resolves with previously published messages, if message persistence was enabled for the topic.

      const topic = "CHAT";

      const handleMessage = (message) => {
      console.log(message);
      };

      const subscribe = async () => {
      const previousMessages = await meeting.pubSub.subscribe(topic, handleMessage);
      console.log(previousMessages);
      };
      • 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.

      • callback: (message: message) => void

        The same callback function that was provided when subscribing to the topic.

      Returns Promise<void>

      const handleMessage = (message) => {
      console.log(message);
      };

      await meeting.pubSub.unsubscribe("CHAT", handleMessage);