SUBSCRIBE
The SUBSCRIBE command is used to listen for messages on specified channels. After a client enters subscription state, it can only receive messages pushed by the PUBLISH command and can no longer send commands other than SUBSCRIBE, UNSUBSCRIBE, PSUBSCRIBE, and PUNSUBSCRIBE.
Redis Native Command Syntax
Section titled “Redis Native Command Syntax”SUBSCRIBE channel [channel ...]Parameter Description
- channel: One or more channel names to subscribe to
Detailed Description
Section titled “Detailed Description”The SUBSCRIBE command is used to subscribe to one or more given channels. Once a client enters subscription state, it can only receive the following types of command replies:
- Messages pushed by the PUBLISH command
- Results of SUBSCRIBE and UNSUBSCRIBE commands
In subscription state, the client cannot send commands other than SUBSCRIBE, UNSUBSCRIBE, PSUBSCRIBE, and PUNSUBSCRIBE.
Message Format
Section titled “Message Format”When a message is published to a channel subscribed by the client, the client receives an array reply containing three elements:
- The first element is the “message” string
- The second element is the name of the channel that generated the message
- The third element is the actual message content
Subscription Confirmation
Section titled “Subscription Confirmation”When a client successfully subscribes to a channel, it receives an array reply containing three elements:
- The first element is the “subscribe” string
- The second element is the name of the subscribed channel
- The third element is the number of channels currently subscribed by the client
Redisun Usage
Section titled “Redisun Usage”In Redisun, the SUBSCRIBE functionality is implemented through the SubscribeCommand class and the subscribe method in the Redisun class.
Basic Usage
Section titled “Basic Usage”Redisun redisun = Redisun.create(options -> { options.setAddress("redis://127.0.0.1:6379");});
// Create subscriberSubscriber subscriber = new Subscriber() { @Override public void onMessage(String channel, String message) { System.out.println("Received message: " + message + " from channel: " + channel); }
@Override public void onSubscribe(String channel) { System.out.println("Subscribed to channel: " + channel); }
@Override public void onUnsubscribe(String channel) { System.out.println("Unsubscribed from channel: " + channel); }
@Override public void onError(Throwable throwable) { System.err.println("Subscription error: " + throwable.getMessage()); }};
// Subscribe to channelsredisun.subscribe(subscriber, "channel1", "channel2");
// Publish message elsewhere// redisun.publish("channel1", "Hello World!");Subscriber Interface Details
Section titled “Subscriber Interface Details”The subscription functionality in Redisun is implemented through the Subscriber interface, which provides the following callback methods:
- onSubscribe(String channel): Called when successfully subscribing to a channel
- onUnsubscribe(String channel): Called when successfully unsubscribing from a channel
- onMessage(String channel, String message): Called when receiving a channel message
- onError(Throwable throwable): Called when a subscription connection encounters an error
- Subscription connections are exclusive - once in subscription state, the connection cannot be used to execute other commands
- If you need to execute regular commands and subscription operations simultaneously, you should create multiple Redisun instances
- Subscribers should handle exceptions properly to avoid crashing the entire subscription system due to callback exceptions
- Subscription connections have automatic reconnection mechanisms and will attempt to resubscribe to previous channels when the connection is disconnected
- In practical applications, subscribers typically run in a separate thread or process