Pub / Sub app

Code a Pub/Sub app to publish ‘greetings ’ message. The app will also receive the greetings message from other app instances. We can run 100's of these pub/sub app instances. Each of them will send a single greeting message and will receive greetings from all the other app instances.

Before trying this sample, follow the Setup and initialization . For more information, see the Channels Pub/Sub API reference documentation.

Download source

Download the pub.sub.app by clicking on this link

Dependencies

For this application, you will require to use databridges-sio-client-lib (dataBridges Client Library) with Node.js.

  • Install databridges client library
  • npm install databridges-sio-client-lib --save

Download the package.json by clicking on this link

Create pub.sub.app.js file and open it in your favorite editor.

Copy paste below code and you are ready to run this example.

/* 
    Code a Pub/Sub app to publish 'greetings ' message. The app will also receive the greetings message from other app instances. 
    We can run 100's of these pub/sub app instances. Each of them will send a single greeting message and will receive greetings from all the other app instances. 
*/

// Include dataBridges client library package
const dBridges = require('databridges-sio-client-lib');

// Initialize dataBridges client
const dbridge = new dBridges(); 

// Replace your application key below, which you have received from dataBridges management portal.
// use environmental variables for greater security of your keys
dbridge.appkey = '____appKey______';

// Replace your authentication url below, which you have received from dataBridges management portal.
// example https://endpoint01.databridges.io/client/v1/authenticate
// use environmental variables for greater security of your keys
dbridge.auth_url = '_____URL_____';

// lets create a sessionID
const sessionID = Math.random().toString(36).substring(2, 7);
console.log('sessionID is ' + sessionID);

// Connect to dataBridges. If any runtime error it will be caught in catch().
dbridge.connect().catch((err) => {
    console.log('dataBridges Connection exception..', err.source, err.code, err.message);
});

//Bind to disconnected event, to get intimation about dataBridges network disconnection.
dbridge.connectionstate.bind("disconnected", () => {
    console.log('Disconnected from dataBridges real-time network');
});

dbridge.connectionstate.bind("connected", () => {
    console.log('Connected to dataBridges real-time network !!!');
    let helloWorld = null;
    try {
        // Subscribe to channel 'Hello-World',
        helloWorld = dbridge.channel.subscribe('Hello-World')
    } catch (err) {
        // Subscribe errors will be handled here.
        console.log('Channel subscription faced an exception: ', err.source, err.code, err.message);
    }

    // We will bind to subscription success event. On success, we will publish event messages.
    helloWorld.bind("dbridges:subscribe.success", (payload, metadata) => {
        // we can also check if the helloWorld subscription isOnline.
        if (helloWorld.isOnline()) {
            try {
                console.log('Publishing greetings ');
                helloWorld.publish('greetings', 'Greetings from ' + sessionID);

            } catch (err) {
                // Any publish error will be caught here.
                console.log('Publishing messages faced an exception:  ', err.source, err.code, err.message);
            }
        }
    });

    //Bind to the channel.event that you want to process. Here we will bind to 'greetings'
    helloWorld.bind('greetings', (payload) => {
        console.log('received greeting message : ', payload);
    });

    // Bind to Subscription failure event, to get notified if subscription to channel has been rejected by dataBridges network
    helloWorld.bind("dbridges:subscribe.fail", (payload, metadata) => {
        console.log('Channel subscription faced an exception:', payload.source, payload.code, payload.message);
    });

});

Save your file and run node pub.sub.app.js from the same folder where you have saved package.json.