Databridges JavaScript Dedicated Worker Wrapper for browsers
Databridges JavaScript Dedicated Worker Wrapper allows you to run dataBridges library instance in the background without affecting the performance of the web page application.
Download source
Source is available in https://github.com/databridges-io/boilerplate.js.webworkers
Usage Overview
The following topics are covered:
Supported platforms
Safari | Firefox | Chrome | Edge | Opera | Firefox Android | Chrome Android | Opera Android |
---|---|---|---|---|---|---|---|
4+ | 3.5+ | 4+ | 12+ | 10.6+ | 4+ | 18+ | 11+ |
Initialization
Just start the dataBridges Dedicated Worker JavaScript file into your JavaScript code as shown below.
You need to replace appkey
and auth_url
with the actual URL and Application Key in the dbridgesDedicatedWorker.js
file in below section.
// Replace your dataBrdiges Application Key and Authentication URL below.
const dbApplnKey = 'appkey';
const dbAuthURL = 'auth_url';
Properties | Description |
---|---|
auth_url | (string) Authentication url from dataBridges dashboard. |
appkey | (string) Application Key from dataBridges dashboard. |
How to Use
To make the coding readable we will be using below structure. This will be used for defining both sending and receiving messages from and to Web Workers. Each event types will have different sets of parameters along with the event types.
NOTE : You can enhance
dbridgesDedicatedWorker.js
this file depending on your requirement. This file helps you get started with basic functionality of dataBridges network library.
const workerEvents = {
subscribe: 1,
publish: 2,
unsubscribe: 3,
channelList: 4,
dbStatus: 11,
dbLog: 12,
event: 16,
}
dbWebWorker.onmessage = function (e) {
const recdData = e.data;
switch (recdData.cmd) {
case workerEvents.dbStatus:
// Databridge connection status.
// recdData.msg will have dataBridges Offline/Online status whenever they are changed
console.log('dataBridges Connection Status :',recdData.msg);
break;
case workerEvents.dbLog:
// Log messages recieved from WebWorker
// Logs on dataBridges Events and other errors if any as and when generated.
console.log(recdData.msg, recdData.err != '' ? 'Error : ' + recdData.err:'');
break;
case workerEvents.channelList:
// Client List received from WebWorker
// List of channels subscirbed by current browser connect. The result is comma seperated.
console.log('Channels Subscribed :',recdData.msg);
break;
case workerEvents.event:
// Data Event received from dataBridges Event listner.
// Pubished events with data received by dataBridges on the Subscribed channel by this browser connect.
console.log('Event Received :', recdData.eventname,'Payload', recdData.payload,'MetaData :',JSON.stringify(recdData.metadata));
break;
default:
break;
}
};
Subscribing to Channel
To subscribe to a channel and listen to an event send a message to dbWebWorker
with below parameters. Replace Channel_Name
and Event_Name
with your Channel Name and Event Name
dbWebWorker.postMessage({
cmd: workerEvents.subscribe,
data: {
channel: 'Channel_Name',
event: 'Event_Name'
}
})
Unsubscribe from channel
To Unsubscribe from a channel send a message to dbWebWorker
with below parameters. Replace Channel_Name
with your Channel Name.
Subscribed channel list
To get list of all subscribed channels send a message to dbWebWorker
with below parameters.
Publish events to channel
To publish an event to a channel send a message to dbWebWorker
with below parameters. Replace Channel_Name
,Event_Name
and Payload
with your Channel Name , Event Name and Payload
dbWebWorker.postMessage({
cmd: workerEvents.publish,
data: {
channel: 'Channel_Name',
event: 'Event_Name',
payload: 'Payload'
}
})
Trust Tokens Implementation
While working with Private,Presence or System channel, you need to implement Trust Tokens. There is a function defined in dbridgesDedicatedWorker.js
which you can modify to get your own trust token implementation. If you want to use the same functionality, you need to expose /token
route in your server and expose the trust token creation functionality.
// Get access token when channel is private,presence or system.
const getAccessToken = (payloadJson, res) => {
fetch('/token', {
method: 'POST',
headers: {
Accept: 'application.json',
'Content-Type': 'application/json'
},
body: JSON.stringify(payloadJson)
}).then(response => {
if (!response.ok) {
throw new Error('Network response was not OK');
}
return response.json();
}).then(data => {
res.end(data);
}).catch(err => {
postMessage({
cmd: workerEvents.dbLog,
msg: 'dataBridges getAccessToken exception..',
err: err.source + ',' + err.code + ',' + err.message
});
res.end({
statuscode: 1,
error_message: error.message,
accesskey: ''
});
});
}