React Api Reference
    Preparing search index...

    Class usePubSub

    usePubSub provides reactive APIs to publish, subscribe, and access persisted messages for a specific topic within a meeting.

    Index

    Constructors

    Properties

    messages: {
        id: string;
        message: string;
        payload: object;
        senderId: string;
        senderName: string;
        timestamp: string;
        topic: string;
    }[]

    This represents all persisted messages for the topic.

    Type Declaration

    • id: string

      Unique identifier for the message.

    • message: string

      The message content.

    • payload: object

      Optional additional data sent along with the message.

    • senderId: string

      The ID of the participant who sent the message.

    • senderName: string

      The display name of the participant who sent the message.

    • timestamp: string

      Timestamp indicating when the message was published.

    • topic: string

      The topic under which the message was published.

    topic: string

    This represents the topic associated with the PubSub instance.

    Methods

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

      Parameters

      • 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 { publish } = usePubSub("CHAT");

      const sendMessage = async (message) => {
      try {
      await publish(message, { persist: true }, null, null);
      } catch (e) {
      console.log("Error while sending message through pubsub", e);
      }
      };

      sendMessage("Hello");

    Events

      • Triggered when a new message is published to the subscribed topic.

      Parameters

      • message: {
            id: string;
            message: string;
            payload: object;
            senderId: string;
            senderName: string;
            timestamp: string;
            topic: string;
        }

      Returns void

      import { usePubSub } from "@videosdk.live/react-sdk";

      var topic = "CHAT";

      function onMessageReceived(message) {
      console.log("New Message:", message);
      }

      const {} = usePubSub(topic, {
      onMessageReceived
      });
      • Triggered once when subscribing to a topic to provide all previously persisted messages for that topic.
      • Only messages published with persist: true are returned.

      Parameters

      • messages: {
            id: string;
            message: string;
            payload: object;
            senderId: string;
            senderName: string;
            timestamp: string;
            topic: string;
        }[]

      Returns void

      import { usePubSub } from "@videosdk.live/react-sdk";

      var topic = "CHAT";

      function onOldMessagesReceived(messages) {
      console.log("Old Messages:", messages);
      }

      const {} = usePubSub(topic, {
      onOldMessagesReceived,
      });