Agenda:

In this article, We are going to discuss event-oriented Publisher-Subscriber Pattern also referred as Pub-Sub Pattern. I have used C# as base language to implement and tried to keep discussion short and simple. Let us get started with terminologies.

Terminology:

  • Publisher: Message sender.
  • Subscriber: Message recepients.
  • Message Broker: A mediator which receive message from publisher and deliver it to subscriber.

Introduction

Publisher-Subscriber is a software design pattern and it is suitable for problems where a system component(publisher) need to provide information to other components(subscribers) as soon as there is change in object state. This also allow to achieve loose coupling between different components of a system. It is similar to asynchronous event-oriented messaging system.


Publisher sends a message to Messgae broker and Message broker notify the Subscribers. Message broker gurantees that message will be delivered sometime in future but not specific. It is worth mentioning that, Sender (Publisher) do not have knowledge of existence of Receivers (Subscribers). It is only role to broadcast information.

Keypoints

  • It decouple system components and can be managed independently.
  • Sender does not have to wait for response so it can easily complete other operations. Asynchronicity makes component more responsive.
  • Improved testability of system.
  • Increased scability. A new recepient simply have to subscribe to message broker.
  • This only support Unidirectional communication from Publisher to Subscriber.
  • It is hightly recommended to use existing message broker available like Redis, RabbitMQ etc.

Hands-On

Publisher

  1. class Publisher
  2. {
  3. public event EventHandler<SampleEventArgs> onPublish;
  4.  
  5. public void Publish() {
  6.  
  7. onPublish(this, new SampleEventArgs("Event Published"));
  8.  
  9. }
  10.  
  11. }

Subscriber

  1. class Subscriber
  2. {
  3. public IPublisher Publisher { get; private set; }
  4. public Subscriber(IPublisher publisher) {
  5. this.Publisher = publisher;
  6. }
  7. }

Client

  1. class Program
  2. {
  3. static void Main(string[] args)
  4. {
  5. IPublisher p = new Publisher();
  6.  
  7. Subscriber s1 = new Subscriber(p);
  8. s1.Publisher.onPublish += Publisher_onPublish1;
  9.  
  10. Subscriber s2 = new Subscriber(p);
  11. s2.Publisher.onPublish += Publisher_onPublish2;
  12.  
  13. p.Publish();
  14.  
  15. Console.ReadLine();
  16. }
  17.  
  18. private static void Publisher_onPublish2(object sender, SampleEventArgs e)
  19. {
  20. Console.WriteLine(e.Message);
  21. }
  22.  
  23. private static void Publisher_onPublish1(object sender, SampleEventArgs e)
  24. {
  25. Console.WriteLine(e.Message);
  26. }
  27. }

For full source code refer rgilotra

Conclusion:

In this article, we learnt how pub-sub pattern can be implemented where notifications needs to be sent to consumers with the help of mediator. Your feedbacks are welcome.

Good Bye 👋 !! until I write again. Code well, Cook good.