Skip to main content
Version: 0.0.x

Chat using PubSub - Python

For communication or any kind of messaging between participants, VideoSDK provides the pubsub in meeting, 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 pubsub in meeting, you can follow this guide.

Implementing Chat

In this example, we are going to use pubsub.publish(), pubsub.subscribe() and custom topic to implement group chat feature. let's join this participant from server side.

Group Chat Example

import asyncio
from videosdk import (
MeetingConfig,
VideoSDK,
Meeting,
MeetingEventHandler,
PubSubSubscribeConfig,
PubSubPublishConfig
)
VIDEOSDK_TOKEN = "<VIDEOSDK_TOKEN>"
MEETING_ID = "<MEETING_ID>"
NAME = "<NAME>"
loop = asyncio.get_event_loop()

class MyMeetingEventHandler(MeetingEventHandler):
def __init__(self, meeting: Meeting):
super().__init__()
self.meeting = meeting

def on_meeting_joined(self, data):
pubsub_config = PubSubSubscribeConfig(
topic="CHAT",
cb=self.on_pubsub_message
)
# Schedule the coroutine to run in the background
asyncio.create_task(self.subscribe_to_pubsub(pubsub_config))

async def subscribe_to_pubsub(self, pubsub_config):
old_messages = await self.meeting.pubsub.subscribe(pubsub_config)
print("Old messages: ", old_messages)

def on_pubsub_message(self, data):
pubsub_config = PubSubPublishConfig(
topic="CHAT",
message="we are using videosdk pubsub"
)
if data["senderId"]!=self.meeting.local_participant.id:
print(
f"Received message from {data['senderId']}: {data['message']}")
# Schedule the coroutine to run in the background
asyncio.create_task(self.publish_to_pubsub(pubsub_config))

async def publish_to_pubsub(self, pubsub_config):
print("Publishing to the pubsub...")
await self.meeting.pubsub.publish(pubsub_config)

def main():
meeting_config = MeetingConfig(
meeting_id=MEETING_ID,
name=NAME,
mic_enabled=True,
webcam_enabled=True,
token=VIDEOSDK_TOKEN,
)
meeting = VideoSDK.init_meeting(**meeting_config)
meeting.add_event_listener(MyMeetingEventHandler(meeting=meeting))
meeting.join()
print("Joined into the meeting..")

if __name__ == "__main__":
main()
loop.run_forever()

Explanation:

Environment Setup: Load necessary environment variables like VIDEOSDK_TOKEN, MEETING_ID, and NAME using dotenv.

  1. Environment Setup: Load necessary environment variables like VIDEOSDK_TOKEN, MEETING_ID, and NAME.

  2. Meeting Event Handler: Define a custom MeetingEventHandler to handle meeting events, particularly subscribing to and receiving messages from PubSub.

  3. Main Functionality:

    • Meeting Initialization: Initialize the meeting and join it asynchronously using VideoSDK.init_meeting() and meeting.join().
    • Message Publishing: Define an asynchronous function publish_message() to publish messages to the "CHAT" topic using meeting.pubsub.publish().
  4. Asyncio Execution: Use asyncio.get_event_loop() to run the code coroutine, which handles all interactions and event processing asynchronously.

Private Chat Example

In the above example, to convert the chat into a private one between two participants, set the sendOnly property.


pubsub_config = PubSubPublishConfig(
topic="CHAT",
message="we are using videosdk pubsub",
options={
"persist": True,
"sendOnly": ["xyz"] # participantId
}
)
await meeting.pubsub.publish(pubsub_config)

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