To Infinity and Beyond

Once we’ve created our channel id and we are familiarized with our hello world example, we are ready to start sending messages of any kind.

Anyway remember that messages have string format so we can send serialized objects in XML/JSON formats as well as a plain text.

The following example will send a fixed message every 10sec.:

using Abetoo;
using System;
using System.Threading;

namespace abetooTest
{
 class Program
 {
 static void Main(string[] args)
 {
 // Connection parameters
 const String myChannel =
 "-TbVO-rqdA0iWg6-gWh0eeQ636243460528994051#" +
 "735acf9cd6eda96e66ee3858496dca59d750aff1";
 
 const int heartBeat = 4;

 // Connection
 AbtooConnection abconnection = new AbtooConnection(myChannel, heartBeat);
 abconnection.connect();

 // Messages Listener
 abconnection.messageReceived += ((sender, abtoomsg) =>
 {
 AbtooMessage abmsg = (AbtooMessage)abtoomsg;
 Console.WriteLine("Message received:" + abmsg.body);
 });

 // Send message every 10 sec.
 const String remoteChannel =
 "-uC6s1LfZaEuw9AK0Js1w9w636243459698606497#" +
 "b90a9186b9b61ead17d51251f689aea1779d5203";
 String message = "Hello from this side";
 String correlationId = "id1";

 while (true)
 {
 if (abconnection.sendMessage(remoteChannel, message, correlationId))
 Console.WriteLine("Message sent!");
 else
 Console.WriteLine("Message not sent!");
 Thread.Sleep(10000);
 }
 }
 }
}

The only difference from the hello world code is the introduction of a loop where the sending messages process take place:

// Send message every 10 sec.
 const String remoteChannel =
 "-uC6s1LfZaEuw9AK0Js1w9w636243459698606497#" +
 "b90a9186b9b61ead17d51251f689aea1779d5203";
 String message = "Hello from this side";
 String correlationId = "id1";

 while (true)
 {
 if (abconnection.sendMessage(remoteChannel, message, correlationId))
 Console.WriteLine("Message sent!");
 else
 Console.WriteLine("Message not sent!");
 Thread.Sleep(10000);
 }

There are 3 new parameters , the channel target, the body message itself and a correlation id. The meaning of this parameters is very straightforward. The channel target is the channel id used for the target device we want to send the message. The body message, as described before, could be any XML/JSON string, in this particular case, is a simple plain text. Lastly the correlation id is used to keep track about the messages sent/received. If you want to use a challenge/response approach you can associate the challenge sent message with a correlation id, once a message is received you can use its correlation id to identify the associated challenge message.

Correlation id is optional, mostly used only when you are using a challenge/response approach like if you are using a regular webservice.

If you launch this code you’ll see something like that:

Why are you getting this message for the very first message sent?

The answer is quite simple, once you launch the connection process with the line,

AbtooConnection abconnection = new AbtooConnection(myChannel, heartBeat);
abconnection.connect();

The connection doesn’t take place instantly. It takes a few seconds to establish the connection with the remote server.

If you want to check if the connection is established before you start to send any message you can insert this line,

while (!abconnection.isConnected()) ;

This way our final example looks like this:

using Abetoo;
using System;
using System.Threading;

namespace abetooTest
{
 class Program
 {
 static void Main(string[] args)
 {
 // Connection parameters
 const String myChannel =
 "-TbVO-rqdA0iWg6-gWh0eeQ636243460528994051#" +
 "735acf9cd6eda96e66ee3858496dca59d750aff1";
 
 const int heartBeat = 4;

 // Connection
 AbtooConnection abconnection = new AbtooConnection(myChannel, heartBeat);
 abconnection.connect();

 // Messages Listener
 abconnection.messageReceived += ((sender, abtoomsg) =>
 {
 AbtooMessage abmsg = (AbtooMessage)abtoomsg;
 Console.WriteLine("Message received:" + abmsg.body);
 });

 while (!abconnection.isConnected()) ;

 // Send message every 10 sec.
 const String remoteChannel =
 "-uC6s1LfZaEuw9AK0Js1w9w636243459698606497#" +
 "b90a9186b9b61ead17d51251f689aea1779d5203";
 String message = "Hello from this side";
 String correlationId = "id1";

 while (true)
 {
 if (abconnection.sendMessage(remoteChannel, message, correlationId))
 Console.WriteLine("Message sent!");
 else
 Console.WriteLine("Message not sent!");
 Thread.Sleep(10000);
 }
 }
 }
}

… and now if we launch our example we’ll see  the following console window:

But there is no much fun if there is no one to talk to.

Let’s add another abetoo connection in the same thread:

using Abetoo;
using System;
using System.Threading;

namespace abetooTest
{
 class Program
 {
 static void Main(string[] args)
 {
 // Connection parameters
 const String myChannel1 =
 "-TbVO-rqdA0iWg6-gWh0eeQ636243460528994051#" +
 "735acf9cd6eda96e66ee3858496dca59d750aff1";

 const int heartBeat = 4;

 // Connection
 AbtooConnection abconnection1 = new AbtooConnection(myChannel1, heartBeat);
 abconnection1.connect();

 // Messages Listener
 abconnection1.messageReceived += ((sender, abtoomsg) =>
 {
 AbtooMessage abmsg = (AbtooMessage)abtoomsg;
 Console.WriteLine("Abetoo 1 message received:" + abmsg.body);
 });

 while (!abconnection1.isConnected()) ;

 const String myChannel2 =
 "-uC6s1LfZaEuw9AK0Js1w9w636243459698606497#" +
 "b90a9186b9b61ead17d51251f689aea1779d5203";

 // Connection
 AbtooConnection abconnection2 = new AbtooConnection(myChannel2, heartBeat);
 abconnection2.connect();

 // Messages Listener
 abconnection2.messageReceived += ((sender, abtoomsg) =>
 {
 AbtooMessage abmsg = (AbtooMessage)abtoomsg;
 Console.WriteLine("Abetoo 2 message received:" + abmsg.body);
 });

 while (!abconnection2.isConnected()) ;

 String message1 = "This message was sent from abetoo connection 1!";
 String message2 = "This message was sent from abetoo connection 2!";

 // Send message to each other every 10 sec.
 while (true)
 {
 if (abconnection1.sendMessage(myChannel2, message1))
 Console.WriteLine("Message abetoo 1 sent!");
 else
 Console.WriteLine("Message abetoo 1 not sent!");

 Thread.Sleep(2000);

 if (abconnection2.sendMessage(myChannel1, message2))
 Console.WriteLine("Message abetoo 2 sent");
 else
 Console.WriteLine("Message abetoo 2 not sent!");

 Thread.Sleep(10000);

 }
 }
 }
}

And launching this code we get the following result:

Finally to give an example of using the library between two remote applications, I’ll show you a very basic chat application with only two users. Add more functionality or more users is very straightforward.

We’re going to get the following applications:

Let’s go with the source code of the User1 Application,

 AbtooConnection abconnection;

 public form1()
 {
    InitializeComponent();

    // Connection parameters
    const String myChannel =
       "-TbVO-rqdA0iWg6-gWh0eeQ636243460528994051#" +
       "735acf9cd6eda96e66ee3858496dca59d750aff1";

    const int heartBeat = 4;

    // Connection
    abconnection = new AbtooConnection(myChannel, heartBeat);

    // Messages Listener
    abconnection.messageReceived += ((sender, abtoomsg) =>
    {
       AbtooMessage abmsg = (AbtooMessage)abtoomsg;
       this.Invoke((MethodInvoker)(() =>
       {
          listBox1.Items.Add("User2: " + abmsg.body);
          listBox1.SelectedIndex = listBox1.Items.Count - 1;
          listBox1.SelectedIndex = -1;
          listBox1.Refresh();
       }));
   });
   abconnection.connect();
 }

 private void button1_Click(object sender, EventArgs e)
 {
    const String remoteChannel =
       "-uC6s1LfZaEuw9AK0Js1w9w636243459698606497#" +
       "b90a9186b9b61ead17d51251f689aea1779d5203";

    abconnection.sendMessage(remoteChannel, messageBox.Text);
    listBox1.Items.Add("Me: " + messageBox.Text);
    listBox1.SelectedIndex = listBox1.Items.Count - 1;
    listBox1.SelectedIndex = -1;
    listBox1.Refresh();
    messageBox.Text = "";
 }

Let’s go with the source code of the User2 Application,

 AbtooConnection abconnection;

 public form1()
 {
    InitializeComponent();

    // Connection parameters
    const String myChannel =
       "-uC6s1LfZaEuw9AK0Js1w9w636243459698606497#" +
       "b90a9186b9b61ead17d51251f689aea1779d5203";

    const int heartBeat = 4;

    // Connection
    abconnection = new AbtooConnection(myChannel, heartBeat);

    // Messages Listener
    abconnection.messageReceived += ((sender, abtoomsg) =>
    {
       AbtooMessage abmsg = (AbtooMessage)abtoomsg;

       this.Invoke((MethodInvoker)(() =>
       {
          listBox1.Items.Add("User1: " + abmsg.body);
          listBox1.SelectedIndex = listBox1.Items.Count - 1;
          listBox1.SelectedIndex = -1;
          listBox1.Refresh();
       }));
    });
    abconnection.connect();
 }

 private void button1_Click(object sender, EventArgs e)
 {
    const String remoteChannel =
       "-TbVO-rqdA0iWg6-gWh0eeQ636243460528994051#" +
       "735acf9cd6eda96e66ee3858496dca59d750aff1"; 
    abconnection.sendMessage(remoteChannel, messageBox.Text);
    listBox1.Items.Add("Me: " + messageBox.Text);
    listBox1.SelectedIndex = listBox1.Items.Count - 1;
    listBox1.SelectedIndex = -1;
    listBox1.Refresh();
    messageBox.Text = "";
 }

As you can see the only difference is we have to invert the channels. (myChannel/RemoteChannel)

Let’s go deeper into the source code,

Within the initialization method form() it’s defined the abetoo connection with the private channel and the heartbeat parameter, (in this case User2 code is shown)

    ....
    // Connection parameters
    const String myChannel =
       "-uC6s1LfZaEuw9AK0Js1w9w636243459698606497#" +
       "b90a9186b9b61ead17d51251f689aea1779d5203";

    const int heartBeat = 4;

    // Connection
    abconnection = new AbtooConnection(myChannel, heartBeat);

    ....

Then it’s assigned a listener for incoming messages, to show them,

     ....

    abconnection.messageReceived += ((sender, abtoomsg) =>
    {
       AbtooMessage abmsg = (AbtooMessage)abtoomsg;

       this.Invoke((MethodInvoker)(() =>
       {
          listBox1.Items.Add("User1: " + abmsg.body);
          listBox1.SelectedIndex = listBox1.Items.Count - 1;
          listBox1.SelectedIndex = -1;
          listBox1.Refresh();
       }));
    });
    
    ....

Finally the connection is launched,

   ....
   abconnection.connect();
   ....

On the other hand, There’s a function to catch click event on button to send new messages,

   ....
   const String remoteChannel =
      "-TbVO-rqdA0iWg6-gWh0eeQ636243460528994051#" +
      "735acf9cd6eda96e66ee3858496dca59d750aff1"; 

    abconnection.sendMessage(remoteChannel, messageBox.Text);
    listBox1.Items.Add("Me: " + messageBox.Text);
    listBox1.SelectedIndex = listBox1.Items.Count - 1;
    listBox1.SelectedIndex = -1;
    listBox1.Refresh();
    messageBox.Text = "";
    ....

Whenever the send button is pressed a new message is sent to the remoteChannel.

You can download the full source code of this two applications here.

 

Was this article helpful?

Related Articles

6 Comments

  1. FS

    Could you please have a section explaining the steps to setup a chat connection between two different people in two different locations ?
    For instance, should each person get a unique Channel ID ?
    Should each of them compile the application as in your Sending Message exampleusing the two IDs in different orders like:
    person 1: myChannel=id_1, remoteChannel=id_2
    person_2: myChannel=id_2, remoteChannel = id_1 ?

    1. Thanks for comment. I’ve added to the article an example of a modified chat version to communicate two remote computers. You’re right every device has to have a channel id, this is the only way to give a warranty that this channel is unique. On the other hand if you want to communicate the devices with each other you simply have to invert the channels order, as you posted.

  2. FS

    Thanks for the update.
    I tried running both chat apps on the same machine and they do not talk to each other; the message from each window is only displayed in that window (not in the other).
    Any suggestions ?

Leave A Comment?