📖 Scenario: You are managing a chat application where users send messages to different chat rooms. You want to listen to messages from multiple chat rooms that share a common naming pattern.
🎯 Goal: Build a Redis subscriber that uses PSUBSCRIBE to listen to all chat rooms whose names start with room:.
📋 What You'll Learn
Create a Redis channel pattern variable called pattern with the value room:*
Use the PSUBSCRIBE command with the pattern variable to subscribe to matching channels
Implement a message handler function called message_handler that takes pattern, channel, and message as parameters
Print the received channel and message inside the message_handler function
💡 Why This Matters
🌍 Real World
Pattern matching subscriptions are useful in chat apps, notifications, and event-driven systems where you want to listen to groups of related channels.
💼 Career
Understanding PSUBSCRIBE helps in building scalable real-time applications and working with Redis Pub/Sub features in backend development.
Progress0 / 4 steps
1
Set up the Redis client connection
Create a Redis client connection variable called client using redis.Redis().
Redis
Hint
Use redis.Redis() to create a client connection to Redis.
2
Create the channel pattern variable
Create a variable called pattern and set it to the string 'room:*' to match all channels starting with room:.
Redis
Hint
Use the wildcard * to match any channel name starting with room:.
3
Define the message handler function
Define a function called message_handler that takes three parameters: pattern, channel, and message. Inside the function, print the channel and message.
Redis
Hint
Use an f-string to print the channel and message inside the function.
4
Subscribe to channels using PSUBSCRIBE with the pattern
Use client.pubsub() to create a PubSub object called pubsub. Then use pubsub.psubscribe() with the pattern and message_handler as the callback to subscribe to matching channels.
Redis
Hint
Use psubscribe with a dictionary mapping the pattern to the handler function.
Practice
(1/5)
1. What does the Redis command PSUBSCRIBE do?
easy
A. It subscribes to channels matching a pattern with wildcards.
B. It unsubscribes from all channels.
C. It publishes messages to a channel.
D. It deletes keys matching a pattern.
Solution
Step 1: Understand the purpose of PSUBSCRIBE
PSUBSCRIBE listens to messages from channels that match a given pattern, using wildcards like '*'.
Step 2: Compare with other commands
Unlike PUBLISH or UNSUBSCRIBE, PSUBSCRIBE focuses on pattern-based subscription.
Final Answer:
It subscribes to channels matching a pattern with wildcards. -> Option A
Quick Check:
PSUBSCRIBE = pattern subscription [OK]
Hint: PSUBSCRIBE listens to patterns, not single channels [OK]
Common Mistakes:
Confusing PSUBSCRIBE with PUBLISH
Thinking PSUBSCRIBE deletes keys
Mixing PSUBSCRIBE with UNSUBSCRIBE
2. Which of the following is the correct syntax to subscribe to all channels starting with 'news.' using PSUBSCRIBE?
easy
A. PSUBSCRIBE news?
B. PSUBSCRIBE news.*
C. PSUBSCRIBE news#
D. PSUBSCRIBE news+
Solution
Step 1: Identify the wildcard for pattern matching
In Redis PSUBSCRIBE, '*' matches any sequence of characters.
Step 2: Match the pattern for channels starting with 'news.'
The pattern 'news.*' matches all channels beginning with 'news.' followed by anything.
Final Answer:
PSUBSCRIBE news.* -> Option B
Quick Check:
'*' wildcard = correct syntax [OK]
Hint: Use '*' to match any characters in PSUBSCRIBE patterns [OK]
Common Mistakes:
Using '?' which matches exactly one character
Using '#' or '+' which are not Redis wildcards
Missing the dot after 'news'
3. Given the following Redis commands executed in order: