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

We need to install the io.databridges:databridges_sio_java_client dependencies for our app. Open your app-module build.gradle file and add these:

// File: ./app/build.gradle
dependencies {
    // other dependencies...  Use Latest Version 
    implementation 'io.databridges:databridges_sio_java_client:2.0.2'
}
After adding the dependencies, sync your Gradle files so that the dependencies are imported.

Create a class inside app/java/com.example.pubsubapp and name it as pub_sub_app.

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

package example.com.pubsubapp;

/* 
    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. 
*/


import android.util.Log;

import com.google.gson.JsonDeserializationContext;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.HashMap;
import java.util.Map;

// Include dataBridges client library package

import io.databridges.databridges_sio_java_client.dBridges;
import io.databridges.databridges_sio_java_client.callbacks.connectionHandler;
import io.databridges.databridges_sio_java_client.exception.dBError;
import io.databridges.databridges_sio_java_client.channel.channel;
import io.databridges.databridges_sio_java_client.callbacks.eventHandler;
import io.databridges.databridges_sio_java_client.utils.metaData;


public class pub_sub_app {   
    dBridges dbridge;
    channel helloWorld;
    String sessionID;

    public  String random(int maxlength) {
        Random generator = new Random();
        StringBuilder randomStringBuilder = new StringBuilder();

        char tempChar;
        for (int i = 0; i < maxlength; i++){
            tempChar = (char) (generator.nextInt(96) + 32);
            randomStringBuilder.append(tempChar);
        }
        return randomStringBuilder.toString();
    }

    public void Execute()
    {
        // Initialize dataBridges client
        dbridge = new dBridges();

        // Replace your application key below, which you have received from dataBridges management portal
        dbridge.appkey = "____appKey______";

        // Replace your authentication url below, which you have received from dataBridges management portal.
        // example https://endpoint01.databridges.io/client/v1/authenticate
        dbridge.auth_url = "_____URL_____";

        // lets create a sessionID
        sessionID =  random(7);
        Log.i("event:" , "sessionID is " + sessionID);

        //Bind to disconnected event, to get intimation about dataBridges network disconnection.
        try {
            dbridge.connectionstate.bind("disconnected", new connectionHandler() {
                @Override
                public void onEvent(Object message) {
                    Log.i("event:" , "disconnected from dataBridges real-time network" );
                }
            });
        }catch (dBError dberr){
            String exception = "Binding to Disconnected event faced an exception : " + dberr.source + " " + dberr.code + "  " +  dberr.message;
            Log.i("exception: " , exception );
        }

        try {
            dbridge.connectionstate.bind("connected", new connectionHandler() {
                @Override
                public void onEvent(Object message) {
                    Log.i("event:" , "Connected to dataBridges real-time network !!!" );

                    try{
                        // Subscribe to channel 'Hello-World',
                        helloWorld =  dbridge.channel.subscribe("Hello-World");
                    }catch (dBError dberr){
                        // Connect errors will be handled here.
                        String exception = "Channel subscription faced an exception:" + dberr.source + " " + dberr.code + "  " +  dberr.message;
                        Log.i("exception: " , exception );
                    }
                    if (helloWorld != null) {
                        // We will bind to subscription success event. On success, we will publish event messages.
                        try {
                            helloWorld.bind("dbridges:subscribe.success", new eventHandler() {
                                @Override
                                public void onEvent(Object message, metaData metadata) {
                                    // we can also check if the helloWorld subscription isOnline.
                                    if(helloWorld.isOnline()){
                                        try {
                                            Log.i("event:" ,  "Publishing greetings ");
                                            helloWorld.publish("greetings", "Greetings from " + sessionID);  
                                        } catch (dBError dberr) {
                                            // Any publish error will be caught here.
                                            String exception = "Publishing messages faced an exception:  " + dberr.source + " " + dberr.code + "  " +  dberr.message;
                                            Log.i("exception: ", exception);
                                        }
                                    }         
                                }
                            });
                        } catch (dBError dberr) {
                            String exception = "Binding to Hello-World Subscribe Success event faced an exception :" + dberr.source + " " + dberr.code + "  " +  dberr.message;
                            Log.i("exception: ", exception);
                        }


                        //Bind to the channel.event that you want to process. Here we will bind to 'greetings'
                        try {
                            helloWorld.bind("greetings", new eventHandler() {
                                @Override
                                public void onEvent(Object message, metaData metadata) {
                                    Log.i("event:" ,"received greeting message : " + (String) message);
                                }
                            });
                        } catch (dBError dberr) {
                            String exception = "Binding to Hello-World greetings event faced an exception :" + dberr.source + " " + dberr.code + "  " +  dberr.message;
                            Log.i("exception: ", exception);
                        }


                        // Bind to Subscription failure event, to get notified if subscription to channel has been rejected by dataBridges network
                        try {
                            helloWorld.bind("dbridges:subscribe.fail", new eventHandler() {
                                @Override
                                public void onEvent(Object message, metaData metadata) {
                                    if (message instanceof dBError) {
                                        dBError dberr = (dBError) message;
                                        String data = "Channel subscription faced an exception: " + dberr.source + " " + dberr.code + "  " +  dberr.message;
                                        Log.i("exception: ", data);
                                    }
                                }
                            });
                        } catch (dBError dberr) {
                            String exception = "Binding to Hello-World Subscribe Failed event faced an exception :" + dberr.source + " " + dberr.code + "  " +  dberr.message;
                            Log.i("exception: ", exception);
                        }
                    }
                }

            });
        }catch (dBError dberr){
            String exception = "Binding to dataBridges Connected event faced an exception :" + dberr.source + " " + dberr.code + "  " +  dberr.message;
            Log.i("exception: " , exception );
        } 

        try {
            // Connect to dataBridges. If any runtime error it will be caught in catch().
            dbridge.connect();
        }catch (dBError dberr){
            String exception = "dataBridges Connection exception.." + dberr.source + " " + dberr.code + "  " +  dberr.message;
            Log.i("exception: " , exception );
        }

    }

}

Save your file and build your project in Release or Debug mode to test.