Requirements 

     
Prerequisite knowledge
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.
 
User level
Intermediate
Required products
Flash Builder (Download trial)
 
Sample files  

   

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.
 

 
Project overview

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).
 
Figure 1. Data push from the server side to the client via MSMQ and WebORB for .NET.
Figure 1. Data push from the server side to the client via MSMQ and WebORB for .NET.

 
Project setup

Before you begin coding you'll need to set up MSMQ and WebORB for .NET.
 
 
MSMQ setup
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.
 
  1. Open Control Panel and then open Administrative Tools > Computer Management.
  2. Expand Message Queuing and you will see Private Queues.
  3. Right-click to create a new non-transactional Private Queue (see Figure 2). Type weborb-testqueue as the Queue Name. Click OK.
Figure 2. Creating a new private queue.
Figure 2. Creating a new private queue.
  1. Right-click on the newly created queue and select Properties. Type WebORB MessageQueue for Flex as the label (see Figure 3). Click OK.
Figure 3. Setting the queue label.
Figure 3. Setting the queue label.
The queue name and label name are important parameters that are used when configuring WebORB.
 
 
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.
 
  1. Open messaging-config.xml and add the following XML:
<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>
  1. Save your changes.
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.
 

 
Developing the .NET code

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.
 

 
Developing the Flex Code

The Flex application acts as the consumer.
 
 
Pinging WebORB
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(); }
 
Registering as a consumer
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"; }
 
Receiving messages
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.
 

 
Running the Example

Follow the steps below to run the sample application:
 
  1. If you have not already done so, create an MSMQ queue and configure WebORB for .NET as described in the Project setup section.
  2. Open Microsoft Internet Information Services (IIS), create a virtual directory, and deploy the .NET project from the sample files into the virtual directory. In your browser, navigate to the default.aspx file on your server; you should see the MSMQ Producer form.
  3. Locate ShareData.dll in the bin folder of the sample .NET project and deploy it inside the WebORB bin folder, for example: [WEBORB HOME]\bin.
  4. Import the sample Flex project into Flash Builder. Add WebORB.swc, which is located [WEBORB HOME]\weborbassets\wdm, to the Flex project Build Path library.
  5. Add the following as an additional compiler argument:
-services "pathname_in_your_machine_to_the_WebORB_directory/WEB-INF/flex/weborb-services-config.xml"
  1. Run the application and wait for the application to ping the WebORB server. If the ping is successful, it will show an Alert that says "Hello from web.orb".
  2. Now Click Connect and you should see an acknowledgement message.
  3. Type some values in the form on the .NET page and click Send. The message will be sent to the queue.
  4. Observe the Flex application, which should update the data grid with the data that you typed in the .NET page (see Figure 4).
Figure 4. The .NET page (left) and the Flex client (right).
Figure 4. The .NET page (left) and the Flex client (right).
If you encounter any problems, the first troubleshooting step is to make sure that the .NET page is sending the messages to the queue.
 
  1. In Control Panel, open Administrative Tools > Computer Management > Message Queuing > Private Queues.
  2. Then open the queue that you configured earlier and check the queue messages. If the messages are not available, it could be that you do not have permission to read and write to the queue.
  3. Go to the Security tab in the Properties section of the queue and assign full permission to your user name (the one that you used to log in).
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.
 

 
Where to go from here

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.