Chat using PubSub - React
For communication or any kind of messaging between participants, VideoSDK provides the usePubSub
hook, which utilizes the Publish-Subscribe mechanism. It can be employed to develop a wide variety of functionalities. For example, participants could use it to send chat messages to each other, share files or other media, or even trigger actions like muting or unmuting audio or video.
This guide focuses on using PubSub to implement Chat functionality. If you are not familiar with the PubSub mechanism and usePubSub
hook, you can follow this guide.
Implementing Chat
Group Chat
- The initial step in setting up a group chat involves selecting a topic to which all participants will publish and subscribe, facilitating the exchange of messages. In the following example,
CHAT
is used as the topic. Next obtain thepublish()
method and themessages
array from theusePubSub
hook.
// importing usePubSub hook from react-sdk
import { usePubSub } from "@videosdk.live/react-sdk";
function ChatView() {
// destructure publish method from usePubSub hook
const { publish, messages } = usePubSub("CHAT");
return <>...</>;
}
- Next create a message input and a send button to publish the messages.
function ChatView() {
// destructure publish method from usePubSub hook
const { publish, messages } = usePubSub("CHAT");
// State to store the user typed message
const [message, setMessage] = useState("");
const handleSendMessage = () => {
// Sending the Message using the publish method
publish(message, { persist: true });
// Clearing the message input
setMessage("");
};
return (
<>
<input
value={message}
onChange={(e) => {
setMessage(e.target.value);
}}
/>
<button onClick={handleSendMessage}>Send Message</button>
</>
);
}
- The final step in the group chat is to display the messages sent by others. For this use the
messages
array and display all the messages.
function ChatView() {
// destructure publish method from usePubSub hook
const { publish, messages } = usePubSub("CHAT");
const [message, setMessage] = useState("");
const handleSendMessage = () => {
...
};
return (
<>
<div>
<p>Messages: </p>
{messages.map((message) => {
return (
<p>
{messsage.senderName} says {message.message}
</p>
);
})}
</div>
<input
value={message}
onChange={(e) => {
setMessage(e.target.value);
}}
/>
<button onClick={handleSendMessage}>Send Message</button>
</>
);
}
Private Chat
In the above example, to convert the chat into a private one between two participants, set the sendOnly
property.
function ChatView() {
// destructure publish method from usePubSub hook
const { publish, messages } = usePubSub("CHAT");
// State to store the user typed message
const [message, setMessage] = useState("");
const handleSendMessage = () => {
// Sending the Message using the publish method
// Pass the participantId of the participant to whom you want to send the message.
publish(message, { persist: true , sendOnly: ['XYZ'] });
// Clearing the message input
setMessage("");
};
//...
}
Display Latest Message Notificaiton
To show the notification to the user when a new message arrives, following code snippet has to be followed.
function ChatView() {
// destructure publish method from usePubSub hook
const { publish, messages } = usePubSub("CHAT", {
onMessageReceived: (message)=>{
window.alert(message.senderName + "says" + message.message);
}
});
const [message, setMessage] = useState("");
const handleSendMessage = () => {
...
};
return <>...</>;
}
Downloading Chat Messages
All the messages from PubSub published with persist : true
can be downloaded as an .csv
file. This file will be accessible in the VideoSDK dashboard and through the Sessions API.
API Reference
The API references for all the methods and events utilized in this guide are provided below.
Got a Question? Ask us on discord