Last Updated: June 14, 2019
·
2.081K
· mutahhir

A great little javascript messaging library

PubSubJS by Morgan Roderick is a great little messaging/event library for Javascript. Based on the Publisher/Subscriber pattern as the name shows, it's dependency free and really really easy to use.

For listening to messages, just call

PubSub.subscribe('message name', function(messageName, eventArgs) { });

and for publishing a message:

PubSub.publish('message name', args);

However, my favorite part of this library is it's support for hierarchal addressing. Say you've got a javascript class called Document. Now, say you decide that each message published by the Document class will be prefixed with document. So, textChanged becomes document.textChanged and so on.

Now, within PubSubJS, you can subscribe to the document.textChanged message, or you can even subscribe to all messages by just subscribing to document. This works for multiple levels of hierarchy, so you can choose to publish all actions under document.actions and the subscribers can listen to all possible action messages by subscribing to just document.actions.

Great work by Morgan Roderick. Thanks man!