Categories

Archives

Cross Platform Mobile Development using Real Time Messaging

Cross platform application connectivity is to be a frequently requested and talked about feature. A lot of enterprise and consumer-facing applications require  robust data exchange mechanism between application instances running on mobile devices. On top of this, many applications depend on real-time updates and notifications pushed from the server to the clients. One of the approaches for cross platform data exchange is a feature called Remote Shared Objects. The video below provides an explanation of the feature as well as a demonstration of iPhone, iPad and Windows Mobile phone communicating with each other as well as a browser-based Flex application.

The demo was built using the latest version of WebORB for .NET (the Java version would work just as well), Communication Library for iOS and a client library for Windows Mobile included into the WebORB distribution. Enjoy!

Flex to .NET Chat Client Using Remote Shared Objects

Integrating .NET applications with other client technologies (or even .NET to .NET) may not seem to be a trivial task. I believe Microsoft has done a great job providing a fairly closed technology stack where things work as long as you stick to Microsoft’s prescription of how to go about solving problems. Take for example the task of creating a multi client messaging system between a .NET application and Flex, AIR, Android or iOS client and you will shortly feel the pain. The real challenge is the .NET messaging framework is not very open to accomodate client applications of other than native .NET or ASP.NET. As a result, you will need a messaging server which will serve the purpose of routing broker and messaging “translator” between heterogeneous client applications.

Earlier this month I wrote about Remote Shared Objects and the benefits the feature provides. Since remote shared objects are no longer a feature exclusive to Flash, Flex and AIR clients I put together a messaging example demonstrating how .NET applications can efficiently communicate with Flex and AIR  (and thus Android,  iOS and Blackberry Playbook). The .NET application is a simple (console-based) chat program which uses remote shared objects to exchange data with a Flex client. The Remote Shared Object API and RTMP connectivity comes with the RTMPClient class available in WebORB for .NET. Here’s the full listing of the C# code with an explanation right after the listing:

<br />
using System;<br />
using System.Collections;<br />
using System.Collections.Generic;<br />
using System.Text;<br />
using System.Threading;<br />
using Weborb.Client;<br />
using Weborb.Messaging.Net.RTMP;<br />
using Weborb.Messaging.Api.Service;<br />
using Weborb.Messaging.Api.SO;</p>
<p>namespace WeborbClientTest<br />
{<br />
  class Program : IPendingServiceCallback, ISharedObjectListener<br />
  {<br />
    private ThreadStart threadStart;<br />
    private Thread chatThread;<br />
    private AutoResetEvent rsoIsReady = new AutoResetEvent( false );</p>
<p>    private RTMPClient rtmpClient;<br />
    private IClientSharedObject rso;</p>
<p>    static void Main( string[] args )<br />
    {<br />
      Program chatClient = new Program();<br />
      chatClient.StartChat();<br />
    }</p>
<p>    public void StartChat()<br />
    {<br />
      rtmpClient = new RTMPClient();<br />
      rtmpClient.setServiceProvider( this );</p>
<p>      System.Console.WriteLine( &quot;Connecting to the RTMP server running at localhost:2037&quot; );<br />
      rtmpClient.connect( &quot;localhost&quot;, 2037, &quot;Chat&quot;, null, this );</p>
<p>      threadStart = new ThreadStart( MainChat );<br />
      chatThread = new Thread( threadStart );<br />
      chatThread.Start();<br />
      chatThread.IsBackground = false;<br />
    }</p>
<p>    public void MainChat()<br />
    {<br />
      rsoIsReady.WaitOne();</p>
<p>      System.Console.WriteLine( &quot;Connecting to the 'ChatSO' remote shared object&quot; );<br />
      rso = rtmpClient.getSharedObject( &quot;ChatSO&quot;, false );<br />
      rso.addSharedObjectListener( this );</p>
<p>      while( true )<br />
      {<br />
        System.Console.WriteLine( &quot;Enter a message to put into RSO. Press \&quot;Enter\&quot; to stop the loop:&quot; );<br />
        String messageText = System.Console.ReadLine();</p>
<p>        if( messageText.Equals( &quot;&quot; ) )<br />
          break;</p>
<p>        Hashtable messageObject = new Hashtable();<br />
        messageObject[ &quot;imText&quot; ] = messageText;<br />
        messageObject[ &quot;username&quot; ] = &quot;.NET Client&quot;;<br />
        messageObject[ &quot;color&quot; ] = 0;<br />
        messageObject[ &quot;isBold&quot; ] = false;<br />
        messageObject[ &quot;isItalics&quot; ] = false;<br />
        messageObject[ &quot;isUnderline&quot; ] = false;</p>
<p>        rso.setAttribute( &quot;UserMessage&quot;, messageObject );<br />
      }<br />
    }</p>
<p>    public void resultReceived( IPendingServiceCall call )<br />
    {<br />
      System.Console.WriteLine( &quot;RTMP client connected&quot; );<br />
      rsoIsReady.Set();<br />
    }</p>
<p>    public void onSharedObjectClear( ISharedObjectBase so )<br />
    {<br />
      System.Console.WriteLine( &quot;EVENT: onSharedObjectClear&quot; );<br />
    }</p>
<p>    public void onSharedObjectConnect( ISharedObjectBase so )<br />
    {<br />
      System.Console.WriteLine( &quot;EVENT: onSharedObjectConnect&quot; );<br />
    }</p>
<p>    public void onSharedObjectDelete( ISharedObjectBase so, string key )<br />
    {<br />
      System.Console.WriteLine( &quot;EVENT: onSharedObjectDelete&quot; );<br />
    }</p>
<p>    public void onSharedObjectDisconnect( ISharedObjectBase so )<br />
    {<br />
      System.Console.WriteLine( &quot;EVENT: onSharedObjectDisconnect&quot; );<br />
    }</p>
<p>    public void onSharedObjectSend( ISharedObjectBase so, string method, IList parms )<br />
    {<br />
      System.Console.WriteLine( &quot;EVENT: onSharedObjectSend&quot; );<br />
    }</p>
<p>    public void onSharedObjectUpdate( ISharedObjectBase so, IDictionary&lt;string, object&gt; values )<br />
    {<br />
      System.Console.WriteLine( &quot;EVENT: onSharedObjectUpdate&quot; );<br />
    }</p>
<p>    public void onSharedObjectUpdate( ISharedObjectBase so, Weborb.Messaging.Api.IAttributeStore values )<br />
    {<br />
      System.Console.WriteLine( &quot;EVENT: onSharedObjectUpdate&quot; );<br />
    }</p>
<p>    public void onSharedObjectUpdate( ISharedObjectBase so, string key, object value )<br />
    {<br />
      System.Console.WriteLine( &quot;EVENT: onSharedObjectUpdate&quot; );</p>
<p>      Hashtable messageData = (Hashtable) value;<br />
      System.Console.WriteLine( &quot;property &quot; + key + &quot;,  imText value &quot; + messageData[ &quot;imText&quot; ] );<br />
    }<br />
  }<br />
}<br />

The StartChat method is where the program connects to the localhost server running on port 2037 (WebORB’s default RTMP port). The “Chat” parameter is the name of the messaging application the client connects to. The main chat logic is in the MainChat

method. As you can see from the code the MainChat method is executed by a separate thread. The reason for this is the process of establishing a connection and receiving a notification that the connection has been established (lines 34 and 72) is asynchronous. As a result, a separate thread is required so the main method of the application does not end prematurely.

Inside of the MainChat method, the code uses the getSharedObject method to obtain a reference to the specified remote shared object – ChatSO (line 47). A listener object is added to the remote shared object instance so the application receives all the RSO events, including the one when the RSO is updated (lines 111-117).

You can download the complete project from the following URL: http://examples.themidnightcoders.com/blog/RSOClient.zip

To run the example and experience the feature:

  1. Compile the example code (you might need to set the weborb.dll assembly reference)
  2. Run the WebORB Management Console
  3. Run the .NET application
  4. In the management console switch to the Help/Resources tab, then click the Examples link
  5. In the examples tree in the console navigate to: FLEX Examples > Real-time Messaging (RTMP) > Basic Text Chat
  6. Type in a send messages both in the Flex and .NET clients – both send and receive data

To see the contents of the Remote Shared Object used in the sample .NET application switch to the Messaging Server tab in the Management Console, select the Chat node in the tree, then select the Shared Objects tab. As the clients send data, the console shows the internal state of the shared object.

Exactly the same approach and APIs are applicable if you build Android and iOS mobile applications. I plan to write a similar post demonstrating cross platform connectivity and messaging between .NET and Android and iOS clients.

Here’s a brief video showing the example in action: