Moving the session from Flex to AIR – PHP Version
Recently I was working to enable a common workflow for a Flex application in the browser and an AIR application. So basically your Flex application is working in a Browser, it does some stuff there and then you decide you want to move everything into an AIR app, let’s say because your user just decided in the middle of the workflow that he wants to switch to the AIR app that you provide.
So now the state of the application needs to move between Flex and AIR and it would be nice if you could move the server session from the browser application to AIR.
For this example I will use PHP as a server side technology but it will work very similar for other server technologies as well. Basically I want to:
- get the session ID/cookie in the Flex Application
- send it over to the AIR application
- use the session ID in the AIR application to make requests that will access the same session as in the browser
Getting the session ID in the Flex Application
First I need to get the the session ID from the Flex App. One simple way to do that is to just grab the whole cookies string. This can be done through the ExternalInterface mechanism. So I inject a little JavaScript function that gets the cookies string from the browser and then call it:
ExternalInterface.call('eval','window.cookieStr = function () {return document.cookie};') var cookieStr:String = ExternalInterface.call('cookieStr');
Communicating with the AIR App
To communicate with the AIR app I use LocalConnection. In the AIR app I register to an inbound connection:
var inbound:LocalConnection = new LocalConnection(); inbound.allowDomain('localhost'); inbound.client = new Object(); inbound.client.notifyMe = function (cookieStr:String):void { //add Code here to use the Cookie String } inbound.connect("session");
and in the Flex App I register to an outbound connection and call the notifyMe() function
outbound.connect("session"); outbound.send("app#phpSessionAIR:session","notifyMe",cookieStr); //outbound.send("app#phpSessionAIR.F0B3F68E1857B8A07069FED1D0638CAF200F76EB.1:session","notifyMe",cookieStr); outbound.close();
Please notice that when launching from Flex Builder, the AIR Application has no Publisher ID so the connection name is “app#phpSessionAIR:session”. After packaging and installation, the AIR app will get a Publisher ID and the connection name will be something like this: app#phpSessionAIR.F0B3F68E1857B8A07069FED1D0638CAF200F76EB.1:session
You can get the publisher ID of the installed AIR App by looking into META-INF/AIR/publisherid file within the application install directory.
Use the session id in the Air App
Now all that remains to be done is to use the cookies string when making the next request from the AIR app:
var myService:HTTPService = new HTTPService(); myService.url = "http://localhost/phpSessionFlex/sessionGet.php"; myService.headers.Cookie = cookieStr; myService.send();
“Please notice that when launching from Flex Builder, the AIR Application has no Publisher ID so the connection name is “app#phpSessionAIR:session”.”
That’s not actually true. You can set the publisherID in the run as / debug as dialogs, so you don’t have to change code between deployed and debugging versions of the application.
That’s right, missed that. I will update the post.
Thanks Gregor!
Wow, the way you inserted a js function into the HTML…WILD! I never knew you could do that!