0
0
Redisquery~10 mins

SUBSCRIBE to channels in Redis - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - SUBSCRIBE to channels
Client sends SUBSCRIBE command
Server registers client to channel(s)
Server waits for messages on channel(s)
Message published to channel
Server sends message to subscribed client
Client receives message
Repeat for new messages or client unsubscribes
The client sends a SUBSCRIBE command to the server, which registers the client to listen to specified channels. When messages are published to those channels, the server forwards them to the client.
Execution Sample
Redis
SUBSCRIBE news sports

(Publisher) PUBLISH news "Hello News"

(Publisher) PUBLISH sports "Game Tonight"
Client subscribes to 'news' and 'sports' channels; messages published to these channels are received by the client.
Execution Table
StepActionServer StateClient Output
1Client sends SUBSCRIBE news sportsClient registered to channels: news, sportsSubscription confirmation for news and sports
2Publisher sends PUBLISH news "Hello News"Message queued for clients subscribed to newsReceived message on channel 'news': "Hello News"
3Publisher sends PUBLISH sports "Game Tonight"Message queued for clients subscribed to sportsReceived message on channel 'sports': "Game Tonight"
4No more messagesWaiting for new messages or unsubscribeNo output, waiting
💡 Execution waits indefinitely until client unsubscribes or connection closes
Variable Tracker
VariableStartAfter Step 1After Step 2After Step 3After Step 4
Subscribed Channels[][news, sports][news, sports][news, sports][news, sports]
Received Messages[][]["Hello News"]["Hello News", "Game Tonight"]["Hello News", "Game Tonight"]
Key Moments - 3 Insights
Why does the client receive a confirmation message after subscribing?
After the SUBSCRIBE command (Step 1), the server sends a confirmation to the client listing the channels subscribed, confirming registration as shown in the Client Output column.
Does the client receive messages from channels it did not subscribe to?
No, only messages published to channels the client subscribed to are forwarded. For example, messages on 'news' and 'sports' are received, but not others.
Why does the server keep waiting after delivering messages?
The SUBSCRIBE mode keeps the connection open to continuously receive messages until the client unsubscribes or disconnects, as shown in Step 4.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what channels is the client subscribed to after Step 1?
A[news, sports]
B[news]
C[sports]
D[]
💡 Hint
Check the Server State and Client Output columns in Step 1
At which step does the client receive the message "Game Tonight"?
AStep 1
BStep 2
CStep 3
DStep 4
💡 Hint
Look at the Client Output column for the message content
If the client unsubscribes after Step 3, what happens at Step 4?
AServer sends more messages
BClient receives no more messages
CClient automatically resubscribes
DServer closes connection immediately
💡 Hint
Refer to the exit_note and Step 4 description
Concept Snapshot
SUBSCRIBE command registers client to channels
Server forwards published messages on those channels
Client receives messages asynchronously
Connection stays open until unsubscribe or disconnect
Used for real-time message delivery
Full Transcript
The SUBSCRIBE command in Redis lets a client listen to one or more channels. When the client sends SUBSCRIBE with channel names, the server registers the client to those channels and confirms the subscription. Whenever a message is published to any of those channels, the server sends the message to the client. The client keeps receiving messages until it unsubscribes or disconnects. This allows real-time message delivery through channels.