RSS LinkedIn Twitter

Messaging with Flex/ColdFusion via BlazeDS: Part 2 (Flex configuration)

This is part two of a two part series. See part one, Messaging with Flex/ColdFusion via BlazeDS : Part 1 (CF configuration)

This installment will go over setting up messaging in Flex, and assumes everything is working correctly on the server. Recall I will be covering two scenarios. A client-publish/client-subscribe model, and a server-publish/client-subscribe model.

Configuring Channels with ActionScript

I prefer defining my channels and channel sets in ActionScript on the client for a number of reasons. It does away with the need for client side configuration files and additional command line parameters that need to be set up each time I check out the project onto a new machine.

A channel is instantiated as follows:

[as3]private var pollChannel : AMFChannel = new AMFChannel(“cf-polling-amf”,;[/as3]

The cf-polling-amf corresponds to the channel definition in the services-config.xml file residing on the server. If you gave it a different name previously, or set up a new one, the names should correspond.

The channel set is instantiated as follows:

[as3]private var amfChannelSet : ChannelSet = new ChannelSet();[/as3]

Then, somewhere inside a method (init(), for example) the channel needs to be assigned to the channel set using the addChannel() method. The channel is now ready to use for remoting or messaging.


There are three classes you’ll use almost exclusively when dealing with AMF messaging. The Producer class, which connects to the messaging channel and is used to send messages. The AsyncMessage class which defines the format of the messages broadcast via the Producer. And the Consumer, which subscribes to the incoming messages received from the server.

The channelSet and destination properties need to be set on the Producer and Consumer, where the destination corresponds to the destination configured in the messaging-config.xml file on the server.

A MessageFaultEvent.FAULT listener can be attached to the Producer to listen for any errors that occur when connecting. And a MessageEvent.MESSAGE listener should be attached to the Consumer to handle messages that are broadcast to the clients.

Finally the consumer must call it’s subscribe method to indicate it is ready to receive messages. A consumer can subscribe and unsubscribe as often as needed.

Publishing a Message

The Publisher sends message to the server in the form of an AsyncMessage. The AsyncMessage class has two properties that we will be concerned with, the headers property and the body property.

The headers property is a dynamic object that you can add any data you want to. In this case we only need to set up one header value (many more are set after you call send() as it moves up the call stack) which is gatewayid. The gatewayid will correspond to the event gateway you set up in your CF Adminstrator.

The body property on the AsyncMessage class is used to send the message payload, and can contain anything. It is an anonymous object, and can have any number of additional properties or objects attached to it.

After setting these properties, the producer calls send, and passes the message as a parameter. The rest happens on the server and in the method that handles the MessageEvent.

Handling the Message Event

Handling the MessageEvent is similar to creating it, only backward. All of the properties that were set when it was published should be there (unless the CFC gateway explicitly removed or overwrote them). Nested in the event is a body and a headers property. They can be accessed as follows:


And that’s all there is to it.

Server Publish

As previously mentioned, the server can also publish messages. This is different from the previous scenario in the following ways:

1. You only need to set up a Consumer… no Publisher is needed since the client will not be publishing messages.
2. A CFC on the server needs to manually publish the message. This can happen in a variety of applications.

This is accomplished in ColdFusion by setting up the event object, and then passing it in a call to SendGatewayMessage(). Here is some sample code demonstrating:

<cfset data.message = “someObjectOrValue” />
<cfset data.type = "optionalValue" />

<cfset event.body = data />
<cfset event.Destination = "AValidDestination" />
<cfset ret = SendGatewayMessage("anEventGateway", event) />

Other Applications

There are a number of configurations you could have that I haven’t mentioned. A Flex application could publish messages and broadcast to a separate Flex or AIR application that is subscribed to the same channel. There are a variety of methods available to filter messages received.

I’ve only just scratched the surface in getting it set up. What I’m excited for is for an RTMP library to become available, as Adobe announced plans to open source it in the first half of this year. We’ll see how that turns out.

No comments yet.

Leave a Comment