Skip to main content
Version: 1.0.x

PubSub Class Methods

publish()

  • publish() is used to publish messages on a specified topic in the room.

Parameters

  • topic

    • type: String
    • This is the name of the topic, for which message will be published.
  • message

    • type: String
    • This is the actual message.
  • options

Returns

  • Future<void>

Example

// Publishing message
room.pubSub
.publish(
"CHAT",
"Hii",
const PubSubPublishOptions(persist: true),
);

subscribe()

  • subscribe() is used to subscribe a particular topic to get all the messages of that particular topic in the room.

Parameters

  • topic:

    • type: String
    • Participants can listen to messages on that particular topic.
  • messageHandler:

Returns

Example

// Subscribing 'CHAT' Topic

void handleMessage(PubSubMessage pubsubMessage) {
print(pubsubMessage.message);
}

void subscribePubSubTopic() async {
PubSubMessages pubsubMessages =
room.pubSub.subscribe("CHAT", handleMessage);

// do something
}

subscribePubSubTopic();

unsubscribe()

  • unsubscribe() is used to unsubscribe a particular topic on which you have subscribed priviously.

Parameters

  • topic:

    • type: String
    • This is the name of the topic to be unsubscribed.
  • messageHandler:

Returns

  • Future<void>

Example

// Unsubscribing 'CHAT' topic
room.pubSub.unsubscribe("CHAT", handleMessage);