In this lecture, we will explore how subscribers can listen to multiple channels using pattern-based subscriptions instead of subscribing to exact topic names. This allows greater flexibility and selective listening in real-time messaging systems.
1. Exact Topic vs Pattern-Based Subscriptions
Exact Topic Subscription:
- Subscriber listens to a single topic.
- Only messages published to that topic are received.
- Example:
Subscriber subscribes to "slack-room-1" Only receives messages published to "slack-room-1"
Pattern-Based Subscription:
- Subscriber listens to multiple topics that match a specific pattern.
- Patterns can include wildcards, prefixes, or regular expressions.
- Useful when a subscriber wants messages from a group of related topics.
- Example:
Subscriber subscribes to "slack-room-*" Receives messages from "slack-room-1", "slack-room-2", "slack-room-3", etc.
2. Example Use Case
- Subscriber 1: Interested only in
"slack-room-1"
- Subscriber 2: Interested in all rooms starting with
"slack-room-"
This allows one subscriber to filter messages precisely while another receives a broader range.
3. Implementing Pattern-Based Subscription
Step 1: Define Subscription Pattern
- Use a string pattern to define the topics of interest.
- Example:
String patternAllRooms = "slack-room-*"; String patternRoomOne = "slack-room-1";
Step 2: Subscribe to Topics Using Patterns
- Subscribers can implement a reactive or lambda-based listener.
- Each message includes:
- The topic name
- The message content
subscriber.subscribe(patternAllRooms, (topic, message) -> { System.out.println("Subscriber 2 received from " + topic + ": " + message); }); subscriber.subscribe(patternRoomOne, (topic, message) -> { System.out.println("Subscriber 1 received from " + topic + ": " + message); });
- This implementation allows lambda expressions for simplicity.
- The listener interface typically has three parameters:
- Topic – the name of the channel the message came from.
- Message – the actual payload.
- Pattern – optional reference to the subscription pattern.
Step 3: Publish Messages
- Using a CLI or programmatically, messages are published to specific topics:
publisher.publish("slack-room-1", "Hi everyone in Room 1!"); publisher.publish("slack-room-2", "Hello from Room 2!");
- Message Delivery Rules:
- Subscriber 1 (exact match
"slack-room-1"
) receives only Room 1 messages. - Subscriber 2 (pattern
"slack-room-*"
) receives all messages from any room starting with"slack-room-"
.
- Subscriber 1 (exact match
4. Behavior Overview
- Exact match subscription:
- Only receives messages for that topic.
- Ignores all other topics.
- Pattern subscription:
- Receives messages for all topics matching the pattern.
- Can include multiple channels without listing them individually.
- Inactive subscribers:
- If a subscriber is offline during message publication, it misses those messages.
5. Example Flow
- Subscriber Setup:
- Subscriber 1 subscribes to
"slack-room-1"
. - Subscriber 2 subscribes to
"slack-room-*"
.
- Subscriber 1 subscribes to
- Publish Messages:
"slack-room-1": Hi everyone in Room 1!
- Subscriber 1 → receives
- Subscriber 2 → receives
"slack-room-2": Hello from Room 2!
- Subscriber 1 → ignores
- Subscriber 2 → receives
- Offline Scenario:
- Subscriber 1 goes offline.
- A message is published to
"slack-room-1"
. - Subscriber 1 does not receive the message.
6. Key Points
- Pattern subscriptions allow dynamic and flexible message routing.
- Ideal for scenarios with many channels where a subscriber wants messages from a subset of topics.
- Works well with reactive streams and real-time messaging systems.
- Subscribers can implement lambda functions to handle messages efficiently.
- Offline subscribers will miss messages unless persistence is implemented.