Skip to content

Subscribing Consumers to Topic(s)

Mustafa TURAN edited this page Oct 15, 2018 · 2 revisions

Assume that you registered topics, created consumers (subscribers) that can handle these topics and your application is ready to emit events for those topics. Ok, it looks it is time to subscribe your consumers to the right topics.

The event_bus library handles topic subscription using the regular expression patterns. It allows you to write multiple regex patterns to subscribe consumers to one topic or many topics.

Subscribe a consumer to all registered topics:

subscriber = MyFirstConsumer
topics = [".*"]
EventBus.subscribe({subscriber, topics})

Subscribe a consumer to only :user_registered topic:

subscriber = MyFirstConsumer
topics = ["^user_registered$"]
EventBus.subscribe({subscriber, topics})

Subscribe a consumer to multiple topics (:user_registered and :email_sent):

subscriber = MyFirstConsumer
topics = ["^user_registed$", "^email_sent$"]
EventBus.subscribe({subscriber, topics})

Subscribe a consumer to topics starts with the :user_* :

subscriber = MyFirstConsumer
topics = ["^user_*"]
EventBus.subscribe({subscriber, topics})

Congratulations!!! You subscribed your subscribers(consumers) to the registered topics. Now you can emit events and see the consumers in action.