I needed to get A Flex application to have a 2 way communication to an embedded Flex app. The examples I found on the interweb were either 1 way or focused on Flash. Also, some of the samples on the net kept giving me this error –

Error #2082: Connect failed because the object is already connected.

Here’s the basics:

Sending a message is pretty easy. I’ve instantiated a LocalConnection called connection. To send messages, I call the send method. The arguments are the connection name, the method in the child swf that will be invoked, and the data to pass.

// receivedParentPingMethod is a function defined in the Child swf
connection.send("fromParent", "receivedParentPingMethod", txtSend.text);

To receive stuff from the child, you need to do 2 things:

//open a connection and listen to it

Create a function on the parent

//define a function that will be called from the child swf
// receivedChildPingMethod will get called from the Child
public function receivedChildPingMethod(msg:String):void {
        txtResult.text += "Received ping from child:\n"+msg+"\n\n";

You then do the same for the child. This is the caveat however. You can’t listen on the same connection for the child because that will throw said error. You’ll have to create a new connection and listen in on that. Something like this:

// listen to events from the parent

This part wasn’t obvious to me and was throwing me off.

Anywho, here’s the project. Enjoy.

Popularity: 13% [?]

Related posts:

  1. Flex E4X XML Filtering