Remotely Connect Your Flex/AIR App Without Services-Config

Many developers have the question of how to define the infamous connections to the various AMF gateways to which they need to communicate through to their server-side code. The process, while usually handled through the famed services-config.xml file, can actually be handled directly in your ActionScript very easily.

The examples below are showing connecting to a AMFPHP endpoint as opposed to a ColdFusion one, only because that is what I have been learning and working on recently. For ColdFusion, simply replace the latter part of the string with /flex2gateway/

First, define a new string in your model that references the URI to which you are going to connect with your RemoteObjects:

var endpointUri:String = "http://mysite.com:8000/amfphp/gateway.php";

Then, simply define a new ChannelSet, and Channel to add to the ChannelSet that references your URI:

var cs:ChannelSet = new ChannelSet();      
var customChannel:Channel = new AMFChannel("my-amfphp", endpointUri);
cs.addChannel(customChannel);

Finally, define your RemoteObject to access this ChannelSet that you just created and set the destination to the name you gave your AMFChannel:

<mx:RemoteObject id="employeeService" channelSet="{cs}" destination="my-amfphp" source="com.yourcompany.app.employee.EmployeeService"
      showBusyCursor="true" />

It is as simple as that! Be sure to take out the services-config declaration in your compiler arguments to test this out appropriately.

Comments (Comment Moderation is enabled. Your comment will not appear until approved.)
Hey Kyle, the color of the string in the first line of code is impossible to read.
# Posted By Raymond Camden | 2/26/08 9:50 AM
Interesting, it must have to do with the auto-recognization of links.
# Posted By Kyle Hayes | 2/26/08 9:55 AM
You forgot to add your AMFChannel to your ChannelSet :)
# Posted By Michael Ramirez | 2/26/08 11:16 AM
I knew that :-P
# Posted By Kyle Hayes | 2/26/08 11:21 AM
Cool. Does this work with both Flex 2/Flex 3 apps?
# Posted By William Ukoh | 2/27/08 11:37 AM
@William - Yes, it does.
# Posted By Kyle Hayes | 2/27/08 11:45 AM
I have been trying to adapt this so that the 'destination' attribute of the RemoteObject is a variable (so i can define vars for the destination and endpoint URI), but Flex doesn't seem to like it. Do you know how you can programmatically set the destination name ?
# Posted By Andrew S | 6/13/08 11:13 AM
You could probably do it using ANT
# Posted By Kyle Hayes | 6/13/08 12:41 PM
True, however I am hoping I could have an external settings.xml file which will set the values at run time, not at compile time. When I put a binding expression in the destination attribute (i.e. destination="{somevar}") the compiler doesn't complain, but at runtime I always get an error.
# Posted By Andrew S | 6/13/08 1:57 PM
I'm pretty sure the compiler needs to know that at runtime, hence the reasoning behind needing access to the services-config.xml or other data about the remote connection when you compile.
# Posted By Kyle Hayes | 6/13/08 5:29 PM