Elect a master window using the Bully algorithm.
This is a very early release. Not quite recommended for production.
Imagine you have a site where it's common for users to open multiple tabs.
Maybe you have some realtime functionality or something that requires a lot of
AJAX requests or similar. Instead of maintaing one socket or having
one message queue (for AJAX) per tab you can have one window being
responsible. One sockect connection instead of
n socket connections.
localStorage as a message bus for communication between open
windows on the same domain. Using the Bully algorithm one window is elected to
be the master.
The elected window can then be used to for example keep one open websocket instead of one per tab.
If Browbeat is loaded in a browser that does not support
localStorage it will
always behave as if it was the master. All the appropriate events etc
will be broadcast "locally" on the window once such a condition is detected.
This means you don't have to separate your logic if you want to support all cases.
To use Browbeat you simple instantiate a new object using the constructor.
var bb = ;
You may optionally pass an options object to the constructor. Available options are:
trueBrowbeat will output a tiny amount of debug information to help you determine the current state.
A common usecase would be to manage a single socket connection. You could do this by writing something like this:
var bb = ;// If we win an election, establish a socket connection.bb;// Handle messages from masterbb;
Most of the Browbeat API is internal and you should mostly interact with it by listening to events. There are a few methods you need to be aware of though.
Resign will only work on the current master. It'll trigger the event
on the master and stop it's own heartbeat. Which in turn will lead to a new
election being initiated.
Register an event listener with Browbeat. The
name is the name of the event
you want to listen for.
handler is the function you want to be triggered
when the event is emitted.
The handler will be passed any additional data provided by the event.
Removes the given handler from the given event.
hanlder must be a reference
to the exact same function as was given to
Sends a message to all open windows on the same domain. Will trigger the
broadcast event with the message as the only argument to the handler.
Sends a message to the master. Will trigger the
master message on the master
only, the handler recieves the messages.
Sends a message to all slaves. Will trigger the event
slave on all open
windows that are not the master. The handler recieves the message as it's only
Let's you trigger an arbitrary event on all windows other then the current one. You can use this to dispatch custom events.
Note that data can only be serialized data, ie. you need to stringify JSON first or similar.
Browbeat implements it's own custom event emitter. You can subsrive to events
by calling the
var bb = ;bb;
resign()method is called).
Custom events can be sent by calling the
sendMessage() method. The
method takes two arguments; message and data. Message is the event that
will be triggered on any other open window.
Should support all major browsers and IE8+.
Browbeat is distributed under the MIT license.