Scriptico

.NET to Flex Data Push using Messaging Destinations

Few days ago I received a question about how to push data from the server code (C#) to the client side (ActionScript) though a messaging (publish/subscribe) destination. The server side is powered by WebORB for .NET and the client side is a Flex (or AIR) client. Using messaging destinations to broadcast messages to subscribers (or even delivering them via unicast in a point-to-point manner) is one of the approaches. The other one could be a server-to-client invocation using an RTMP connection initiated by the client. Both approaches have their own pros and cons, but in this post I will focus on the messaging destination approach. I am going to emphasize only the main steps/API, but the client and server projects are attached at the end of the article for the complete reference.

Destination Configuration

In order to use the approach we have to have the properly configured messaging destination. All messaging destinations are defined in the messaging-config.xml file. The file is located [VIRT_DIR_with_WebORB]\WEB-INF\flex. (where VIRT_DIR_with_WEBORB can be your ASP.NET application, or you can use the default one at /Program Files/WebORB for .NET/[version number]). We will use the following sample destination:


 
  
   false
  
  Weborb.Messaging.PubSub.Memory.MessagingServiceHandler
  Weborb.Messaging.PubSub.Memory.MemoryStoragePolicy
 
 
  
 
  

The destination above uses the WebORB Message Routing handler and the handler has a configurable policy for storing messages received from publishers and retrieving messages for delivery to subscribers. Also, the Flex client will communicate with the destination using the my-polling-amf channel ( the channel is defined in the weborb-services-config.xml).

Server Side

I created a simple service that will echo the received message from the particular client to all connected clients that are connected to the destination.

public void echoMessage(string message) {
            // get destination manager
            DestinationManager manager = ORBConfig.GetInstance().GetDataServices().GetDestinationManager();
            // get destination. the GenericDestination destination is defined in
            MessagingDestination destination = (MessagingDestination)manager.GetDestination("SamplePollingDestination");
            // create message wrapper
            Object[] messageWrapper = new Object[] { message };
            // push message to destination
            destination.GetServiceHandler().AddMessage(null, messageWrapper);
        }

As you can see from the code, it obtains a reference to the destination and publishes the message to the destination’s service handler. Yep, that’s it. Pretty easy, isn’t it? Now, let’s look on the client code.

Client Side

The client-side code is simple as well. It creates a consumer, adds an event listener to handle received messages, and connects to the destination. In the example, I am using the weborb-services-config.xml file as a -services parameter in the additional flex compiler parameters:

-services “C:\Program Files\WebORB for .NET\4.5.0.0\WEB-INF\flex\weborb-services-config.xml”

Unfortunately, it has a dependency to the weborb client library (weborb.swc) and as a result, the library has to be placed into the lib folder on the client side. However, you may exclude the dependency by  constructing the channel “chain” for the Consumer using the APIs. For more information on how to do it, please have a look at this article (will be opened in the new window). The client code snippet is shown below:

...
private function init():void {
	consumer = new Consumer();
	consumer.destination = "SamplePollingDestination";
	consumer.addEventListener( MessageEvent.MESSAGE,  onMessageEvent);
	consumer.addEventListener(ChannelEvent.CONNECT, onChannelEvent);
	consumer.addEventListener(ChannelEvent.DISCONNECT, onChannelEvent);
	consumer.subscribe();
}
...
private function onMessageEvent (event:MessageEvent):void {
	txtMessages.text = event.message.body.toString() + "\n" + txtMessages.text;
}
...

Example in action

Ok, we have the client and server sides, but how do you to put it all together? Although you may write or extend the current example and randomly push strings into the server service, I prefer to use the WebORB console.

  1. Build the server-side code and copy the assembly file into the bin folder of the WebORB installation.
  2. Open the WebORB console, click the Services tab, in the deployed .NET services tree navigate to .NET assemblies and find your assembly.
  3. If you did not make any changes in the server-side project, you should see the “DataPushExample.dll” node.
  4. Expand the node and select the DataPushExample –> FooService –> echoMessage node. WebORB Console will show the TestDrive tab for the selected method.
  5. Type any message in the field for the argument and click the Invoke button. The console will invoke the server-side method which will republish the message into the destination. That’s it!

The client side is attached as an exported Flash Builder project, so just download it, import into your workspace, build and run it.
The following screenshot shows the result of the service method invocation:

How to push data from server to client

Resources

Category: Action Script, C#, Development, Flex, WebORB (.NET)

Tagged:

Leave a Reply

ERROR: si-captcha.php plugin: GD image support not detected in PHP!

Contact your web host and ask them to enable GD image support for PHP.

ERROR: si-captcha.php plugin: imagepng function not detected in PHP!

Contact your web host and ask them to enable imagepng for PHP.