Making Flash Remoting easier in Flash CS3 by Thibault Imbert

Here is something I have just added into my book in the Flash Remoting chapter and wanted to share with you :)

As you may know, no wrapper class is embedded into Flash CS3 for your remoting calls, you have to use the raw NetConnection class which is not very practical in a large project.

I wanted a simple class to call some remote methods in Flash CS3 like the RemoteObject class does in Flex, and like the Service class we had in Flash 8. I came across Danny Patterson remoting framework which is very smooth, but noticed that it was not working the way I wanted it.

So I created a wrapper around the NetConnection object which behaves like the Service class we used to have with a PendingCall assigned to each remote procedure.

Here is how you create the remote service :

// create a remote object (Singleton)
var service:Service = Service.getInstance();

// set the required infos
service.setRemoteInfos ("http://www.bytearray.org/wp-content/projects/remotingservice/amfphp/gateway.php",
"org.bytearray.HelloWorld",
ObjectEncoding.AMF3 );

Then you call the remote method, this returns you a PendingCall object :

// call the remote method
var rpc:PendingCall = service.sayHello("Hello there ?");

The PendingCall object is dispatching two events, ResultEvent.RESULT and FaultEvent.FAULT :

// call the remote method
var rpc:PendingCall = service.sayHello("Hello there ?");

// wait for the result event (everything is ok)
rpc.addEventListener ( ResultEvent.RESULT, success );

// wait for the fault event (an error occured)
rpc.addEventListener ( FaultEvent.FAULT, error );

// get the result
function success ( pEvt:ResultEvent ):void
{

// output : you said : Hello there ?
trace( pEvt.result )

}

// get the result
function error ( pEvt:FaultEvent ):void
{

trace( pEvt.fault.description )

}

You can change the service or gateway at any time :

service.service = "org.bytearray.AnotherService";
service.gatewayURL = "http://www.myserver.org/amfphp/myOtherGateway.php";

Or authenticate yourself with some credentials :

service.setRemoteCredentials ("bob", "groove");

Sources and file examples available here

Update (05/04/09) : Version 0.2 : Changed the Service implementation as a Singleton.

Comments (30)

  1. frog wrote::

    Nice one Jimmy !

    Thursday, January 24, 2008 at 2:47 pm #
  2. Thibault Imbert wrote::

    haha you’re welcome froggy ;)

    Thursday, January 24, 2008 at 2:54 pm #
  3. Marc wrote::

    funny i thought the same about the as3 lightweight remoting framework (missing listeners on calls) and just as i started to write my own implementation i found the SSR (super simple remoting ;) ). it works as expected and saved some time. but thanks for your implementation none the less *gg*

    Thursday, January 24, 2008 at 3:10 pm #
  4. Marc wrote::

    whoops, the link: http://osflash.org/projects/ssr

    Thursday, January 24, 2008 at 3:10 pm #
  5. Mike wrote::

    Can timeout interval be adjusted?

    Thursday, January 24, 2008 at 6:43 pm #
  6. William from Lagos wrote::

    What’s the title of your book by the way?

    Thursday, January 24, 2008 at 7:54 pm #
  7. Thibault Imbert wrote::

    Hi Mike,

    yes such a feature can be added, I will add that :)

    Hi William,

    The book is called “Pratique d’ActionScript 3″ and is written in French. O’Reilly should translate it after the release !

    link to the book :

    http://www.oreilly.fr/catalogue/2841774457

    regards,

    Thibault

    Thursday, January 24, 2008 at 8:15 pm #
  8. Thibault Imbert wrote::

    Hi Marc,

    very nice !

    Maybe we could merge our two projects ?

    What do you think ? :)

    best,

    Thibault

    Thursday, January 24, 2008 at 8:18 pm #
  9. Marc wrote::

    Hi Thibault, SSR was written by Aaron Smith (http://blog.rubyamf.org/) and not by me. I just use it ;)

    Thursday, January 24, 2008 at 10:30 pm #
  10. Thibault Imbert wrote::

    hehe ok Marc !

    thanks :)

    Thursday, January 24, 2008 at 10:42 pm #
  11. Moïse wrote::

    Very nice,
    I had started started to write my own remoting class
    I’m going to try it :-)

    Friday, February 15, 2008 at 8:46 am #
  12. paul wrote::

    Why not just import the rpc.swc from the Flex SDK, why reinvent the wheel here… Interesting solution however.

    Saturday, February 16, 2008 at 10:54 pm #
  13. Thibault Imbert wrote::

    Hi paul,

    I saw quite complicated hacks to do that, did you try it ?

    If it works smoothly, that would be nice :)

    let me know

    cheers

    Sunday, February 17, 2008 at 12:05 am #
  14. Thomas wrote::

    Hi Thibault,

    thanks for sharing your solution with us. May I ask, under which licence you’re publishing the code? Am I allowed to use it in commercial projects and am I allowed to modify it to my special needs?

    Wednesday, March 5, 2008 at 11:45 am #
  15. Thibault Imbert wrote::

    Hi Thomas,

    There is no licence. You can modify the code and do whatever you want with it :)

    cheers,

    Thibault

    Wednesday, March 5, 2008 at 12:05 pm #
  16. CG wrote::

    Question…how can I make Flash remoting -> ColdFusion secure?

    It seems that you have to set a CFC to access=”remote” but that allows anyone in the world to run it as well.

    Monday, March 24, 2008 at 3:23 pm #
  17. gregpich wrote::

    Hi Thibault,
    I’m looking for a french tutorial video that you have done, about AS3 and amfphp, but I can’t find it anymore on the web. May you please post the link?

    Wednesday, March 26, 2008 at 5:37 pm #
  18. TrAvIkK wrote::

    Hello :)

    First of all, I wanted to say, that you rock man :) Waiting for your book to see it in English :)

    Here is what i wanted to admit: In some way, FaultEvent is not dispatched, or catched by event listener, in Flex 3.

    Tuesday, April 15, 2008 at 9:36 am #
  19. SiggeLund wrote::

    I’m having problems getting this to work with query returns from ColdFusion.

    var results:Object = pEvt.result;
    var queryItems = results.items;
    for (var queryIndex=0; queryIndex<queryItems.length; queryIndex++)
    {
    }

    This snippet won’t work since the items object is undefined. Anyone know how this should be done in AS3?

    Saturday, September 6, 2008 at 8:00 pm #
  20. SiggeLund wrote::

    Figured it out. This code snippet can be used to find the old style Recordset

    /*
    Returns an array with objects, that works the same way as the results.items did in AS2.

    Return events from AS3 Flash Remoting calls only contain one member when returning,
    and it’s called “serverInfo”. Pass that object to this method, and the normal array is then returned.
    Stupid that Adobe has not implemented this themselves…

    Example:
    function listEvents_Result( pEvt: ResultEvent ):void
    {
    var results = pEvt.result;
    results["items"] = RemotingUtil.getRecordSet(results.serverInfo);

    for (var queryIndex=0; queryIndex<results.items.length; queryIndex++)
    {
    var queryObj = results.items[queryIndex];
    trace(queryObj.name);
    trace(queryObj.date);
    }
    }

    In this example we assume that the recordset has the two columns “name” and “date”.

    */
    public static function getRecordSet(serverInfo:Object):Array
    {
    var queryItems = serverInfo.initialData;
    var queryColumns = serverInfo.columnNames;
    var retArray:Array = new Array();

    for (var queryIndex=0; queryIndex<queryItems.length; queryIndex++)
    {
    var queryObj = new Object();
    for (var j=0; j<queryColumns.length; j++)
    {
    queryObj[queryColumns[j]] = queryItems[queryIndex][j];
    }
    retArray.push(queryObj);
    }
    return retArray;
    }

    Sunday, September 7, 2008 at 11:38 am #
  21. Thibault Imbert wrote::

    Hi SiggeLund,

    Yes, in Flash CS3 when you are receiving a query from the server you receive an objet with a serverInfo property that you have to parse the construct an appropriate RecordSet.

    Adobe decided to remove the Flash Remoting components from Flash CS3 which is in my opinion a real mistake. Flash CS4 should solve that limitation ;)

    Sunday, September 7, 2008 at 12:55 pm #
  22. SiggeLund wrote::

    Thibault:

    Tell me about it… Good thing that I found the tool ServiceCapture, would have been a bit confused otherwise. :)

    Sunday, September 7, 2008 at 3:34 pm #
  23. Thibault Imbert wrote::

    Hi SiggeLund,

    I cannot talk about it for the moment ;) , but as soon as possible I will post a tutorial here about the new capabilities offered by Flash CS4 in terms of Remoting.

    Yes ServiceCapture is a must-have when it comes to Remoting debugging. ;)

    best,

    Thibault

    Sunday, September 7, 2008 at 7:17 pm #
  24. SiggeLund wrote::

    Sounds great, I will look in from time to time to see what you’re cooking. =)

    Sunday, September 7, 2008 at 10:09 pm #
  25. Hi Thibault, we also implemented a remoting package in our as3 opensource project (we are a commercial company) recently launched at http://www.dpdk.nl/opensource.
    It also features built in support for recordsets which is a datatype instead of a multidimensional array as presented in the example above.

    It also tackles the problem you tackled with danny patterson’s implementation, which is that you can only call one method on one service. Both your and our solution fixed that by providing handlers/events for each method called on the service

    Monday, October 6, 2008 at 8:48 pm #
  26. John Barrett wrote::

    Thanks for the post`-`
    I have been going crazy trying to use flash remoting in CS3 with a cfc backend, and with no luck.

    I am at school now, but I am going to try this when I get home.

    Johnny

    Thursday, October 16, 2008 at 10:29 am #
  27. Kid Hat wrote::

    thank you for sharing

    Thursday, May 7, 2009 at 12:09 pm #
  28. Nathan wrote::

    Thank you VERY MUCH. I have been trying to get AMFPHP to work correctly using several tutorials, all with no luck, and I finally got this one to work!

    Friday, July 17, 2009 at 4:51 am #
  29. iguess wrote::

    How can I set remote info for local coldfusion server? An example would be great. Thanks

    Monday, August 22, 2011 at 6:39 am #
  30. Matt wrote::

    Ive got this perfectly implemented using a live gateway. However, for our development pages, we have the entire development site password protected. When I attempt to access the development gateway page, I get an error saying Flash cannot open the page. I know this has to do with the page authentication. Is there any sort of bypass on Flash’s end that will take care of this. With the Flash 8 MX package we dont have this issue, but we obviously cant use that package with an AS 3.0 swf.

    Monday, December 12, 2011 at 11:33 pm #

Trackbacks/Pingbacks (5)

  1. [...] check it [...]

     
  2. KinRou Blog » AS3 Netconnection wrapper for Flash Remoting on Friday, January 25, 2008 at 1:39 pm

    [...] http://www.bytearray.org/?p=122  [...]

     
  3. [...] We also found another remoting implementation in as3 which is also cool, but lacking resultsets, on bytearray.org « AsUnit modification for asynchronous testing The Log and LogEvent classes. [...]

     
  4. Color Hook » 一步一步之AMFPHP(3) on Tuesday, March 17, 2009 at 12:32 pm

    [...] Flex中有RemoteObject类来实现Remoting,这个类无法在Flash IDE中使用。要在Flash CS3中实现Remoting,我们可以借助bytearray.org上面提供的一些类来完成。你可以在这个地址中下载相关的类文件和范例: http://www.bytearray.org/?p=122 [...]

     
  5. [...] ByteArray.org By: Thibault Imbert [...]