I’ve been working on a project where we had to use Flex on a .Net environment and it had to be realtime. I was a little worried at first that it could be tricky to set up. But with WebORB.Net it’s pretty easy. We used the integrated RTMP (Real Time Messaging Protocol) messaging server. It’s almost like using WCF. The most important differences are that, by default, objects are transmitted as Hashtable and calls can’t be synchronous. We can bind the object to right .Net object within the WebOrb management console but we decided to do it ourself using reflection (because we don’t like to depend too much on the management console).

RTMP

The result is pretty impressive as it makes a really powerful real-time management system. And it’s freaking fast. Like WCF, it can handle concurrent calls. It’s in fact easier because everything is asynchronous but still, it does manage that. These calls are received on parallel threads.

The only problem with WebORB.Net is that you don’t have too much examples in the documentation. Our biggest problem was that we didn’t even knew how to detect what was the current calling connection on a method. This is pretty easy but you can’t exactly guess it. This is a dummy sample to show you how it’s done :

public class FlexConnectionsServer:ApplicationAdapter {
    private int _connectionIdCounter = ;
    private Dictionary<IConnection,int> _connectionsToId = new Dictionary<IConnection,int>();
 
    // Returns the CURRENT connection (must be used from a method called by the flex client)
    private IConnection CurrentConnection { get { return ConnectionHub.getConnectionLocal(); } }
    private int CurrentID { get { return _connectionsToId[ CurrentConnection ]; } }
 
    // When the RTMP service is loaded, we start the time dispatcher thread
    public override bool appStart( IScope app ) {
        new Thread( Thread_TimeDispatcher ).Start();
    }
 
    // This method is called when a client connects
    public override bool appConnect( IConnection conn, object[] parms ) {
        lock ( _connectionsToId ) {
            _connectionsToId.Add( conn, _connectionIdCounter++ );
        }
 
        return true;
    }
 
    // This method is called when a client disconnects
    public override void appDisconnect( IConnection conn ) {
        lock ( _connectionsToId ) {
            _connectionsToId.Remove( conn );
        }
    }
 
    // This is a sample method call
    public String GetMyName() {
        return String.Format( "Client{0}", CurrentId );
    }
 
    // This thread dispatches the current time to every client
    private void Thread_TimeDispatcher() {
        while( true ) {
            var t = DateTime.UtcNow;
            SendCommand( "CurrentTime", new Object[] { t } );
            Thread.Sleep( 1000 );
        }
    }
 
    // Call a remote method on each client connection
    private void SendCommand( String command, Object[] params ) {
        lock ( _connectionsToId ) {
            foreach( var kvp in _connectionsToId )
                ( kvp.Key as IServiceCapableConnection ).Invoke( command, params );
        }
    }
}

It’s a little bit like using WCF in Single instance mode.

IIS 7.0 Setup

There was something really weird that happened with my IIS 7.0 on my Windows 7 x64. Using the RTMP connection, I frequently had null return values on .Net method calls. We firstly find a quick way to solve it : Recall each time we get a null value. It’s really dirty, but It worked as a very temporary solution.

Then, when debugging on the IIS process, I noticed a lot of NullReferenceException exceptions displayed on the debugger console. They were about the WebORB “LicenseManager” class, and I had as much exceptions as I had null values on the Flex client. I tried a lot of things that didn’t do anything. And then, I changed the “Managemd pipeline mode” of the Application Pool from “Integrated” to “Classic”, and the problem was solved. The really weird stuff here is that WebORB recommends to use the “Integrated” mode.

MidnightCoders support

We asked MidnightCoders to send us a community edition license, and they instantly answered. We got our license within the 48 hours after our first mail (I was the one slowing it down).

Quick note on the project

To give you a better view of the project I’m talking about :

GPRS Equipment <–(M2MP protocol)*> [Generic M2M Server] <–(WCF)–> [Business logic software] <–(WCF)–> [Flex connections server] <–(RTMP)–> Flex

We’ve got a little equipment (TC65 based as you can guess) that connects to a generic server using our own (real-time and bandwidth optimized) protocol. A business logic software connects to this server using a WCF service. And then we have the WebORB.Net / IIS that connects to this business logic software.

WCF helps make very interesting .Net applications because it simplifies all the communication but can also create deadlocks you might not have seen. Take this example : You have a lock on the WCF Server on the clients list object, you make a call to the WCF client and this client makes a call to the WCF Server that also requires a lock. If you were doing all this client/server calls on the same process this wouldn’t be a problem. But as you use WCF client-to-server and server-to-client calls are made on differents threads. You create a deadlock (unless you set the OperationContract attribute to IsOneWay and/or you fix the deadlock source).

If you’re doing real-time applications, I would recommend to define all the frequently used methods with the “IsOneWay=true” option.

06/11/2009 Update : I added a sample server-to-client code because some people were interested by it.