0
0
Redisquery~5 mins

Real-time notification pattern in Redis

Choose your learning style9 modes available
Introduction

Real-time notifications let apps send messages instantly to users. This keeps users updated without refreshing the page.

When a chat app needs to show new messages immediately.
When a stock price changes and users must see updates right away.
When a game sends alerts about player moves or scores.
When a social media app shows likes or comments as they happen.
When a monitoring system alerts about server status changes instantly.
Syntax
Redis
PUBLISH channel message
SUBSCRIBE channel

PUBLISH sends a message to a channel.

SUBSCRIBE listens for messages on a channel.

Examples
Sends a message to the 'news' channel.
Redis
PUBLISH news "Breaking news: Redis is fast!"
Starts listening to messages on the 'news' channel.
Redis
SUBSCRIBE news
Sends an alert message to the 'alerts' channel.
Redis
PUBLISH alerts "Server down!"
Listens for alert messages on the 'alerts' channel.
Redis
SUBSCRIBE alerts
Sample Program

First, you listen to the 'notifications' channel. Then, you send a message to that channel. The subscriber immediately receives the message.

Redis
# In one terminal, subscribe to channel:
SUBSCRIBE notifications

# In another terminal, publish a message:
PUBLISH notifications "New user signed up!"
OutputSuccess
Important Notes

Subscribers only get messages sent after they start listening.

Channels are like chat rooms where messages are shared instantly.

Redis Pub/Sub does not store messages; if no one is listening, messages are lost.

Summary

Use PUBLISH to send messages to a channel.

Use SUBSCRIBE to listen for messages on a channel.

This pattern helps apps send instant updates to users.