DEV Community

Cover image for 🍻 Pub Sub 🍻

Posted on • Originally published at

🍻 Pub Sub 🍻

A terrible pun intended ☝️

Have you ever joined a mailing list or followed an account on social media? Have you ever subscribed to a channel on YouTube?

If your answer is "Yes!" to any of the questions above then congrats! πŸŽ‰ You pub-subbed.

Pub-subbed [ πŸ“’ puhb-suhbd ]
βšͺ verb
Becoming a part of publish-subscribe messaging system.
Creating a publish-subscribe messaging system.
"He pub-subbed his way out of the problem."


More accurate image to represent pub/sub

What is pub/sub?

Pub/sub in its full form publish/subscribe is a messaging pattern. It is a way for different components of a system to notify each other and it's a great alternative to polling.

In this pattern there are - you guessed it - publishersπŸ“€ and subscribersπŸ“₯. In addition to them there are messagesβœ‰οΈ and channels/topics πŸ“«.

πŸ’― Publishers can have any amount of subscribers
πŸ•΅οΈβ€β™‚οΈ Publishers don't have to know about their subscribers
⬅️ Publishers send messages to channels or topics to which subscribers can subscribe

πŸ’― Subscribers can subscribe to any amount of publishers
πŸ•΅οΈβ€β™‚οΈ Subscribers don’t need to know who the message comes from
➑️ Subscribers receive messages from channels or topics to which publishers send messages

What are the advantages of pub/sub?

βœ… Decoupling/Loose coupling

  • Since neither the publishers nor the subscribers have to know about each other pub-subbing helps with the decoupling of system components.

βœ… Scalability

  • When number of subscribers or channels/topics increases adding new servers to the system helps with dealing with the increased load.

βœ… Asynchronicity

  • Since publisher sends its messages to a channel or topic it doesn't have to wait for a subscriber to receive the message. And since the subscriber receives messages from a channel/topic it doesn't have to wait for a message. In fact it doesn't have to be up when the message was sent.

Do all these remind you something? Hint: The answer is a design pattern.


The answer is: Observer!

Pubsub is similar to Observer design pattern of Gof πŸ™†β€β™‚οΈ.

πŸ”’ Which problems does Observer solve? πŸ”’

From Gof

  • How can a one-to-many dependency between objects be defined without making the objects tightly coupled?
  • How can an object notify an open-ended number of other objects?

πŸ”‘ How does Observer solve these problems? πŸ”‘

From Gof

  • Define Subject and Observer objects
    • Subject -> Publisher and channel/topic
    • Observer -> Subscriber
  • When a subject changes state, all registered observers are notified and updated automatically
    • Subject changes state -> Publisher sends a message to channel/topic
    • Observers are notified and updated -> Subscribers are notified and updated


  • Just like in pubsub, in observer the subjects don't have to know about their observers and vice versa.
  • Just like in pubsub, in observer the subjects can have any amount of observers and vice versa.
  • Just like in pubsub, subjects and observers are loosely coupled.
  • Just like in pubsub, observers can be updated asynchronously.

If you're not sick of me associating things, both observer pattern and pub/sub messaging reminds me of the mailing systems in human world πŸ€·β€β™‚οΈ. It's really cool to see concepts from computing world resemble human world πŸ’» 🌎.

Cover Image: Photo by Mary Rebecca Elliott on Unsplash
Body Image: Photo by Mathyas Kurmann on Unsplash

Discussion (2)

induratized profile image
Abhinav Sharma

A good read.
Can you also update the post with some working code snippets ?
It really works wonder with code examples.

ufko profile image
ufko Author

Thank you! Sure I'll add some working code snippets ASAP πŸ‘