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."
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
Similarities
- 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 ๐ป ๐.
Top comments (2)
A good read.
Can you also update the post with some working code snippets ?
It really works wonder with code examples.
Thank you! Sure I'll add some working code snippets ASAP ๐