How do you subscribe to and publish messages on a specific Redis channel?
Short answer
You use the SUBSCRIBE command with a channel name to start listening for messages on that channel, and the PUBLISH command with a channel name and message content from a different client to send a message that all current subscribers of that channel will immediately receive.
Advertisement
AModel answer
You use the SUBSCRIBE command with a channel name to start listening for messages on that channel, and the PUBLISH command with a channel name and message content from a different client to send a message that all current subscribers of that channel will immediately receive.
Example
SUBSCRIBE weather_updates PUBLISH weather_updates 'Storm warning issued for the area'
Real-world example
A weather alert system publishes severe weather warnings to a dedicated channel, instantly notifying every subscribed application or device that is currently listening for updates.
Likely follow-ups
- What happens to a client's subscription if it disconnects and reconnects?
- Can you unsubscribe from a channel without disconnecting entirely?
Related topics