This is a brief video detailing “PubSub“, also known as a Publish-Subscribe pattern in software architecture. This is an extremely common pattern where senders of messages, called publishers, do not program the messages to be sent directly to specific receivers, called subscribers, but instead categorize published messages into classes without knowledge of which subscribers, if any, there may be.
A better way to visualize and understand this concept is displayed in the video. Alex Young codes a simple “clicker” style game in JavaScript where you get a certain amount of “xp” that fill up a progress bar per second. You can increase this gain by clicking a button that adds xp. Once your progress bar fills up, you move on to the next level and the progress bar resets. The way that PubSub fits into this is by publishing an xp_changed event and having the game’s User Interface subscribe to that event in order to update the progress bar and update the current xp. He also publishes when he “levels up” so that the game can recognize that it needs to reset the progress bar and progress the player on to the next level.
This application of a PubSub pattern was especially helpful for me in understanding a practical way to use an extremely popular pattern and also allow me to visualize how I might use it in the future. Coding in JavaScript is something I’m rather unfamiliar with but this video does a good job of describing the process in simple terms and showing how each change affects the game.
I chose this particular video because in the video I detailed in my previous blog post, the author spoke in depth on PubSub patterns and how prevalent they were in Software Architecture. I wanted to learn more about how they work and practical basic uses that I may encounter so I found a brief video showing a concrete example of how PubSub functions in JavaScript.
One thing that I found was particularly enlightening was actually in the comments of this video. Another viewer compared PubSub in JavaScript to event listeners in Vue. Since we were recently learning about Vue, I was able to use my understanding of Vue to better understand the examples in JavaScript.
From the blog CS@Worcester – Jeremy Studley's CS Blog by jstudley95 and used with permission of the author. All other rights reserved by the author.