Skip to content

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.

Terminal window
SUBSCRIBE channel [channel ...]

Parameter Description

  • channel: One or more channel names to subscribe to

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:

  1. Messages pushed by the PUBLISH command
  2. Results of SUBSCRIBE and UNSUBSCRIBE commands

In subscription state, the client cannot send commands other than SUBSCRIBE, UNSUBSCRIBE, PSUBSCRIBE, and PUNSUBSCRIBE.

When a message is published to a channel subscribed by the client, the client receives an array reply containing three elements:

  1. The first element is the “message” string
  2. The second element is the name of the channel that generated the message
  3. The third element is the actual message content

When a client successfully subscribes to a channel, it receives an array reply containing three elements:

  1. The first element is the “subscribe” string
  2. The second element is the name of the subscribed channel
  3. The third element is the number of channels currently subscribed by the client

In Redisun, the SUBSCRIBE functionality is implemented through the SubscribeCommand class and the subscribe method in the Redisun class.

Redisun redisun = Redisun.create(options -> {
options.setAddress("redis://127.0.0.1:6379");
});
// Create subscriber
Subscriber 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 channels
redisun.subscribe(subscriber, "channel1", "channel2");
// Publish message elsewhere
// redisun.publish("channel1", "Hello World!");

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
  1. Subscription connections are exclusive - once in subscription state, the connection cannot be used to execute other commands
  2. If you need to execute regular commands and subscription operations simultaneously, you should create multiple Redisun instances
  3. Subscribers should handle exceptions properly to avoid crashing the entire subscription system due to callback exceptions
  4. Subscription connections have automatic reconnection mechanisms and will attempt to resubscribe to previous channels when the connection is disconnected
  5. In practical applications, subscribers typically run in a separate thread or process