Backend Engineering
brian | Published: Feb. 24, 2024, 1:28 p.m. | Updated: May 25, 2025, 7:33 p.m.
Publish Subscribe
Publish subscribe is a messaging pattern where the publishers put their messages into categories, which are then received by the subscriber. Basically the publisher can post something to the server, and he can then move on with his day. Then later on in the day, the client can consume this piece from the server.
How it Works
1: publishers produce the message, so they create a message and publish it to the server.
2: if the subscriber shows interest in a particular channel, or topic then they can subscribe, and now that they're subscribed, they will recieve messages from that publisher
pros: "loose coupling" publishers don't even need to know about the subscribers existence.
cons: "Message delivery issues"
Example 1: When you upload a video to youtube, and once you're done uploading, thats it, your job is finished and you can close the tab if you wish. After you have uploaded the video, the mp4 gets sent to the broker, and there all things like compressing the video, and processing taken care of, and once that's done the subscribers can view the videos
Example 2:
Video Upload Notifications:
When a user uploads a new video, YouTube's system publishes a message to a "New Video Uploads" section.
Subscribers, of the channel or users who opted for notifications, receive notifications about the new video in real-time.
Webhooks
Webhooks are custom callback URLS that an application can call to communicate with another application, and it enables real-time communication between systems or applications over HTTP. Webhooks are triggered by some sort of an event that occurs (ie. posting an article to my website)
For example: Discord provides webhook functionality, we can go to one of our channels, create one, and then we will be given a URL.
Now we can go to python, make a post request to that URL, and once we do that it will trigger an event on discord that causes the channel to display the message that you pass it. You can add this to your django application if you'd like for example when you create an article, but below is a simple example i created.
WEB_HOOK = 'https://discord.com/api/webhooks/1211827285368373299/NkH3duBGRBcYc-2wf4AImeZFw4Pd244Tcp31Jb_yuaNVYFo3J_p__MHmqx0vXYHj6fTx'
pay_load = {
'content':'new article created!'
}
#sends a post request to the URL provided by discord
requests.post(WEB_HOOK, json=pay_load)