PubSub Methods - sjonesyodle/Cluster GitHub Wiki

Cluster has two, very helpful methods built in called _pub & _sub, which publish messages to subscribing modules. Its best to think of a message as an ID of sorts--though its a string, it is used to describe an action or event in your app.

Since Cluster modules are initiated synchronously, all subscriptions must be defined before a message is published.

Inside of a Cluster Module, use the below method to subscribe to a publisher:

var token = this._sub("message", function(arg, arg ...){ /* CALLBACK */ });

"message" Should be a string. The callback can accept an argument list.


To publish a message (from within a Module), use:

this._pub("message"[, arg, arg ...]);

At any point you want to unsubscribe from a publisher, use:

this._unsub(token);

Notice that token is the variable that was assigned to the ._sub() above.