UNSUBSCRIBE
The UNSUBSCRIBE command is used to unsubscribe from specified channels. If no channel name is provided, all channels will be unsubscribed.
Redis Native Command Syntax
Section titled “Redis Native Command Syntax”UNSUBSCRIBE [channel [channel ...]]Parameter Description
- channel: Optional parameter, one or more channel names to unsubscribe from. If no channels are specified, unsubscribe from all channels.
Detailed Description
Section titled “Detailed Description”The UNSUBSCRIBE command is used to unsubscribe a client from one or more channels previously subscribed to through the SUBSCRIBE command. When a client no longer needs to receive messages from specific channels, this command can be used to unsubscribe.
Message Format
Section titled “Message Format”When a client successfully unsubscribes from a channel, it receives an array reply containing three elements:
- The first element is the “unsubscribe” string
- The second element is the name of the unsubscribed channel
- The third element is the number of channels currently subscribed by the client
If the client no longer subscribes to any channels after executing the UNSUBSCRIBE command, it will exit subscription state and can send any type of command.
Redisun Usage
Section titled “Redisun Usage”In Redisun, the UNSUBSCRIBE functionality is implemented through the UnsubscribeCommand class and the unsubscribe method in the Redisun class.
Basic Usage
Section titled “Basic Usage”Redisun redisun = Redisun.create(options -> { options.setAddress("redis://127.0.0.1:6379");});
// Subscribe to channelsredisun.subscribe(subscriber, "channel1", "channel2", "channel3");
// Unsubscribe from specific channelsredisun.unsubscribe("channel1", "channel2");
// Unsubscribe from all channelsredisun.unsubscribe();- If no channel parameters are provided, the UNSUBSCRIBE command will unsubscribe from all channels
- After unsubscribing from all channels, the client will exit subscription state and can execute other Redis commands
- Only channels that the current client has already subscribed to can be unsubscribed
- Unsubscribing from non-existent channels will not produce an error