Search.../

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 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.

Syntax

function unsubscribe(options: UnsubscribeOptions): Promise<void>

unsubscribe Parameters

NAME
TYPE
DESCRIPTION
options

UnsubscribeOptions

Options to use when unsubscribing.

Returns

Fulfilled - When unsubscribed from the channel or channel resource. Rejected - Error that caused the rejection.

Return Type:

Promise<void>

Was this helpful?

Unsubscribe from all subscriptions to a channel

Code Example

Copy Code
1import wixRealtime from 'wix-realtime';
2
3// ...
4
5const someChannel = {"name": "someChannel"};
6let subscriptionId1;
7let subscriptionId2;
8
9// first subscription to a channel
10wixRealtime.subscribe(someChannel, messageHandler1)
11 .then( (id) => {
12 subscriptionId1 = id;
13 }) ;
14
15// second subscription to the same channel
16wixRealtime.subscribe(someChannel, messageHandler2)
17 .then( (id) => {
18 subscriptionId2 = id;
19 }) ;
20
21// ...
22
23wixRealtime.unsubscribe({"channel": someChannel})
24 .then( () => {
25 // unsubscribed from all subscriptions to channel
26 } );
27
28// ...
29
30function messageHandler1(message, channel){
31 // handle channel messages
32}
33
34function messageHandler2(message, channel){
35 // handle channel messages
36}
Unsubscribe from a specific subscription to a channel

Code Example

Copy Code
1import wixRealtime from 'wix-realtime';
2
3// ...
4
5const someChannel = {"name": "someChannel"};
6let subscriptionId1;
7let subscriptionId2;
8
9// first subscription to a channel
10wixRealtime.subscribe(someChannel, messageHandler1)
11 .then( (id) => {
12 subscriptionId1 = id;
13 }) ;
14
15// second subscription to the same channel
16wixRealtime.subscribe(someChannel, messageHandler2)
17 .then( (id) => {
18 subscriptionId2 = id;
19 }) ;
20
21// ...
22
23wixRealtime.unsubscribe({"subscriptionId": subscriptionId1})
24 .then( () => {
25 // unsubscribed from first subscription to channel
26 } );
27
28// ...
29
30function messageHandler1(message, channel){
31 // handle channel messages
32}
33
34function messageHandler2(message, channel){
35 // handle channel messages
36}