Skip to content

Installation

Installation

You can use NPM package manager to install the package.

Use package installer npm to install dbridge libraries inside your application folder

npm install databridges-sio-server-lib --save

You can use pip package manager to install the package.

pip3 install databridges_sio_server_lib

You can use any visual studio package manager.

Install-Package Databridges.Sio.Server.Lib

Public Repository

Initialization

const dBridges = require('databridges-sio-server-lib');
const dbridge = new dBridges();
from databridges_sio_server_lib import dBridges
from databridges_sio_server_lib.exceptions import dBError
dbridge = dBridges()
using dBridges;
dBridges.dBridges dbridge = new dBridges.dBridges();

Global Configuration

Required

The following is the list of required connection properties before connecting to dataBridges network.

dbridge.auth_url = 'URL';
dbridge.appkey = 'APP_KEY';
dbridge.appsecret = 'APP_SECRET';
dbridge.auth_url = 'URL'
dbridge.appkey = 'APP_KEY'
dbridge.appsecret = 'APP_SECRET'
dbridge.auth_url = 'URL';
dbridge.appkey = 'APP_KEY';
dbridge.appsecret = 'APP_SECRET';

You need to replace URL , APP_KEY and APP_SECRET with the actual URL ,Application Key and Application Secret.

Properties Description Exceptions
auth_url (string) Authentication url from dataBridges dashboard. source: DBLIB_CONNECT
code: INVALID_URL
appkey (string) Application Key from dataBridges dashboard. source: DBLIB_CONNECT
code: INVALID_AUTH_PARAM
appsecret (string) Application Secret from dataBridges dashboard. source: DBLIB_CONNECT
code: INVALID_AUTH_PARAM

Optional

The following is the list of optional connection properties before connecting to dataBridges network.

dbridge.maxReconnectionRetries = 10;
dbridge.maxReconnectionDelay = 120000; 
dbridge.minReconnectionDelay = 1000 + Math.random() * 4000;
dbridge.reconnectionDelayGrowFactor = 1.3;
dbridge.minUptime = 200 ; 
dbridge.connectionTimeout = 10000;
dbridge.autoReconnect = true; 
dbridge.cf.enable = false; 
dbridge.maxReconnectionRetries = 10
dbridge.maxReconnectionDelay = 120000 
dbridge.minReconnectionDelay = 1000 + (new Random()).NextDouble() * 4000
dbridge.reconnectionDelayGrowFactor = 1.3
dbridge.minUptime = 200  
dbridge.connectionTimeout = 10000
dbridge.autoReconnect = true 
dbridge.cf.enable = false 
dbridge.maxReconnectionRetries = 10;
dbridge.maxReconnectionDelay = 120000; 
dbridge.minReconnectionDelay = 1000 + Math.random() * 4000;
dbridge.reconnectionDelayGrowFactor = 1.3;
dbridge.minUptime = 200 ; 
dbridge.connectionTimeout = 10000;
dbridge.autoReconnect = true; 
dbridge.cf.enable = false;
Properties Default Description
maxReconnectionDelay 10 (integer) The maximum delay between two reconnection attempts in seconds.
minReconnectionDelay 1000 + Math.random() * 4000 (integer) The initial delay before reconnection in milliseconds (affected by the reconnectionDelayGrowFactor value).
reconnectionDelayGrowFactor 1.3 (float) The randomization factor used when reconnecting (so that the clients do not reconnect at the exact same time after a server crash).
minUptime 200 (integer) Uptime before connected event is triggered, value in milliseconds.
connectionTimeout 10000 (integer) Number of milliseconds the library will wait for a connection to be established. If it fails it will emit a connection_error event.
maxReconnectionRetries 10 (integer) The number of reconnection attempts before giving up.
autoReconnect true (boolean) If false, library will not attempt reconnecting.
cf.enable false (boolean) Enable exposing client function for this connection. (Check Client Function section for details.)

Connection

Once the properties are set, use connect() function to connect to dataBridges Network.

dbridge.connect().catch((err) => {
    console.log('dBridge Connection exception..', err.source, err.code, err.message);
});
try:
    await dbridge.connect()
except Exception as e:
    print("source: {0} ,  code: {1}, message: {2}".format(e.source , e.code , e.message))
try{
    await dbridge.connect();
} catch (dBError err) {
    Console.WriteLine("{0} ,  {1} , {2}" , err.source, err.code, err.message);
}

Exceptions:

Source Code Message Description
DBLIB_CONNECT INVALID_URL Value of dbridge.auth_url is not a valid dataBridges authentication URL.
DBLIB_CONNECT INVALID_AUTH_PARAM Value of dbridge.appkey or dbridge.appsecret is not a valid dataBridges application key.
DBLIB_CONNECT ACCESSTOKEN_FAILED dbridge.appsecret validation failed.
DBLIB_CONNECT HTTP_ HTTP protocol reported message. HTTP Errors returned during authentication process. HTTP Error code will be concatenated with HTTP_ in the err.code. eg. HTTP_501
DBLIB_CONNECT INVALID_CLIENTFUNCTION If "callback function" is not declared for client function or typeof() variable defined is not a "function". This is applicable only if clientFunction is enabled. (Check Client Function section for details.)

sessionid (string)

console.log(dbridge.sessionid);
print("sessionid: {0}".format( dbridge.sessionid))
Console.WriteLine(dbridge.sessionid);

Making a connection provides the application with a new sessionid that is assigned by the application. This can be used to distinguish the application's own events. A change of state might otherwise be duplicated in the application. It is also stored within the connection, and used as a token for generating signatures for private/presence/system channels/rpc functions.

disconnect (function)

To close a connection use disconnect function. When a connection has been closed explicitly, no automatic reconnection will happen.

dbridge.disconnect();
await dbridge.disconnect()
dbridge.disconnect();

Objects

Object Description
connectionstate connectionstate object expose properties, functions and events to monitor and manage the health of dataBridges network connection.
channel channel object exposes trust-safe flexible Pub/Sub messaging properties, functions and events to build real-time event messaging / event driven applications at scale.
rpc rpc object exposes trust-safe properties, functions and events to provide reliable two-way messaging between multiple endpoints allowing you to build sophisticated asynchronous interactions.
cf CF (Client-function) object is a special purpose RPC implementation to build command and control applications. CF object exposes properties, functions and events for command and control applications to send messages to devices and application using dataBridges client library in **trust-safe manner **, build smart update configuration system and implement **trust-safe ** actions for remote and automated management.

object:ConnectionState

Connectionstate object expose properties, functions and events to monitor and manage the health of dataBridges network connection.

Properties

The following is the list of connection state properties.

console.log(dbridge.connectionstate.state);
console.log(dbridge.connectionstate.isconnected);
console.log(dbridge.connectionstate.rttms);
console.log(dbridge.connectionstate.reconnect_attempt);
print(dbridge.connectionstate.state)
print(dbridge.connectionstate.isconnected)
print(dbridge.connectionstate.rttms)
print(dbridge.connectionstate.reconnect_attempt)
Console.WriteLine(dbridge.connectionstate.state);
Console.WriteLine(dbridge.connectionstate.isconnected);
Console.WriteLine(dbridge.connectionstate.rttms);
Console.WriteLine(dbridge.connectionstate.reconnect_attempt);
Property Description
connectionstate.state (String) Current state of dataBridges network connection . List of Return Values are detailed below.
connectionstate.isconnected (Boolean) To verify if the application is still connected to the dataBridges network.
connectionstate.rttms (integer) Latency in milliseconds between your application and the dataBridges router where your application is connected.
connectionstate.reconnect_attempt (integer) Number of reconnection attempted as of now.
connectionstate.state
Return Values Description
connecting Your application is now attempting to connect to dataBridges network.
connected The connection to dataBridges network is open and authenticated with your appkey.
connection_break Indicates a network disconnection between application and dataBridges network. The library will initiate an automatic reconnection, if the reconnection property is set as true.
connect_error The dataBridges network connection was previously connected and has now errored and closed.
disconnected The application is now disconnected from the dataBridges network. The application will than need to initiate fresh connection attempt again.
reconnecting Your application is now attempting to reconnect to dataBridges network as per properties set for reconnection.
reconnect_error Reconnection attempt has errored.
reconnect_failed The application will enter reconnect_failed state when all the reconnection attempts have been exhausted unsuccessfully. The application is now disconnected from the dataBridges network. The application will than need to initiate fresh connection attempt again
reconnected The application has successfully re-connected to the dataBridges network, This state will follow connect_error or reconnect_error.

Bind to connectionstate events

Apart from retrieving state of a dBrige connection, application can bind to connectionstate events.

You can use the following methods on connectionstate object to bind to events.

dbridge.connectionstate.bind(eventName, () => {});
dbridge.connectionstate.unbind(eventName);
dbridge.connectionstate.unbind();
dbridge.connectionstate.bind(eventName, callable)
dbridge.connectionstate.unbind(eventName)
dbridge.connectionstate.unbind()
dbridge.connectionstate.bind(eventName, Action<object>);
dbridge.connectionstate.unbind(eventName);
dbridge.connectionstate.unbind();

bind() on eventName has callback functions to be defined where you can write your own code as per requirement.

To stop listening to events use unbind(eventName) function.

To stop listening to all events use unbind() [without eventName] function.

Below are library events which can be bind to receive information about dataBridges network.

System events for connectionstate object

dbridge.connectionstate.bind("connecting", () => {
    console.log('connecting');
});
dbridge.connectionstate.bind("reconnecting", () => {
    console.log('reconnecting', "reconnect_attempt=", dbridge.connectionstate.reconnect_attempt);
});
dbridge.connectionstate.bind("disconnected", () => {
    console.log('disconnected');
});
dbridge.connectionstate.bind("reconnected", () => {
    console.log('reconnected');
}); 
dbridge.connectionstate.bind("connection_break", (payload) => {
    console.log('connection_break', payload.source, payload.code, payload.message);
});
dbridge.connectionstate.bind("state_change", (payload) => {
    console.log('state_change', dbridge.connectionstate.state, payload);
});
dbridge.connectionstate.bind("connect_error", (payload) => {
    console.log('connect_error', payload.source, payload.code, payload.message); 
});
dbridge.connectionstate.bind("reconnect_error", (payload) => {
    console.log('reconnect_error', payload.source, payload.code, payload.message);
}); 
dbridge.connectionstate.bind("reconnect_failed", (payload) => {
    console.log('reconnect_failed', payload.source, payload.code, payload.message); 
});
dbridge.connectionstate.bind("rttpong", (payload) => {
    console.log('rttpong', payload, dbridge.connectionstate.rttms);
}); 
dbridge.connectionstate.bind("connected", () => { 
    console.log('connected');
});
async def connecting():
    print("connecting")

async def reconnecting():
    print("reconnecting")

async def connection_break():
    print("connection_break")

async def state_change( data):
    print("state_change:", data)

async def connect_error( data):
    if isinstance(data, str):
        print("connect_error:" + str(data))
    if isinstance(data, dBError.dBError):
        print(data.code, data.source, data.message)

async def reconnect_error(data):
    if isinstance(data, str):
        print("connect_error:" + str(data))
    if isinstance(data, dBError.dBError):
        print(data.code, data.source, data.message)

async def reconnect_failed( data):
    print("reconnect_failed:", data)

async def reconnected():
    print("reconnected:")

async def rttpong(data=None):
    print("rttpong:", .dbridge.connectionstate.rttms)

async def disconnected():
    print("disconnected:")

async def connected():
    print("connected...")

try:
      dbridge.connectionstate.bind("connecting", connecting)
      dbridge.connectionstate.bind("reconnecting", reconnecting)
      dbridge.connectionstate.bind("connection_break", connection_break)
      dbridge.connectionstate.bind("state_change", state_change)
      dbridge.connectionstate.bind("connect_error", connect_error)
      dbridge.connectionstate.bind("reconnect_error", reconnect_error)
      dbridge.connectionstate.bind("reconnect_failed", reconnect_failed)
      dbridge.connectionstate.bind("reconnected", reconnected)
      dbridge.connectionstate.bind("rttpong", rttpong)
      dbridge.connectionstate.bind("connected", connected)
      dbridge.connectionstate.bind("disconnected", disconnected)

except Exception as e:
    print(e.code, e.source, e.message)
Action<object> connecting;
dbridge.connectionstate.bind("connecting", connecting = (object payload) => {
    Console.WriteLine("connecting");
});

Action<object> reconnecting;
dbridge.connectionstate.bind("reconnecting", reconnecting = (object payload) => {
    Console.WriteLine("reconnecting, reconnect_attempt={0}", dbridge.connectionstate.reconnect_attempt);
});

Action<object> disconnected;
dbridge.connectionstate.bind("disconnected", disconnected = (object payload) => {
    Console.WriteLine("disconnected");
});

Action<object> reconnected;
dbridge.connectionstate.bind("reconnected", reconnected = (object payload) => {
   Console.WriteLine("reconnected");
}); 

Action<object> disconnected;
dbridge.connectionstate.bind("connection_break", reconnected = (object payload) => {
    dBError Payload =  payload as dBError
    Console.WriteLine("connection_break {0},{1},{2}", Payload.source, Payload.code, Payload.message);
});

Action<object> state_change;
dbridge.connectionstate.bind("state_change", state_change = (object payload) => {
    stateChange statechange = payload as stateChange;
    Console.WriteLine("state_change {0} , {1}", dbridge.connectionstate.state, payload);
});

Action<object> connect_error;
dbridge.connectionstate.bind("connect_error", connect_error = (object payload) => {
    dBError Payload =  payload as dBError
    Console.WriteLine("connect_error,  {0},{1},{2}", Payload.source, Payload.code, Payload.message); 
});

Action<object> reconnect_error;
dbridge.connectionstate.bind("reconnect_error", reconnect_error = (object payload) => {
    dBError Payload =  payload as dBError
    Console.WriteLine("reconnect_error {0},{1},{2}", Payload.source, Payload.code, Payload.message); 
});

Action<object> reconnect_failed;
dbridge.connectionstate.bind("reconnect_failed", reconnect_failed = (object payload) => {
    dBError Payload =  payload as dBError
    Console.WriteLine("reconnect_failed{0},{1},{2}", Payload.source, Payload.code, Payload.message); 
});

Action<object> rttpong;
dbridge.connectionstate.bind("rttpong", rttpong = (object payload) => {
   Console.WriteLine("rttpong {0} , {1}", payload, dbridge.connectionstate.rttms);
}); 

Action<object> connected;
dbridge.connectionstate.bind("connected", connected = (object payload) => {
    Console.WriteLine("dBridge Event Bind {0}", "connected");
});
payload: (dberror object)
{
    "source": "DBLIB_CONNECT" ,             // (string) Error source
    "code": "RECONNECT_ATTEMPT_EXCEEDED",   // (string) Error code 
    "message": ""                           // (string) Error message if applicable.
}
Events Parameters Description
connecting This event is triggered when your application is attempting to connect to dataBridges network.
connected This event is triggered when connection to dataBridges network is open and authenticated with your appkey.
connection_break payload (dberror object) Indicates a network disconnection between application and dataBridges network. The library will initiate an automatic reconnection, if the reconnection property is set as true.
connect_error payload (dberror object) This event is triggered when the dataBridges network connection was previously connected and has now errored and closed.
disconnected The application is now disconnected from the dataBridges network. The application will than need to initiate fresh connection attempt again.
reconnecting This event is triggered when application is now attempting to reconnect to dataBridges network as per properties set for reconnection.
reconnect_error payload (dberror object) This event is triggered when reconnection attempt has errored.
reconnect_failed payload (dberror object) reconnect_failed event is triggered when all the reconnection attempts have been exhausted unsuccessfully. The application is now disconnected from the dataBridges network. The application will than need to initiate fresh connection attempt again.
reconnected This event is triggered when the connection to dataBridges network is open and reconnected after connect_error or reconnect_error.
state_change payload with payload.previous, payload.current (dict) This event is triggered whenever there is any state changes in dataBridges network connection. Payload will have previous and current state of connection.
rttpong payload (integer) In Response to rttping() function call to dataBridges network, payload has latency in milliseconds between your application and the dataBridges router where your application is connected.

dberror:

Source Code Message Description
DBLIB_CONNECT RECONNECT_ATTEMPT_EXCEEDED Triggered when reconnect_failed event is raised.
DBNET_CONNECT DISCONNECT_REQUEST Triggered when connect_error event is raised.
DBNET_CONNECT RECONNECT_REQUEST Triggered when connect_error, connection_break event is raised.
DBLIB_CONNECT NETWORK_DISCONNECTED Triggered when connect_error, reconnect_error event is raised.
DBLIB_CONNECT INVALID_URL Value of dbridge.auth_url is not a valid dataBridges authentication URL.
DBLIB_CONNECT INVALID_AUTH_PARAM Value of dbridge.appkey or dbridge.appsecret is not a valid dataBridges application key.
DBLIB_CONNECT ACCESSTOKEN_FAILED dbridge.appsecret validation failed.
DBLIB_CONNECT HTTP_ HTTP protocol reported message. HTTP Errors returned during authentication process. HTTP Error code will be concatenated with HTTP_ in the err.code. eg. HTTP_501
DBLIB_CONNECT INVALID_CLIENTFUNCTION If "callback function" is not declared for client function or typeof() variable defined is not a "function". (Check Client Function section for details.)

Exceptions:

Source Code Description
DBLIB_CONNECT_BIND INVALID_EVENTNAME Invalid Event name. Not in defined events as above.
DBLIB_CONNECT_BIND INVALID_CALLBACK If "callback function" is not declared or typeof() variable defined is not a "function".

Functions

rttping()

This method is to understand the latency in milliseconds between your application and the dataBridges router where your application is connected. Event rttpong is triggered once response is received from dataBridges network. Bind to event:rttpong to retrieve the latency in ms.

// To get the last known Latency in milliseconds between your application and the dBridges router where your application is connected.
// The dBridges library exchanges rttms during the initial dBridges network connection routine.
console.log(dbridge.connectionstate.rttms);

// To get the latest Latency in milliseconds between your application and the dBridges router where your application is connected.
dbridge.connectionstate.rttping();

// Bind to rttpong, to get notified about the latest Latency in milliseconds between your application and the dBridges router where your application is connected.
dbridge.connectionstate.bind("rttpong", (payload) => {
    console.log('rttpong', payload);
    //payload is an integer value is in millisecond which is same as dbridge.connectionstate.rttms.
});
# To get the last known Latency in milliseconds between your application and the dBridges router where your application is connected.
# The dBridges library exchanges rttms during the initial dBridges network connection routine.
print(dbridge.connectionstate.rttms);

# To get the latest Latency in milliseconds between your application and the dBridges router where your application is connected.
try:
    dbridge.connectionstate.rttping();
    expect Exception as err:
        Console.WriteLine("{0} ,  {1} , {2}" , err.source, err.code, err.message);

# Bind to rttpong, to get notified about the latest Latency in milliseconds between your application and the dBridges router where your application is connected.

def rttpong(payload):
    Console.WriteLine(payload);
    //payload is an integer value is in millisecond which is same as dbridge.connectionstate.rttms.

try:
    dbridge.connectionstate.bind("rttpong",rttpong);
    expect Exception as err:
    Console.WriteLine("{0} ,  {1} , {2}" , err.source, err.code, err.message);
// To get the last known Latency in milliseconds between your application and the dBridges router where your application is connected.
// The dBridges library exchanges rttms during the initial dBridges network connection routine.
Console.WriteLine(dbridge.connectionstate.rttms);

// To get the latest Latency in milliseconds between your application and the dBridges router where your application is connected.
try {
    dbridge.connectionstate.rttping();
} catch (dBError err){
    Console.WriteLine("{0} ,  {1} , {2}" , err.source, err.code, err.message);
}
// Bind to rttpong, to get notified about the latest Latency in milliseconds between your application and the dBridges router where your application is connected.

void rttpong(object payload) {
    Console.WriteLine(payload);
    //payload is an integer value is in millisecond which is same as dbridge.connectionstate.rttms.
}
try {
    Action<object> rttpong_callback = rttpong;
    dbridge.connectionstate.bind("rttpong",rttpong_callback);
} catch (dBError err){
    Console.WriteLine("{0} ,  {1} , {2}" , err.source, err.code, err.message);
}

Exceptions:

Source Code Description
DBLIB_RTTPING NETWORK_DISCONNECTED Connection to dataBridges network is not active.