Skip to main content
Version: 0.0.x

PubSub - Python

PubSub is a concise acronym for the Publish-Subscribe mechanism. This mechanism is employed to send and receive messages within a specified topic. As the name implies, to send a message, one must specify the topic and the message to be published. Similarly, to receive a message, a subscriber must be connected to that particular topic.

Here is a visual to better understand the publish-subscribe mechanism.

pubsub

Using PubSub with VideoSDK To utilize PubSub in a meeting with VideoSDK, you need to set up the meeting configuration, join the meeting, and handle the PubSub events. Below is a guide on how to achieve this in Python.

Publishing the messages

  1. Import Necessary Modules: Ensure you import the necessary modules and classes from the videosdk package.

  2. Publish Configuration: Define the configuration for publishing a message using PubSubPublishConfig.It will accept following parameters as input.

  • topic: This parameter represents the actual topic to where published and should be in str format.
  • message: This parameter represents the actual message to be published and should be in str format.
  • options: This object specifies the options for publishing. You can set following properties :
    • persist : When set to true, this option retains the message for the duration of the session. If persist is true, the message will be available for upcoming participants and can be accessed in the VideoSDK Session Dashboard in CSV format after the session is completed.
    • sendOnly: If you want to send a message to specific participants, you can pass their respective participantId here. If you don't provide any IDs, the message will be sent to all participants by default.
  • payload: If you need to include additional information along with a message, you can pass it here as an
  1. Publish Method: Use the publish method to send the message to the specified topic.
import asyncio
from videosdk import (
MeetingConfig,
VideoSDK,
Meeting,
MeetingEventHandler,
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 = PubSubPublishConfig(
topic="CHAT",
message="hello there"
)
# 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):
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()

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

Receiving the messages

  • For Receiving the messages, This method is used to subscribe for particular topic. This method returns a list of messages which were sent earlier.
  1. Import Necessary Modules: Ensure you import the necessary modules and classes from the videosdk package.

  2. Subscribe Configuration: Define the configuration for subscribing a topic using PubSubSubscribeConfig.It will accept following parameters as input.

    • topic: This parameter represents the actual topic to be subscribed and should be in str format.
    • cb: This is callback when new message will received on the same topic.
  3. Subscribe Method: Use the subscribe method to listen the new/old message to the specified topic.

import asyncio
from videosdk import (
MeetingConfig,
VideoSDK,
Meeting,
MeetingEventHandler,
PubSubSubscribeConfig,
)
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, message):
print("on_pubsub_message ::", message)

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()

here on_pubsub_message called when new message will be published and message object contains the following properties:

  • senderId: Represents the participantId of the participant who sent the message.
  • senderName: Represents the displayName of the participant who sent the message.
  • message: The actual message that was sent.
  • timestamp: The timestamp for when the message was published.
  • topic: The name of the topic the message was published to.
  • payload: The data that was sent along with message.

Applications of PubSub

PubSub is a powerful mechanism that can be employed to enhance the interactive aspects of your meeting experience. Some common use cases for PubSub during a meeting include:

  1. Chat: Implement features like Private Chat or Group Chat.
  2. Raise Hand: Allow attendees to raise their hands at any point during the meeting.
  3. Layout Switching: Change the meeting's layout for every participant at once.
  4. Whiteboard: Develop an interactive whiteboard functionality.
  5. Poll: Create polls, let users respond to them, and display the results.
  6. Question Answer Session: Design interactive features based on a question-and-answer format.
  7. AI chat agents Session: Design interactive Chat features using implementation of AI.

Downloading PubSub 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