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-js-lib (dataBridges Client Library) and Socket.io client with javascript.

The dependencies are already embeded in the tag with skeleton file which is referred in "Setup and initialization" section.

Create pub.sub.app.html file using the skeleton and open it in your favorite editor.

Copy paste below code inside $(document).ready() jquery function and you are ready to run this example.

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

// Replace your application key below, which you have received from dataBridges management portal.
// use session 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 session variables for greater security of your keys
dbridge.auth_url = '_____URL_____';

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

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

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

dbridge.connectionstate.bind("connected", () => {
    logit(['Connected to dataBridges real-time network !!!']);
    let helloWorld = null;
    try {
        // Subscribe to channel 'Hello-World',
        helloWorld = dbridge.channel.subscribe('Hello-World')
    } catch (err) {
        // Connect errors will be handled here.
        logit(['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 {
                logit(['Publishing greetings ']);
                helloWorld.publish('greetings', 'Greetings from ' + sessionID);

            } catch (err) {
                // Any publish error will be caught here.
                logit(['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) => {
        logit(['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) => {
        logit(['Channel subscription faced an exception:', payload.source, payload.code, payload.message]);
    });

});

Save your file and double click if you want to test using file system. If you want to host this file in any of your preferred web server, copy the file into the root folder and open it in browser to see the output.