Anyone familiar with Flex Data Services knows about services-config.xml.

This file usually contains security constraint definitions, channel definitions, and logging settings that each of the services can use. (more here)

It’s one of those config files that always feels more complicated than it needs to be, and to top it off, it’s a compile-time resource. This limits its flexibility, especially when it comes to channel definitions.

Why should you have to recompile your application whenever your channel definitions change? Let’s declare them in AS3 instead!

Here’s an example of two channel definitions from a services-config.xml file:

<channel-definition id="cf-polling-amf"
    class="mx.messaging.channels.AMFChannel">

    <endpoint uri="http://{server.name}:80/flex2gateway/cfamfpolling"
        class="flex.messaging.endpoints.AMFEndpoint"/>

    <properties>
        <polling-enabled>true</polling-enabled>
        <polling-interval-seconds>2</polling-interval-seconds>
    </properties>

</channel-definition>

<channel-definition id="cf-rtmp"
    class="mx.messaging.channels.RTMPChannel">

    <endpoint uri="rtmp://{server.name}:2048"
        class="flex.messaging.endpoints.RTMPEndpoint"/>

</channel-definition>


Now let’s look at the same two channel definitions in AS3:

var serverName : String = parameters.serverName;

var pollingChannel : AMFChannel = new AMFChannel ("cf-polling-amf",
    "http://&quot; + serverName + ":80/flex2gateway/cfamfpolling");
var rtmpChannel : RTMPChannel = new RTMPChannel ("cf-rtmp",
    "rtmp://" + serverName + ":2048");

pollingChannel.pollingEnabled = true;
pollingChannel.pollingInterval = 2000;

var channelSet : ChannelSet = new ChannelSet ();
channelSet.addChannel (pollingChannel);
channelSet.addChannel (rtmpChannel);


At first glance this code doesn’t appear to offer any advantage over the channels defined in services-config.xml. However in the AS3 code, the server name for the channels is passed in as a flashVar to the application.

This allows your application to determine at runtime which channels to use, and it also eliminates the need to compile the XML file into your application.

Two wins in my book.

Addendum: Adding Channel Definition Timeouts