0
0
Redisquery~5 mins

PUBLISH to channels in Redis

Choose your learning style9 modes available
Introduction

Publishing to channels lets you send messages instantly to many listeners. It helps different parts of a program talk to each other easily.

You want to send a notification to many users at once, like a chat message.
You need to update multiple parts of your app when data changes, like live scores.
You want to trigger actions in other services when something happens, like sending an email.
You want to broadcast alerts or warnings to all connected clients.
You want to build a real-time feed that many users can watch simultaneously.
Syntax
Redis
PUBLISH channel message

channel is the name of the channel to send the message to.

message is the text you want to send.

Examples
Sends the message "Hello subscribers!" to the channel named "news".
Redis
PUBLISH news "Hello subscribers!"
Sends an update message to the "updates" channel.
Redis
PUBLISH updates "New version available"
Sends an alert message to the "alerts" channel.
Redis
PUBLISH alerts "Warning: High CPU usage"
Sample Program

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.

Redis
# Subscriber (run in one client):
SUBSCRIBE news

# Publisher (run in another client):
PUBLISH news "Hello subscribers!"
PUBLISH news "Second message"
OutputSuccess
Important Notes

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.

Summary

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.