![Oracle Drags Its Feet in the JavaScript Trademark Dispute](https://cdn.sanity.io/images/cgdhsj6q/production/919c3b22c24f93884c548d60cbb338e819ff2435-1024x1024.webp?w=400&fit=max&auto=format)
Security News
Oracle Drags Its Feet in the JavaScript Trademark Dispute
Oracle seeks to dismiss fraud claims in the JavaScript trademark dispute, delaying the case and avoiding questions about its right to the name.
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.
Browbeat uses 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 = new Browbeat();
You may optionally pass an options object to the constructor. Available options are:
true
Browbeat 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 = new Browbeat();
// If we win an election, establish a socket connection.
bb.on('wonElection', function won() {
var socket = new WebSocket('myhost');
socket.onopen = function connectionOpen() {
socket.onmessage = function socketMessage(msg) {
// Forward socket message to slaves
bb.messageSlaves(msg.data);
// Use data in this window
var data = JSON.parse(msg.data);
alert(data);
}
}
});
// Handle messages from master
bb.on('slave', function slaveMessage(msg) {
var data = JSON.parse(msg.data);
alert(data);
});
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()
Resign will only work on the current master. It'll trigger the event resigned
on the master and stop it's own heartbeat. Which in turn will lead to a new
election being initiated.
on(_event_, _handler_)
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.
off(_event_, _handler_)
Removes the given handler from the given event. hanlder
must be a reference
to the exact same function as was given to on()
.
broadcast(_message_)
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.
messageMaster(_message_)
Sends a message to the master. Will trigger the master
message on the master
only, the handler recieves the messages.
messageSlave(_message_)
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
argument.
sendMessage(_message_, _data_)
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 on()
method.
var bb = new Browbeat();
bb.on('wonElection', function () {
// This window won the election. Establish socket connections etc.
});
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.
FAQs
Elect a master browser window.
The npm package browbeat receives a total of 2 weekly downloads. As such, browbeat popularity was classified as not popular.
We found that browbeat demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Oracle seeks to dismiss fraud claims in the JavaScript trademark dispute, delaying the case and avoiding questions about its right to the name.
Security News
The Linux Foundation is warning open source developers that compliance with global sanctions is mandatory, highlighting legal risks and restrictions on contributions.
Security News
Maven Central now validates Sigstore signatures, making it easier for developers to verify the provenance of Java packages.