I’m John C Bland II

Husband, Father, Tech Author, Deacon.
Founder ofย Katapult Media, and full-stack polyglot developer.
Political Free Agents Podcast Host.

I create. I launch.

YouTube Channel

I post regular fun on YouTube like me playing the bass and anything else I find fun.ย 

Get Something Built

All project work goes through Katapult Media. Business is open. Let’s chat.

Yesterday I was pinged on IM by John Farrar about an issue he was having connecting Flex to ColdFusion on his new system. He’d done this many times before but hit a bump so we jumped on Adobe Connect and looked into it.

One of the issues he was having, which went unresolved, was in him setting his server location info. It asks for things like server root, etc (see below).

Flex Builder Server Technology Screen

The main problem is in his setup, it seemed. He is using Apache as his web server and server docs out of a separate folder on his system, not the standard c:\ColdFusion8\wwwroot (for standalone). Typing in the CF location expects your “Output folder” to be within the CF\wwwroot folder. The problem with that is he using a completely separate folder (c:\foo\whatever\localhost). Maybe there was a way around it but after fooling with it for a second I fell back to old faithful. ๐Ÿ™‚

From my first time playing with Flex 2 remoting I attempted to use this screen but didn’t want to use my localhost CF (I stopped doing that a while back; have come back to local dev though). I learned how to set my endpoint, destination, and source all on my mx:RemoteObject directly so I took that approach in helping Senor Farrar.

Here is some simple code to point out the ease of writing Remoting code in mxml.

1
2
3
<mx:RemoteObject id="service" destination="ColdFusion" endpoint="https://localhost/flex2gateway" source="com.katapultmedia.test.SomeCFC" fault="trace(event.message.body)">
<mx:method name="lookMa" result="trace(event.result)" />
</mx:RemoteObject>

That’s it. This would work for PHP or any other backend by simply setting the endpoint, destination, and source. In the case I’m pointing to my local ColdFusion instance, calling a cfc in folder com/katapultmedia/test/SomeCFC.cfc, and calling method lookMa(). There is a “global” fault handler (on the mx:RemoteObject tag itself) which means any and every unhandled fault message will be handled with the this one. It merely traces the fault messages body. The method I’m calling traces the result from the server.

That’s it. It literally is that easy. You can point it o any server you like (with Remoting on and a CFC exposed) at compile time or at runtime, via flashvars, config file or any other type of runtime config’ing. (is that a word? is now.)

Now, I wouldn’t be me if I didn’t show you how to do this in ActionScript 3 as well. It is just as easy…well…maybe a few more lines of code but still.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
package{
	import mx.messaging.ChannelSet;
	import mx.messaging.channels.AMFChannel;
	import mx.rpc.events.FaultEvent;
	import mx.rpc.events.ResultEvent;
	import mx.rpc.remoting.RemoteObject;
 
	public class DataManager{
		private var service:RemoteObject;
 
		public function DataManager(){
			//Create the AMF Channel pointing to your CF instance
			var channel:AMFChannel = new AMFChannel("my-cfamf", "https://localhost/flex2gateway");
 
			service = new RemoteObject("ColdFusion"); //setup the RemoteObject
			service.channelSet = new ChannelSet([channel]);
			service.destination = "com.katapultmedia.test.SomeCFC"; //set the destination
			service.addEventListener(FaultEvent.FAULT, function(event:FaultEvent):void{ trace(event.message.body); }); //default fault handler
			callMama(); //call your method
		}
 
		public function callMama():void{
			//see if there is an event listener already setup for this method; if not, set it.
			if(!service.lookMa.hasEventListener(ResultEvent.RESULT)) service.lookMa.addEventListener(ResultEvent.RESULT, mamaSaid);
			service.lookMa();
		}
 
		private function mamaSaid(event:ResultEvent):void{
			trace(event.result); //trace result
		}
	}
}

I commented the AS a bit so I won’t go through explaining but you can see how easy it is to write it in AS. My suggestion is to write it in AS so you have 1 single location of managing/handling remote data calls. You CAN do that through MXML but this is just cleaner to me but hey…I’m a code hungry nut, at times. ๐Ÿ˜‰

Anyways…take the above code as experimental. I didn’t test it. I wrote it in Flex Builder just to make sure I didn’t have any obvious errors but I didn’t test it against my server or anything.

Hopefully this helps someone in the future with RemoteObject dev.

** ADVICE **
Keep in mind any time you want to call a remote service and your swf will NOT sit on the same server you are calling, it is best to set the values at runtime even if they are in a static class holding the values (meaning not dynamically retrieved from flashvars or otherwise). I think the biggest reason why is you never know when you might make your swf into an AIR app which will require this approach anyway.

Have a great one!

Oh…Nimer posted about this a while back too. He talks a bit more about the services-config, etc. Peep it: https://www.mikenimer.com/index.cfm/2007/1/10/Bye-bye-services.