Friday, November 28, 2008

Finally figured out how to have Flash call Javascript in Facebook!

This took some doing. My goal was to have a Flash as3 movie (built in Flex) to communicate with Javascript on a Facebook canvas page. Here are the steps (quickly):

1. On the Facebook Canvas page insert a <fb:fbjs-bridge/> tag ABOVE your embedded flash movie (very important that it is ABOVE).
2. Insert the <fb:swf /> as indicated in the Facebook docs. Facebook will automatically pass a localConnection name via flashvars into the swf (the var is called "fb_local_connection").
3. In your flash movie get the localConnection name that Facebook passed in:
private var connectionName:String = Application.application.parameters.fb_local_connection;

4. Facebook exposes the Javascript that has been placed on a Canvas page by providing the function "callFBJS" in the bridge flash movie. This function requires two arguments:

callFBJS(myFunctionName:String,myArgs:Array)


5. With this info you can make the connection and call the javascript function:

connection.send(connectionName,"callFBJS",myFunctionName,myArgs);



Here is the sample code of a AS3 Flex app that call a js function on the canvas page called: showInvite();



<?xml version="1.0" encoding="utf-8"?>

<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute" xmlns:ns1="components.*" creationComplete="init();">

<mx:Script>

<![CDATA[

import flash.net.LocalConnection;

private var connection:LocalConnection = new LocalConnection();

private var connectionName:String = "";



private function init():void{

connectionName = Application.application.parameters.fb_local_connection;

}



private function callFBJS(methodName:String,args:Array):void {

if (connectionName!=""){

connection.allowDomain("apps.facebook.com", "apps.*.facebook.com");

connection.send(connectionName,"callFBJS",methodName,args);

}

}



private function showInvite():void{

var args:Array = new Array("arg1","arg2");

callFBJS("showInvite",args);

}

]]>

</mx:Script>

<mx:Button x="40" y="10" label="click" click="showInvite()"/>

</mx:Application>




For more info on this evolving drama (Facebook changes stuff all the time it appears): http://wiki.developers.facebook.com/index.php/Fb:fbjs-bridge

4 comments:

Unknown said...

thanks for this. i have been scratching my head hard. what have you done in javascript? do you have any code on that as i'm stuck there trying to what i have sent from the flex swf written into a feed story.

Brian said...

Hi Leon,
I'm not entirely satisfied with it, but I wound up deciding to use a dynamically created fb:share-button.

The way it works is this:

1. Set up the swf connection to a javascript function and pass the values that you want in the feed. In my case it was a video clip.
2. Have the javascript function make an ajax call to a php page that takes those values and prints an fb:share-button tag with them. I posted a bit about this in a recent post.
3. This fb:share-button tag is written into a div tag and displays the button just below the swf movie in a way that makes it look logical in the app.

{Please note that another more recent post shows that in IE when a user click the share button the pod causes the swf to disappear -- which I dealt with using another hack. It wasn't pretty but it actually works ok.)

I plan on posting all of this at some point but I would prefer to get the flex app calling Facebook directly, but that is another story and may take somewhat longer.

Fahim Akhter said...

what if javascript wants to call a flash function? I'm having trouble figuring that out.

Brian said...

Dear Fahim,

I haven't done any FB development for a while and a lot has changed since I did. I would recommend that you go here to get more info: http://developers.facebook.com/flash.php