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:

12 comments so far

  1. mani October 13, 2010 04:41

    Excellent tutorial.. No article talks about the _connectionName and its benefits. Great find 🙂

  2. Ben February 22, 2011 18:39

    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?

  3. Jozef Chúťka February 23, 2011 22:06

    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

  4. […] 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 […]

  5. John January 9, 2012 11:51

    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.

  6. Justin Putney April 12, 2012 02:36

    Super helpful! Thanks!

  7. Ryan January 9, 2013 19:01

    Hi,

    thanks for your guide.

    I need to send a text from the SWF (Online) to a AIR desktop app. (Windows)
    Unfortunately I can’t get it to work.

    Can you help me with an example?
    Thanks
    Ryan

  8. Jozef Chúťka January 14, 2013 10:29

    hi Ryan, I guess you are talking about swf and air app running on the same machine. In that case you can use “SWF to AIR” instructions

  9. YopSolo June 11, 2013 21:35

    cool thx for the tut, it help me in AIR AIR

  10. Subramanya March 24, 2014 11:30

    Hi Sir,

    I have tried with the same coding methods but its not at all working fine with me .. Can you please help me out. Waiting for the reply…

    Thanks…

  11. Jozef Chúťka March 24, 2014 14:34

    hi Subramanya,
    could you please clarify what exactly does not work? what is the platform you work with / os / version etc.?

  12. Charles May 28, 2014 03:08

    Ok it is a fairly old thread, but since I spent quite some time resolving a LocalConnection issue, I’ll share.
    If one of your LocalConnection is running on Google Chrome using the Pepper plugin interface (PAPI), it will be sandboxed and won’t be able to communicate with other LocalConnection (with AIR app for instance).
    check this article from adobe :
    http://helpx.adobe.com/flash-player/kb/unsandbox-localconnection-chrome.html
    Anyway the whole LocalConnection stuff is behaving inconsistently, I ended up switching to Sockets instead.

Leave a comment

Please be polite and on topic. Your e-mail will never be published.