diff --git a/README.md b/README.md index 11325ab7..4d653759 100644 --- a/README.md +++ b/README.md @@ -519,15 +519,13 @@ sub.publish({"input": "hello world"}).then(function() { You can call `unsubscribe` method to unsubscribe from a channel: ```javascript -await sub.unsubscribe(); +sub.unsubscribe(); ``` -Note, `sub.unsubscribe()` is asynchronous and may be used without `await`, but in some cases it may be very important to await it before calling subscribe again. For example, this becomes important when using HTTP-based fallback transports and quick unsubscribe/subscribe calls – awaiting unsubscribe call allows to properly synchronize unsubscribe and subscribe requests (to avoid subscribe request be processed before unsubscribe request on the server). To WebSocket and Webtransport this does not apply - since requests go through a direct connection to a server instance that holds connection. - **Important thing to mention** is that unsubscribing from subscription does not remove event handlers you already set to that Subscription object. This allows to simply subscribe to channel again later calling `.subscribe()` method of subscription (see below). But there are cases when your code structured in a way that you need to remove event handlers after unsubscribe **to prevent them be executed twice** in the future. To do this remove event listeners explicitly after calling `unsubscribe()`: ```javascript -await sub.unsubscribe(); +sub.unsubscribe(); sub.removeAllListeners(); ``` diff --git a/src/centrifuge.test.ts b/src/centrifuge.test.ts index 7a5ed9d7..8b81a9c2 100644 --- a/src/centrifuge.test.ts +++ b/src/centrifuge.test.ts @@ -360,13 +360,13 @@ test.each(transportCases)("%s: subscribe and unsubscribe loop", async (transport }) for (let index = 0; index < 10; index++) { - await sub.subscribe(); - await sub.unsubscribe(); + sub.subscribe(); + sub.unsubscribe(); } expect(sub.state).toBe(SubscriptionState.Unsubscribed); await unsubscribedPromise; - await sub.subscribe() + sub.subscribe() const presenceStats = await sub.presenceStats(); expect(presenceStats.numClients).toBe(1) expect(presenceStats.numUsers).toBe(1); diff --git a/src/subscription.ts b/src/subscription.ts index 7ca53452..a5aa9875 100644 --- a/src/subscription.ts +++ b/src/subscription.ts @@ -37,6 +37,7 @@ export class Subscription extends (EventEmitter as new () => TypedEventEmitter) { @@ -65,6 +66,7 @@ export class Subscription extends (EventEmitter as new () => TypedEventEmitter TypedEventEmitter TypedEventEmitter TypedEventEmitter