Skip to main content
Version: 0.1.x

PubSub - React

PubSub feature allows the participant to send and receive messages of the topics which he has subscribed.

Methods​

publish()​

This method is use for publishing message of specific topic.

Syntax​

function publish(message: String, { persist : Boolean });
Parameter NameTypeDescription
messageStringThis is the actual message, which will be published to participants, who had subscribed to a particular topic.
optionsObjectThis is an object, which provides an option, such as persist, which persists message history for upcoming participants.

Example​

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

// destructure publish method from usePubSub hook
const { publish } = usePubSub("CHAT");

// publish message
const message = "Hello Everyone!";
publish(message, { persist: true });

Sample Code​

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

const MyComponent = () => {
// CHAT Topic
const { publish, messages } = usePubSub("CHAT");

// publish message
const sendMessage = () => {
const message = "Hello People!";
publish(message, { persist: true });
};

// get latest messages
console.log("Messages : ", messages);
};
export default MyComponent;

Got a Question? Ask us on discord