An Introduction to Flash Remoting
By: Jorge Solis
The Flash side
The flash movie needs to create a connection to the service, call a method, then wait for a response from the server. The following is the code that will be needed in the Flash movie to make this happen. The code should be placed in the first frame of the main timeline.
1. Create the connection
#include "NetServices.as"
#include "NetDebug.as"
NetServices.setDefaultGatewayUrl("http://localhost/Hello/gateway.php");
conn = NetServices.createGatewayConnection();
In the first 2 lines we include two files that come with flash remoting and help to set up the connection and debug the result. The first line is a base library that helps to build the connection. The second line (NetDebug.as) is used by the debugger to show the connection status. You can open the NetConnection Debugger (menu Window, NetConnection Debugger, in Others Panels submenu under MX 2004) to see what happens when a request is made. After this we set the path to the default gateway for the application, this will be used for all of our services (for this connection). The last line is used to finally create the actual connection.
2. Create the response object
request = new Object(); //handles result
request.onResult = function(result){
show.text = result;
}
In the above code we create an object to handle the results returned from the Remote Call. Each call to a remote procedure triggers an onResult event in the movie. Here's a table with the order in which result functions/methods are called:
| Order | Result function/method called |
| 1 | The methodName_result() method of the object passed to the remote procedure when called |
| 2 | The onResult() method of an object passed to the Remote Procedure when called |
| 3 | The methodName_Result() method of the default object passed to the getService() method |
| 4 | The onResult method of the default object passed to the getService() method |
| 5 | If no response object was found, then the results are displayed in the Output window (in test movie only) |
In our example we're using the second one. If we want to use the first one, we need to use an echo_result function, and since we're not passing any object in the getService method as a second parameter, we can't use method 3 or 4. Our request object receive's a result parameter (if you take a look at our service definition, you can will see that it returns the same parameter that we pass it) and simply show the result in a textfield named show.
3. Call the service
We need to put a dynamic textfield on the stage with instance name of show and two simple buttons with instance name's of callButton and clearButton, respectively. Here's our final example:
service = conn.getService("HelloWorld");
callButton.onPress = function(){
service.makeEcho(request, "Hello World!");
}
clearButton.onPress = function(){
show.text = "";
}
Lastly, we connect to the service (remember the file, class, and main function have the same name) and then we set the onPress event handler of the callService button, where we call the makeEcho method. We pass a request object that we have defined earlier to handle the answer and a parameter, a string. The clear button will simply clear the textfield. This is not very useful, of course, but illustrates the method of calling remote procedures and let us be on the way.
Hello World Remoting !! Now you can get started with flash remoting with amfphp. We should have some new tutorials on this up soon.

5 most recent
Flash button as Flex icon
Tree menu
Flash Spell Checker
Flash Remoting Library
MX 2004 Chart/Poll
Articles