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.
- type:
-
message
- type:
String
- This is the actual message.
- type:
-
options
- type:
PubSubPublishOptions
- This specifies the options for publish.
- type:
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.
- type:
-
messageHandler:
- type:
Function(PubSubMessage)
- This function will be called, whenever pubsub message received.
- type:
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.
- type:
-
messageHandler:
- type:
Function(PubSubMessage)
- This is the message handler to be unsubscribed.
- type:
Returns​
Future<void>
Example​
// Unsubscribing 'CHAT' topic
room.pubSub.unsubscribe("CHAT", handleMessage);
Got a Question? Ask us on discord