Skip to main content
Version: Next

PubSub Class Methods - Flutter

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

  • payload

    • type: Map<String,dynamic>
    • If you need to include additional information along with a message, you can pass here as Map<String, dynamic>. This is optional parameter.

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

Got a Question? Ask us on discord