Redis - Publish Subscribe

Author: Al-mamun Sarkar Date: 2020-09-21 15:22:29

Redis - Publish-Subscribe is used to implement a message broker using Redis. In this lesson, I will show you how to create a message broker using Redis Pub/Sub.

 

Subscribe to a channel:

Syntax:

SUBSCRIBE channel_name ...

Example (Subscribe to news channel):

SUBSCRIBE news

 

Publish a message to a channel:

Syntax:

PUBLISH channel_name message

Example (Subscribe to news channel):

PUBLISH news "Welcome to Bangladesh"

All subscribers which subscribe to the news channel will receive a "Welcome to Bangladesh" message 

 

Subscribe to channel using patterns:

Syntax:

PSUBSCRIBE pattern

Example:

PSUBSCRIBE n*

 

Unsubscribe from a channel:

Syntax:

UNSUBSCRIBE channel_name ...

Example (Subscribe to news channel):

UNSUBSCRIBE news

 

Unsubscribe to channel using patterns:

Syntax:

PUNSUBSCRIBE pattern

Example:

PUNSUBSCRIBE n*