•  
  •  

Use AMF with JavaScript in Adobe AIR

I’ve been working with Adobe Flex since its beta and have been a long time believer in using Action Message Format (AMF) as the communication protocol. I’ve also been working with Adobe AIR since its beta, but had only used AMF with Flex-based AIR applications. Until now…

I was working on a JavaScript-based AIR application (some refer to this as an AJAX-based application) recently where they wanted to use AMF, but didn’t want to hide a SWF in it to facilitate the AMF communication. Since there are other Flash Remoting gateways available, like openAMF and AMFPHP, it would be great if I could just use a JavaScript library to do the communication with those gateways.

If you look at examples in Flash that do this type of connection, you’ll notice that they use a NetConnection class. Guess what?! That class is also available in the JavaScript API for AIR. Yes, its that simple!

So with a simple refactoring of the same code from Flash, I was able to get my JavaScript-based AIR application to communicate with a Flash Remoting gateway. This example assumes you have installed a Flash Remoting gateway somewhere, that you replace [my_flash_remoting_gateway] with the gateway root and you’ve created a class with a method that you can invoke.

<html>
<head>
<title>JavaScript-based Flash Remoting</title>
<link href="sample.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="lib/air/AIRAliases.js"></script>
<script type="text/javascript" src="lib/air/AIRSourceViewer.js"></script>
<script type="text/javascript">
function doAMF()
{
var netConnection = new air.NetConnection();
netConnection.connect("http://[my_flash_remoting_gateway]");
var responder = new air.Responder(onComplete, onFail);
netConnection.call("HelloWorld", responder);
}
function onComplete(results)
{
alert( results.length );
}

function onFail(results)
{
alert(results)
}

</script>
</head>

<body>
<h3>Get data over AMFPHP</h3>

<ul>
<li>Has access to AIR APIs:
<input type="button" onclick="doAMF()" value="Make AMF call"/>
</li>
</ul>

</body>

</html>

Here is an example class in PHP:

<?php
class HelloWorld
{
function HelloWorld()
{
$this->methodTable = array
(
"say" => array
(
"access" => "remote",
"description" => "Pings back a message"
)
);
}
}
?>

About Rob Rusher

In his role as Principal Consultant for On3, Rob leads an software development practice to help his clients build rich Internet applications for the desktop, browser, and mobile devices, and to rapidly increase their knowledge and skills to better support their organization's goals. Rob is an Adobe Certified Expert, Community Professional, and Certified Instructor. He has taught and mentored the technical teams at HP, Overstock, Paychex, SAS, the FedEx, and other Government and Fortune 100 organizations. Rob has co-authored four best-selling books on building secure, cutting-edge and rapidly developed applications using Adobe AIR, ColdFusion and Flex. He is very active in organizing and speaking at RIA, Adobe LiveCycle, mobile conferences, and user groups. In addition to growing his software consulting practice, On3, Rob has been building expertise in rich client application development on a wider variety of devices and platforms that extend the applications to change the way we all create and live.