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).

  • Install dataBridges client library from "Manage NuGet Packages..." from the Solution -> Application Name context menu.

Rename Program.cs to pub_sub_app.cs.

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. 
*/
using System;
using System.Threading.Tasks;
using dBridges.exceptions;
using dBridges.channel;
using System.Text;
namespace pub_sub_app
{
    class Cpub_sub_app
    {
        public dBridges.dBridges dbridge;
        public string sessionID;
        public string RandomString(int size, bool lowerCase = false)
        {
            var builder = new StringBuilder(size);
            Random _random = new Random();
            char offset = lowerCase ? 'a' : 'A';
            const int lettersOffset = 26;
            for (var i = 0; i < size; i++)
            {
                var @char = (char)_random.Next(offset, offset + lettersOffset);
                builder.Append(@char);
            }
            return lowerCase ? builder.ToString().ToLower() : builder.ToString();
        }
        public async Task init_dataBridges()
        {
            // Initialize dataBridges client
            this.dbridge = new dBridges.dBridges();

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

            // Replace your authentication url below, which you have received from dataBridges management portal.
            // example https://endpoint01.databridges.io/client/v1/authenticate
            this.dbridge.auth_url = "_____URL_____";
            // lets create a sessionID
            this.sessionID = RandomString(36).Substring(2, 7);
            Console.WriteLine("sessionID is " + this.sessionID);

            //Bind to disconnected event, to get intimation about dataBridges network disconnection. 
            Action<object> a_disconnect;
            this.dbridge.connectionstate.bind("disconnected", a_disconnect = (object eventinfo) =>
            {
                Console.WriteLine("Disconnected from dataBridges real-time network");
            });

            Action<object> a_connect;
            this.dbridge.connectionstate.bind("connected", a_connect = async (object eventinfo) =>
            {
                Console.WriteLine("Connected to dataBridges real-time network !!!");
                dBridges.channel.channel helloWorld = null;
                try
                {
                    // Subscribe to channel 'Hello-World',
                    helloWorld = await this.dbridge.channel.subscribe("Hello-World");
                }
                catch (dBError dberror)
                {
                    // Subscription errors will be handled here.
                    Console.WriteLine("Channel subscription faced an exception: {0} , {1} , {2}", dberror.source, dberror.code, dberror.message);
                }
                if (helloWorld != null)
                {
                    // We will bind to subscription success event. On success, we will publish event messages.
                    Action<object, object> a_subscribesuccess;
                    helloWorld.bind("dbridges:subscribe.success", a_subscribesuccess = (object payload, object metadata) =>
                    {
                        // we can also check if the helloWorld subscription isOnline.
                        if (helloWorld.isOnline())
                        {
                            try
                            {
                                Console.WriteLine("Publishing greetings ");
                                helloWorld.publish("greetings", "Greetings from " + sessionID);
                            }
                            catch (dBError dberror)
                            {
                                // Any publish error will be caught here.
                                Console.WriteLine("Publishing messages faced an exception:  {0} , {1} , {2}", dberror.source, dberror.code, dberror.message);
                            }
                        }
                    });

                    //Bind to the channel.event that you want to process. Here we will bind to 'greetings'
                    Action<object, object> a_greeting;
                    helloWorld.bind("greetings", a_greeting = (object payload, object metadata) =>
                    {
                        Console.WriteLine("received greeting message : {0}", payload);
                    });

                    // Bind to Subscription failure event, to get notified if subscription to channel has been rejected by dataBridges network
                    Action<object, object> a_subscribefail;
                    helloWorld.bind("dbridges:subscribe.fail", a_subscribefail = (object payload, object metadata) =>
                    {
                        dBError dberror = payload as dBError;
                        Console.WriteLine("Channel subscription faced an exception:{0} , {1} ,  {2}", dberror.source, dberror.code, dberror.message);
                    });
                }
            });
            // Connect to dataBridges. If any runtime error it will be caught in catch().
            try
            {
                await this.dbridge.connect();
            }
            catch (dBError dberror)
            {
                Console.WriteLine("dataBridges Connection exception.. {0} , {1} , {2}", dberror.source, dberror.code, dberror.message);
            }
            string ch = Console.ReadLine();
        }
    }
    class Program
    {
        static void Main(string[] args)
        {
            Cpub_sub_app db = new Cpub_sub_app();
            Task.Run(db.init_dataBridges).Wait();
        }
    }
}

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