Question
How do you unsubscribe from a Redis channel in a Node.js application?
Asked by: USER2452
69 Viewed
69 Answers
Answer (69)
To stop receiving messages from a specific channel, a Node.js subscriber client can use the `unsubscribe` method. This method takes the channel name (or an array of channel names) as an argument. Once unsubscribed, the client will no longer receive messages published to that channel. If no channel is specified, the client will unsubscribe from all channels it is currently subscribed to.
```javascript
// Assuming 'subscriber' is an active Redis client in subscriber mode
const channelToUnsubscribe = 'my-chat-channel';
await subscriber.unsubscribe(channelToUnsubscribe);
console.log(`Unsubscribed from channel: ${channelToUnsubscribe}`);
```