Unsubscribes from a channel or channel resource.
The unsubscribe()
function unsubscribes from receiving messages published
on the specified channel or channel resource.
Use the channel
property of the UnsubscribeOptions
object
passed to the options
parameter to unsubscribe from all subscriptions to a
specific channel or channel resource.
When you unsubscribe from a channel you do not unsubscribe from the resources on that same channel. To no longer receive messages on a channel resource, you must explicitly unsubscribe from the specific resource.
Use the subscriptionId
property of the UnsubscribeOptions
object
passed to the options
parameter to unsubscribe from a specific subscription.
This option is typically used when you have more than one subscription to the same channel or channel resource and you only want to unsubscribe from one of those subscriptions.
Get a subscription's subscriptionId
from the resolution of the Promise returned
when creating the subscription with the subscribe()
function.
function unsubscribe(options: UnsubscribeOptions): Promise<void>;
Options to use when unsubscribing.
import wixRealtimeFrontend from "wix-realtime-frontend";
// ...
const someChannel = { name: "someChannel" };
let subscriptionId1;
let subscriptionId2;
// first subscription to a channel
wixRealtimeFrontend.subscribe(someChannel, messageHandler1).then((id) => {
subscriptionId1 = id;
});
// second subscription to the same channel
wixRealtimeFrontend.subscribe(someChannel, messageHandler2).then((id) => {
subscriptionId2 = id;
});
// ...
wixRealtimeFrontend.unsubscribe({ channel: someChannel }).then(() => {
// unsubscribed from all subscriptions to channel
});
// ...
function messageHandler1(message, channel) {
// handle channel messages
}
function messageHandler2(message, channel) {
// handle channel messages
}
This method doesn’t return any custom errors, but may return standard errors. Learn more about standard Wix errors.