Redis UNSUBSCRIBE – How to unsubscribe from multiple channels in redis pub/sub

2 weeks ago Lalit Bhagtani 0

In this tutorial, we will learn about how to unsubscribe from multiple channels in redis message broker system using redis-cli.

UNSUBSCRIBE Command

The UNSUBSCRIBE command is used to unsubscribe the client from one or more specified channels in redis message broker system. If no channels are specified, then client is unsubscribed from all the subscribed channels. It returns a message for every unsubscribed channel.

The syntax of redis UNSUBSCRIBE command is as follows :-

Syntax :-

redis host:post> UNSUBSCRIBE <channel> [ <channel> ]

Output :- 

- (array) reply of 3 elements.

Format of Pushed Message :-

A message is a array reply with three elements. The first element of the array reply is unsubscribe which means that we have successfully unsubscribed from the channel given as the second element in the reply. The third argument represents the number of channels we are currently subscribed to. When the last argument is zero, client is no longer subscribe to any channel, and it can issue any redis command as it is outside of subscribed state.

Example :-

Redis UNSUBSCRIBE

References :-

  1. UNSUBSCRIBE Command Docs

That’s all for how to unsubscribe from one or more channels in redis message broker using redis-cli. If you liked it, please share your thoughts in comments section and share it with others too.