broadcom v0.1.1
Broadcom
A Broadcasting & Web Worker Communication JavaScript Library.
Installation
Step 1: Download the package from NPM:
npm i -S broadcom
Step 2: Decide if you want to rely on the CDN version of the worker or if you want to serve the file locally. If you choose the CDN route, do nothing, the broadcaster will automatically use the CDN hosted version of the web worker. If you need or want to serve the broadcast worker script yourself simply download the broadcaster-worker.min.js
script attached to the latest release and place it in your projects webroot directory. The broadcaster will automatically use the local version instead of the CDN version when available.
Usage
Broadcom exports three utility functions:
import { hookup, disconnect, message } from "broadcom";
Hookup
The hookup()
method requires an inbox alias and a callback function and returns the inboxes unique ID. The UID can be used to disconnect the inbox.
import { hookup } from "broadcom";
class Foo() {
constructor(){
this.inboxID = hookup('foo', this.inbox.bind(this));
}
inbox(data){
const {type} = data;
switch(type){
default:
console.warn(`Unknown message type: ${type}`);
break;
}
}
}
new Foo();
The data
object sent to the inbox will always have a type
key with a string value. All additional data is unique to the message but the data will always be transferable.
The inbox alias is not a unique string, multiple inboxes can and will use the same inbox alias.
Disconnect
The disconnect()
method requires the inboxes unique ID. The inbox UID is provided as the return value from the hookup()
method.
import { hookup, disconnect } from "broadcom";
class Foo() {
constructor(){
this.inboxID = hookup('foo', this.inbox.bind(this));
}
inbox(data){
const {type} = data;
switch(type){
default:
console.warn(`Unknown message type: ${type}`);
break;
}
}
disconnectInbox(){
disconnect(this.inboxID);
}
}
const foo = new Foo();
/** Disconnect the inbox after 3 seconds */
setTimeout(() => {
foo.disconnectInbox();
}, 3000);
The disconnect()
method is typically used when the inbox is attached to a UI component and the component is removed from the DOM.
Message
The message()
method requires the recipients inbox alias and a MessageData
object.
type MessageData = {
type: string;
[key: string]: any;
};
A maxAttempts
number can be provided or will default to 1. Max attempts is the number of times the broadcaster system should attempt to deliver the message to at least one inbox. Max attempts must be a number >= 1
and can be set to Infinity
if the message should never stop attempting delivery.
Anything can send a message to an inbox.
foo.js
import { hookup, disconnect } from "broadcom";
class Foo() {
constructor(){
this.inboxID = hookup('foo', this.inbox.bind(this));
}
inbox(data){
const {type} = data;
switch(type){
case 'bar':
console.log(data.message);
break;
default:
console.warn(`Unknown message type: ${type}`);
break;
}
}
disconnectInbox(){
disconnect(this.inboxID);
}
}
const foo = new Foo();
bar.js
import { message } from "broadcom";
function sendMessageOnInit() {
message("foo", { type: "bar", message: "This is part of the message object." }, Infinity);
}
sendMessageOnInit();
In the example above Foo
will receive the bar
message even if the foo.js
file is loaded after the bar.js
file.