The Ultimate Guide To LocalConnection – SWF to SWF to AIR to AIR to SWF
Even if it stands in documentstion (some crucial things mentioned in one line somewhere), it was really tricky thing to figure out how to make LocalConnection-s work between different application runtimes (flash player / air). It got even more tangled with empty flash player error saying “Error #2044: Unhandled StatusEvent:. level=error, code=”. Basically, there is a generic method/form, with underscore prefix for connection name, that you can successfully use in all cases, but if your application requires higher security you should define your connections more exactly.
Based on your applications origin (AIR / flash player) you may define a few types of communication nature:
- SWF to SWF on a same domain
- SWF to SWF on different domains
- AIR to SWF
- SWF to AIR
- AIR to AIR
Let’s suppose, we use the following variables and client definition for receiver:
var localConnection:LocalConnection = new LocalConnection(); // both sender and receiver localConnection.client = someInstanceWithPublicMethods // receiver only
SWF to SWF on a same domain
This one is pretty common and simple situation:
// receiver
localConnection.connect("connectionName");
// sender
localConnection.send("connectionName", "methodName");
SWF to SWF on different domains
Based on apps nature – for generic/unknown domains you can use super generic form (works for every combination domain/swf/air). Notice the underscore character “_” with connection name:
// SWF receiver on unknown domain
localConnection.allowDomain("*");
localConnection.connect("_connectionName");
// SWF sender on unknown domain
sendingLC.send("_connectionName", "methodName");
… or more secure version, where you define exactly the domain you are targeting:
// SWF receiver on domain1.com
localConnection.allowDomain("domain2.com");
localConnection.connect("connectionName");
// SWF sender on domain2.com
localConnection.send("domain1.com:connectionName", "methodName");
AIR to SWF
Now its getting tricky, please focus. Air app calls method on swf on some exact domain:
// SWF receiver on domain1.com
localConnection.allowDomain("app#SENDER*AIR*ID"); // e.g. "app#sk.yoz.air.SenderApp"
localConnection.connect("connectionName");
// AIR sender
localConnection.send("domain1.com:connectionName", "methodName");
… or the same super generic method mentioned earlier for unknown/generic receiver swf file location
// SWF receiver on unknown domain
localConnection.allowDomain("*");
localConnection.connect("_connectionName");
// AIR sender
localConnection.send("_connectionName", "methodName");
SWF to AIR
… now the opposite standing air receiver and swf sender:
// AIR receiver
localConnection.allowDomain("domain1.com");
localConnection.connect("connectionName");
// SWF sender on domain1.com
localConnection.send("app#RECEIVER*AIR*ID:connectionName", "methodName");
// e.g. "app#sk.yoz.air.ReceiverApp:connectionName"
… or use super generic form:
// AIR receiver
localConnection.allowDomain("domain1.com");
localConnection.connect("_connectionName");
// SWF sender on domain1.com
localConnection.send("_connectionName", "methodName");
AIR to AIR
This one is hefty, make sure to substitute with correct ids:
// AIR receiver (sk.yoz.air.ReceiverApp)
localConnection.allowDomain("app#SENDER*AIR*ID"); // e.g. "app#sk.yoz.air.SenderApp"
localConnection.allowDomain("localhost"); // may help with local testing
localConnection.connect("connectionName");
// AIR sender (sk.yoz.air.SenderApp)
localConnection.send("app#RECEIVER*AIR*ID:connectionName", "methodName");
// e.g. "app#sk.yoz.air.ReceiverApp:connectionName"
… or once again the generic form, now possible even without allowDomain()
// AIR receiver
localConnection.connect("_connectionName");
// AIR sender
localConnection.send("_connectionName", "methodName");
Where to go from here:
Excellent tutorial.. No article talks about the _connectionName and its benefits. Great find
This is a great post on how to use LocalConnection. I have been struggling with getting the communications from an AIR app to a SWF. I’ve tried the least secure (or supuer generic) method first just to get things rolling. I keep getting “error” returned on the status event. I’m currently running the SWF from the local file system.
Any idea on what else I might be missing?
Ben what is your OS and flash player and air versions? Make sure to have the latest, there has been some issues reported with fp10 in macs. If this is not your case:
1. make sure your LocalConnection is connect()ed before you call send() … run the receiving app first
2. if does not help, debug it first from your local swf-swf, air-air, swf-air … make those tests and let me know what fails
[...] you can use LocalConnection to communicate between the videoplayer and AIR app. To pass the video info, for example, and most importantly to let the AIR application know when remove the HTML component. Refer to The Ultimate Guide To LocalConnection [...]
Just have to say that over the last 6 months you have saved my bacon twice.
The facebook connection class worked like a dream.
Now this, this puts the tin hat on it. Saved my ass again.. couldn’t find a decent explanation anywhere
All hail Josef…and keep up the excellent short posts..
Thank you very much once again.