Learnitweb

Pattern-Based Subscriptions in Pub/Sub

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:
    1. The topic name
    2. 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:
    1. Topic – the name of the channel the message came from.
    2. Message – the actual payload.
    3. 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-".

4. Behavior Overview

  1. Exact match subscription:
    • Only receives messages for that topic.
    • Ignores all other topics.
  2. Pattern subscription:
    • Receives messages for all topics matching the pattern.
    • Can include multiple channels without listing them individually.
  3. Inactive subscribers:
    • If a subscriber is offline during message publication, it misses those messages.

5. Example Flow

  1. Subscriber Setup:
    • Subscriber 1 subscribes to "slack-room-1".
    • Subscriber 2 subscribes to "slack-room-*".
  2. 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
  3. 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.