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 OPTIONAL- This specifies the options for publish. If you leave it out, the message is not stored and goes to everyone subscribed to the topic.
- type:
-
payload
- type:
Map<String,dynamic> OPTIONAL- If you need to include additional information along with a message, you can pass here as
Map<String, dynamic>. It comes afteroptions, so passoptionstoo when you send a payload.
- type:
Returns
Future<void>
Example
// Publishing message
try {
await room.pubSub.publish(
"CHAT",
"Hii",
const PubSubPublishOptions(persist: true),
);
// With a payload, which comes after the options
await room.pubSub.publish(
"CHAT",
"Hii",
const PubSubPublishOptions(persist: true),
{"type": "greeting"},
);
} on VideoSDKError catch (e) {
print("publish failed: ${e.code} ${e.name}");
} catch (e) {
// Anything else that went wrong.
print("publish failed: $e");
}
publish() needs a joined room. Called before Events.roomJoined fires, or after Events.roomLeft fires, it throws a VideoSDKError with code 3035.
When the SDK refuses the message, publish() throws a VideoSDKError, for example 3065 (ERROR_ROOM_RECONNECTING) while the room reconnects, or 4087 (PUBSUB_PUBLISH_FAILED) when the message cannot be sent. A 4087 failure also reaches Events.error. A failure that happens after publish() has completed reaches only Events.error, with code 4087.
subscribe()
-
subscribe()is used to subscribe to a topic and receive its messages. -
Each
PubSubMessageListeneris its own subscription, with its own options. The topic's stored messages, published withpersist: true, arrive on itsonOldMessagesReceived. New messages arrive on itsonMessageReceived.
Parameters
-
topic:
- type:
String - Participants can listen to messages on that particular topic.
- type:
-
listener:
- type:
PubSubMessageListener - The callbacks that receive the topic's messages. Every callback is optional.
- type:
-
options:
- type:
PubSubSubscribeOptions OPTIONAL- How many stored messages to deliver, and what to do when live messages arrive faster than the app takes them. A field you leave out takes the server's default.
- type:
Returns
Future<void>
Example
// Subscribing 'CHAT' Topic
final chatListener = PubSubMessageListener(
onMessageReceived: (PubSubMessage message) {
print(message.message);
},
onOldMessagesReceived: (List<PubSubMessage> messages, PubSubHistoryInfo info) {
// Stored messages; info.isLast is true on the last delivery
},
);
Future<void> subscribePubSubTopic() async {
try {
await room.pubSub.subscribe(
"CHAT",
chatListener,
const PubSubSubscribeOptions(oldMessageLimit: 50),
);
} on VideoSDKError catch (e) {
print("subscribe failed: ${e.code} ${e.name}");
} catch (e) {
// Anything else that went wrong.
print("subscribe failed: $e");
}
}
// Subscribe once the room is joined
room.on(Events.roomJoined, () {
subscribePubSubTopic();
});
subscribe() needs a joined room. Called before Events.roomJoined fires, or after Events.roomLeft fires, it throws a VideoSDKError with code 3035 and the listener is not registered. Subscribe from your Events.roomJoined handler.
Subscribing the same listener to the same topic twice throws a VideoSDKError with code 3073 (ERROR_INVALID_PARAMETER).
When the SDK refuses the subscription, subscribe() throws a VideoSDKError, for example 3065 (ERROR_ROOM_RECONNECTING) while the room reconnects, or 4088 (PUBSUB_SUBSCRIBE_FAILED). The listener is then not registered.
unsubscribe()
unsubscribe()is used to stop a listener receiving a topic you subscribed to before.
Parameters
-
topic:
- type:
String - This is the name of the topic to be unsubscribed.
- type:
-
listener:
- type:
PubSubMessageListener - The same listener object that you passed to
subscribe().
- type:
Returns
Future<void>
Example
// Unsubscribing 'CHAT' topic
try {
await room.pubSub.unsubscribe("CHAT", chatListener);
} catch (e) {
// chatListener is removed even when this throws
print("unsubscribe failed: $e");
}
The listener stops receiving messages at once, even when unsubscribe() throws.
Telling the server needs a joined room. Once Events.roomLeft has fired, unsubscribe() throws a VideoSDKError with code 3035. In teardown code such as State.dispose(), where you cannot await, add a .catchError() handler to the call.
While the room reconnects it can throw a VideoSDKError with code 3065 (ERROR_ROOM_RECONNECTING), or 4089 (PUBSUB_UNSUBSCRIBE_FAILED) when the request fails.
Got a Question? Ask us on discord

