webdu 2009 developer conference

Easy Flash Remoting Part One

Introduction

Author: Andrew Muller

ColdFusion MX includes Macromedia's new Flash Remoting, which allows for a simple connection and transfer of structured data between a Flash movie and the server without the need for complex coding.

Within the Flash movie ActionScript is used to connect and communicate with the remote ColdFusion server, this connection is made to either a ColdFusion template or ColdFusion Component (CFC) on the server.

In this article we will be constructing a Flash movie that will connect to a CFC on the server, calling a specific method.

The Basics - Establishing a Connection

A Flash movie that will connect to ColdFusion MX via Flash Remoting requires specific ActionScript to both enable and define the connection.

To be able to develop for Flash Remoting you will need to download and install the Flash Remoting components. This is a collection of ActionScript classes and debugging tools.

An ActionScript class, NetServices.as, is used to include the objects that will manage the connection to the server. This class calls functions on the server, passes parameters and handles the returned results. This must be included in the first line of code for the movie.

A second optional ActionScript class, NetDebug.as, is included with the Flash Remoting components. It allows the developer to view the debug information generated by the server.

This is an extremely useful tool during development and will be discussed later. References to NetDebug.as must be removed before the Flash movie is deployed.

Flash Remoting - NetServices ActionScript
Calling the ActionScript classes

The "#include" action is used to include the contents of a script.

Additional code is required in the Flash movie so that we may define the server gateway connection.

The gateway connection information includes the path of the server, an object to make the connection and a reference to the component or page on the server that you're connecting to.

Flash Remoting - Gateway
Calling the server gateway

The first line in the sample code above defines the gateway connection and sets it equal to a predetermined virtual directory for Flash Remoting service on the ColdFusion server.

The second line declares a variable equal to the service and it's location on the server that the Flash movie will connect to, this value is the path to the file, minus extension, relative to the web root using dot notation. "dm_FR.dmEmployeeData" in this example is therefore: webroot/dm_FR/dmEmployeeData.cfc.

Once the connection to the remote server has been established we can call a function, in the sample code below we're calling a function or method on the CFC called "getDepartments".

Flash Remoting - Calling the Server Function
Calling the server function

Here we have now the complete code, notice that the gateway connection is surrounded with a test to ensure that we initialise the connection only once.

Flash Remoting - Completed Code
Completed Flash ActionScript

Ctrl + Enter to test the movie from within Flash MX and you should expect to see the code run, producing a blank page and a debug window appearing to inform you that:

"NetServices info 1: getDepartments_Result was received from server: [object Object]"

GetDepartments_Result is the data that the function or method has returned from the CFC.

Easy Flash Remoting part two discusses working with the results returned to the Flash movie by the server.

Download the files for this article. (87kb)

Daemon now does Macromedia certified Flash MX training