unsubscribe( )
Unsubscribes from a channel or channel resource.
Description
The unsubscribe()
function unsubscribes from receiving messages published
on the specified channel or channel resource.
Use the
channel
property of theUnsubscribeOptions
object passed to theoptions
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 theUnsubscribeOptions
object passed to theoptions
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 thesubscribe()
function.
Syntax
function unsubscribe(options: UnsubscribeOptions): Promise<void>
unsubscribe Parameters
NAME
TYPE
DESCRIPTION
Options to use when unsubscribing.
Returns
Fulfilled - When unsubscribed from the channel or channel resource. Rejected - Error that caused the rejection.
Return Type:
Was this helpful?
1import wixRealtime from 'wix-realtime';23// ...45const someChannel = {"name": "someChannel"};6let subscriptionId1;7let subscriptionId2;89// first subscription to a channel10wixRealtime.subscribe(someChannel, messageHandler1)11 .then( (id) => {12 subscriptionId1 = id;13 }) ;1415// second subscription to the same channel16wixRealtime.subscribe(someChannel, messageHandler2)17 .then( (id) => {18 subscriptionId2 = id;19 }) ;2021// ...2223wixRealtime.unsubscribe({"channel": someChannel})24 .then( () => {25 // unsubscribed from all subscriptions to channel26 } );2728// ...2930function messageHandler1(message, channel){31 // handle channel messages32}3334function messageHandler2(message, channel){35 // handle channel messages36}
1import wixRealtime from 'wix-realtime';23// ...45const someChannel = {"name": "someChannel"};6let subscriptionId1;7let subscriptionId2;89// first subscription to a channel10wixRealtime.subscribe(someChannel, messageHandler1)11 .then( (id) => {12 subscriptionId1 = id;13 }) ;1415// second subscription to the same channel16wixRealtime.subscribe(someChannel, messageHandler2)17 .then( (id) => {18 subscriptionId2 = id;19 }) ;2021// ...2223wixRealtime.unsubscribe({"subscriptionId": subscriptionId1})24 .then( () => {25 // unsubscribed from first subscription to channel26 } );2728// ...2930function messageHandler1(message, channel){31 // handle channel messages32}3334function messageHandler2(message, channel){35 // handle channel messages36}