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 π