Publishing to channels lets you send messages instantly to many listeners. It helps different parts of a program talk to each other easily.
PUBLISH to channels in Redis
PUBLISH channel message
channel is the name of the channel to send the message to.
message is the text you want to send.
PUBLISH news "Hello subscribers!"PUBLISH updates "New version available"PUBLISH alerts "Warning: High CPU usage"This example shows subscribing to the "news" channel in one client and then publishing two messages to it from another client. The subscriber will receive both messages.
# Subscriber (run in one client): SUBSCRIBE news # Publisher (run in another client): PUBLISH news "Hello subscribers!" PUBLISH news "Second message"
The PUBLISH command returns the number of clients that received the message.
Publishing is fast and does not store messages; if no clients are listening, the message is lost.
Use PUBLISH when you want real-time communication without saving messages.
PUBLISH sends messages to all clients listening on a channel.
It is useful for real-time notifications and broadcasts.
Messages are not saved; only connected subscribers get them.
Practice
What does the PUBLISH command do in Redis?
Solution
Step 1: Understand the purpose of PUBLISH
The PUBLISH command sends messages to clients currently subscribed to a channel.Step 2: Differentiate from storage commands
PUBLISH does not store messages permanently; it only broadcasts to active subscribers.Final Answer:
Sends a message to all clients subscribed to a specific channel -> Option BQuick Check:
PUBLISH broadcasts messages [OK]
- Thinking PUBLISH stores messages permanently
- Confusing PUBLISH with channel creation
- Assuming PUBLISH deletes messages
Which of the following is the correct syntax to publish a message 'Hello World' to a channel named news in Redis?
?Solution
Step 1: Recall Redis PUBLISH syntax
The syntax is: PUBLISH channel message. Quote the message if it contains spaces.Step 2: Identify correct option
PUBLISH news 'Hello World' matches the syntax exactly.Final Answer:
PUBLISH news 'Hello World' -> Option AQuick Check:
Correct Redis PUBLISH syntax = PUBLISH news 'Hello World' [OK]
- Not quoting messages with spaces
- Swapping channel and message order
- Using commas or extra symbols
Given the following Redis commands executed in order:
SUBSCRIBE sports
PUBLISH sports "Game tonight"
PUBLISH news "Weather update"What messages will the subscriber receive?
Solution
Step 1: Identify subscribed channel
The subscriber is subscribed to the 'sports' channel only.Step 2: Check published messages
Messages published to 'sports' are received; messages to 'news' are ignored by this subscriber.Final Answer:
Only "Game tonight" -> Option AQuick Check:
Subscribers get messages only from their channels [OK]
- Assuming subscriber gets all published messages
- Confusing channel names
- Ignoring subscription context
What is wrong with this Redis command to publish a message?
PUBLISHChoose the best explanation.
Solution
Step 1: Check required arguments for PUBLISH
PUBLISH requires two arguments: channel and message.Step 2: Identify missing parts in the command
The command only has PUBLISH with no arguments, so it is incomplete.Final Answer:
Missing channel and message arguments -> Option DQuick Check:
PUBLISH needs channel and message [OK]
- Thinking PUBLISH works without arguments
- Assuming message length causes error here
- Believing SUBSCRIBE is required before PUBLISH
You want to send a notification to multiple channels news and alerts at once. Which Redis approach is best?
Solution
Step 1: Understand Redis PUBLISH limitations
Redis PUBLISH sends messages to one channel at a time; no built-in multi-channel publish exists.Step 2: Choose correct method to notify multiple channels
Send separate PUBLISH commands for each channel to reach all subscribers.Final Answer:
Use multiple PUBLISH commands, one per channel -> Option CQuick Check:
Multiple channels need multiple PUBLISH calls [OK]
- Expecting one PUBLISH to reach multiple channels
- Combining channel names incorrectly
- Assuming Redis supports multi-channel publish natively
Ready to prove it?
Pass the test and this pattern moves unknown
