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"
)
);
}
}
?>





What about a NetStream equivalent? I’ve been trying to find info about recording Microphone data, and every Flash example I’ve found attaches the NetConnection object to a NetStream object so you can send it to Flash Media Server.
NetStream doesn’t appear to be available according to the JavaScript Language Reference, do you know if there’s any way to record Microphone data either locally or send it to a server? I have an AMFPHP server available if it’s possible to send it there.
Answering myself a month or so later, apparently NetStream IS available in JavaScript AIR apps, even though it’s not part of the JS Language Reference.
Hope this formatting comes through:
var nc = new air.NetConnection();
nc.addEventListener(air.NetStatusEvent, function(e) {
if (e.info.code == ‘NetConnection.Connect.Success’) {
var ns = new runtime.flash.net.NetStream(nc);
ns.publish(‘myFilename’, ‘record’);
// don’t forget to close nc and ns when you’re done
}
});
nc.connect(‘rtmp://localhost/myService’);
what exactly can we do using amf.net
AMF provides better performance of data transmission between the client and server. It is a binary format that is sent over HTTP and it is native to the Flash Player. Your other alternatives are XML-based and ascii, so they are inherently slower.
You should check out this app by James Ward that demonstrates the capabilities of AMF vs other technologies. http://www.jamesward.com/census/
Hi, very nice post. Very usefull