8 August 2011
Intermediate level knowledge of Flex, .NET, and IIS are required to make the most of this article. Some knowledge of MSMQ will also be helpful.
Intermediate
Additional required other products
There are several message queuing options available to push data from the server side to Flex clients. The option you choose will depend upon the server side technology you use. If your server-side is Microsoft .NET, then you can use Microsoft Message Queuing (MSMQ), a free queuing mechanism included with most Windows operating systems, to push data from .NET to various clients. This allows applications running on separate servers and processes to communicate with one another. MSMQ enables publish-subscribe message exchange paradigms for Windows/.NET environments. This article will show you how to route messages from MSMQ to Flex clients, enabling data push from .NET to Flex.
This article walks through the process of creating a simple real-time stock quoting application that uses WebORB for .NET and MSMQ to enable communication between a .NET back end and a Flex client. The sample project contains a simple server-side page, which is used to enter company stock information. The Flex client has a data grid, which displays the stock information in real-time as it is received via MSMQ.
WebORB for .NET is used for publish-subscribe messaging, enabling messaging destinations to be connected with MSMQ queues. WebORB for .NET acts as a proxy between the queue and the subscribed client (see Figure 1).
Before you begin coding you'll need to set up MSMQ and WebORB for .NET.
On your Windows computer, open the Control Panel and then open Computer Management in Administrative Tools. Expand Services and Applications and look for a folder named Message Queuing. If you don't find such a folder, you will need to install MSMQ for this example to work properly. MSMQ can be installed via the Control Panel. For more information on installing it on different Windows operating systems see Installing Message Queuing (MSMQ) on MSDN.
Once installed, you will need to create a queue.
The queue name and label name are important parameters that are used when configuring WebORB.
Download and install WebORB for .NET if you haven't already done so. The MSMQ exposed through WebORB acts as a messaging destination for an application that needs to listen for messages in the queue. To configure the queue as a destination for the Flex client, you'll need to edit WebORB's messaging-config.xml file located under the [WEBORB-INSTALL]/WEB-INF/flex directory.
<destination channels="weborb-rtmp" id="webmsmq">
<properties>
<msmq>
<path>.\private$\weborb-testqueue</path>
<deliverPastMessages>-1</deliverPastMessages>
<BasePriority>0</BasePriority>
<Category>00000000-0000-0000-0000-000000000000</Category>
<Label>WebORB MessageQueue for Flex</Label>
<MaximumQueueSize>4294967295</MaximumQueueSize>
<UseJournalQueue>false</UseJournalQueue>
<MaximumJournalSize>4294967295</MaximumJournalSize>
</msmq>
<message-service-handler>
Weborb.Messaging.PubSub.Msmq.MessagingServiceHandler
</message-service-handler>
</properties>
<channels>
<channel ref="weborb-rtmp"/>
</channels>
</destination>
The purpose of each node is explained in the messaging-config.xml file.
In the sample project, the .NET web page targets the queue named weborb-testqueue. The value of the id attribute of the destination node (in this case, webmsmq) specifies the messaging destination. This same value will be used for the destination property in the consumer—that is, the Flex client—as well.
The .NET code will act as an MSMQ producer. The web page consists of a form into which values are typed. Upon clicking the Send button on the ASPX page, SendMsg() is invoked
public void SendMSG()
{
StockVO objVo = new StockVO { Company = txtCompanyName.Text,LastSales = Convert.ToDouble(txtLastSales.Text), ShareVolume = Convert.ToDouble(txtShareVolume.Text), change = Convert.ToInt64(txtChange.Text) };
MessageQueue objQueue = new MessageQueue("FormatName:DIRECT=OS:.\\private$\\weborb-testqueue");
BinaryMessageFormatter objformat = new BinaryMessageFormatter();
objformat.TypeFormat = System.Runtime.Serialization.Formatters.FormatterTypeStyle.TypesAlways;
objQueue.Formatter = objformat;
objQueue.DefaultPropertiesToSend.Recoverable = true;
Message objMsg = new Message(objVo, objformat);
objMsg.Label = "test";
objQueue.Send(objMsg);
}
All the values from the form are passed as properties of StockVO, which is the value object. The objQueue instance of MessageQueue targets the queue and sends objVO (the StockVO instance) as a message to the target queue.
The Flex application acts as the consumer.
For the consumer to receive messages, it is important to know whether the WebORB messaging service is up and running. So, on the creationComplete event of the application the PingFromWeborb() method is invoked. This method returns a string as the result, which is displayed in an alert. When the result event fires, you know that the messaging service is running. If the ping was unsuccessful, then the fault event handler will display the error details in an alert. To check what went wrong, you can use an HTTP sniffer tool to see if the WebORB gateway was called. To investigate further, debug the Flex application as well as the Global.asax file on the server side. Global.asax, which is located inside the WebORB folder, is where the messaging server is started.
private function pingWeborb():void
{
var ro:RemoteObject = new RemoteObject("GenericDestination");
ro.source = "ping";
ro.endpoint = "http://localhost/weborb4/weborb.aspx";
ro.PingFromWebORB.addEventListener(ResultEvent.RESULT, onPingResult);
ro.PingFromWebORB.addEventListener(FaultEvent.FAULT, onPingFault);
ro.PingFromWebORB();
}
Upon clicking the Connect button, the Flex client will register itself as a consumer. The WeborbConsumer class of the WebORB Client API enables the Flex client to subscribe to a messaging destination. The value of the destination property should correspond to the value of the destination attribute in the messaging-config.xml file. The subscribe() method subscribes the Flex client to the queue.
public function doConnect():void
{
consumer = new Consumer();
consumer.destination = "webmsmq";
consumer.addEventListener( MessageEvent.MESSAGE, receivedMessage );
consumer.addEventListener( MessageAckEvent.ACKNOWLEDGE, receivedACK);
consumer.subscribe();
currentState = "connectedState";
}
To get messages from the queue, the instance of the consumer listens to MessageEvent.MESSAGE. Its message handler will receive the message from the queue.
public function receivedMessage( event:MessageEvent ):void
{
stock = StockVO( event.message.body );
dataCollection.addItemAt(stock, 0);
}
The messages received are cast to complex types and added to an ArrayCollection object, which populates the data in a data grid.
Follow the steps below to run the sample application:
-services "pathname_in_your_machine_to_the_WebORB_directory/WEB-INF/flex/weborb-services-config.xml"
If you encounter any problems, the first troubleshooting step is to make sure that the .NET page is sending the messages to the queue.
If you have messages in the queue and you're still not getting data pushed to the client, the messaging server might be down. For the messaging server to start, an ASP.NET request must be sent to the application first. For more information on how to do this, see RTMP Server Overview in the WebORB for .NET User Guide.
Now that you have the application up and running, you can run more than one instance of the Flex client to see that the data pushed from MSMQ through WebORB is routed to multiple Flex clients.
You may also want to learn more about conditional message delivery, which you can use to control the delivery of messages based on conditional logic in the message delivery chain. For more details, see Data Push from MSMQ to Flex.

This work is licensed under a Creative Commons Attribution-Noncommercial-Share Alike 3.0 Unported License