The topic to which the message will be published.
The message content to be published. This value must be a string.
Optionaloptions: { persist?: boolean; sendOnly?: string[] }
Optionalpersist?: booleanWhen set to true, the message is stored for the entire session and is available to newly joined participants.
OptionalsendOnly?: string[]An array of participantId that should receive the message. If not provided, the message is broadcast to all participants.
Optionalpayload: object
Additional data to be sent along with the message.
message is not a string or payload is not an objectThis method can be used to subscribe to a specific topic and receive messages published under that topic.
The provided listeners exposes multiple callbacks so you can handle realtime messages, batched realtime messages, old messages, and message-drop events independently.
The topic to subscribe to.
An object of callbacks that handle incoming messages for the subscribed topic.
OptionalonBatchReceived?: (messages: message[]) => voidFires with an array of realtime messages — the same set surfaced one-by-one through onMessageReceived, grouped for bulk handling.
OptionalonMessageDrop?: (info: { droppedCount: number }) => voidCalled when messages are dropped because your internet is slow or your device (CPU) can't keep up with the incoming rate. Receives an info object describing how many messages were dropped.
OptionalonMessageReceived?: (message: message) => voidFires once per realtime message as it arrives.
OptionalonOldMessagesReceived?: (messages: message[], info: { isLast: boolean }) => voidCalled with old messages when message persistence was enabled for the topic. Receives (messages, { isLast }) — isLast is true on the final batch of old messages.
Optionaloptions: {OptionalmaxQueue?: numberMaximum number of message batches to queue when your internet is slow or your device can't keep up with the incoming rate. Increase this value to retain more messages and catch up after recovering. Default: 70. Max: 200.
OptionalnewMessageLimit?: numberMaximum number of realtime messages to receive per 500 ms.
OptionaloldMessageLimit?: numberHow many old messages to receive. Pass 0 to receive none. If omitted, all old messages are delivered. Default: all.
OptionalrealtimeOverflow?: "queue" | "drop"Behavior when your internet is slow or your device can't keep up with the incoming rate. RealtimeOverflow.QUEUE (default) queues the remaining messages so you receive them once you catch up; RealtimeOverflow.DROP drops them instead.
listeners callback (onMessageReceived, onBatchReceived, onOldMessagesReceived, onMessageDrop) is not a function, maxQueue was provided while realtimeOverflow is RealtimeOverflow.DROP, or newMessageLimit is not greater than 0 / oldMessageLimit is less than 0const topic = "CHAT";
const listeners = {
onMessageReceived: (message) => {
console.log("realtime message:", message);
},
onBatchReceived: (messages) => {
console.log("realtime batch:", messages);
},
onOldMessagesReceived: (messages, { isLast }) => {
console.log("old messages:", messages, "isLast:", isLast);
},
onMessageDrop: (info) => {
console.log("dropped messages:", info);
},
};
const options = {
oldMessageLimit: 50,
realtimeOverflow: RealtimeOverflow.QUEUE,
maxQueue: 100,
newMessageLimit: 20,
};
const subscribe = async () => {
try {
await meeting.pubSub.subscribe(topic, listeners, options);
} catch (err) {
console.log("Error in subscribe", err);
}
};
The topic from which to unsubscribe.
The same listeners object that was provided when subscribing to the topic.
listeners callback (onMessageReceived, onBatchReceived, onOldMessagesReceived, onMessageDrop) is not a functionconst listeners = {
onMessageReceived: (message) => {
console.log(message);
},
onBatchReceived: (messages) => {
console.log(messages);
},
onOldMessagesReceived: (messages, { isLast }) => {
console.log(messages, isLast);
},
onMessageDrop: (info) => {
console.log(info);
},
};
try {
await meeting.pubSub.unsubscribe("CHAT", listeners);
} catch (err) {
console.log("Error in unsubscribe", err);
}