How to communicate between multiple Flash files with LocalConnection

Published on Thu 07 Aug 2008 by Will


Have you ever come across a website containing banner ads of the same creative which appear to be in sync? Here I’m going to demonstrate how this is done by giving an example of how to use the LocalConnection class in Actionscript 3.0.

What is a local connection?

A local connection is used to communicate between two or more currently running SWF files. These can be either opened in Flash Player or, more commonly, embedded in a web page. A local connection object can be used to facilitate the communication between SWFs by enabling one SWF to call a function in another SWF.

A common place local connections are used is in banner advertising. An advertiser will buy out multiple ad positions on the same web page and create an advertisement that will utilize both spaces for greater impact and viewer retention.

A good use of this was the recent ‘Mac vs PC’ ads that did the rounds late last year. This ad featured our befuddled PC character attempting to light up a sign promoting Windows Vista featured in the above banner.

Mac Vs. PC

A local connection is important in this instance as both banners need to be in sync. Eveytime PC hit’s the button the lower SWF is calling a function in the upper SWF via a local connection object.

So how do you create a local connection?

Creating a local connection between SWFs is pretty easy to accomplish. Here I’m going to show a very simple local connection. This will feature 2 SWF files, where one SWF will call a function in the other.

First let’s create 2 new Flash documents called Receiver and Sender.

As the name implies Receiver will be our receiving SWF. This will contain a function which will be invoked by a user interaction in the Sender SWF. In my example below I have created a MovieClip on the timeline called ‘light’ which will play when triggered.

On frame 1 of our Receiver document, paste the following code.

import flash.net.LocalConnection;

var receiverLC:LocalConnection = new LocalConnection()
receiverLC.connect("_myConnection");
receiverLC.client = this;

function playMC():void {
    light.play();
}

As you can see in the above code, we are first instantiating a new LocalConnection object and assigning it to the variable receiverLC.

We are then calling the connect method, passing in the connection string “_myConnection”. This string can be anything of your choosing however it is good practice to prefix this with an underscore (to find out why check out LocalConnection on Adobe LiveDocs).

The client property of our LocalConnection object needs to point to the object containing the function(s) we want to call. For the sake of simplicity we are using the ‘this’ keyword as our function exists in the same scope as our connection.

In this example the method we will be calling is playMC which in turn calls the play method the MovieClip object called ‘light’.

Next, on frame one of our Sender document, paste the following code.

import flash.net.LocalConnection;
import flash.events.MouseEvent;

var receiverLC:LocalConnection = new LocalConnection();
stage.addEventListener(MouseEvent.MOUSE_DOWN, sendData);

function sendData(e:MouseEvent):void {
 receiverLC.send("_myConnection", "playMC");
};

Like our receiver, we need to instantiate a LocalConnection, this time assigned to the variable senderLC.

We then create an event listener which will call the sendData function when the user clicks anywhere inside this SWF. Inside this sendData function we call the send method of our LocalConnection object. To this we pass in 2 strings, our connection string and and the name of the function we wish to call. Note that the connection string, “_myConnection”, is the same as what we used in our Receiver file.

That’s it! If you publish both the Receiver and Sender documents and open them both with Flash Player you should have a working local connection. Try out my version below.

This content requires Flash.

This content requires Flash.

It is important to note that this is an example of using LocalConnection in it’s simplest form and is only communicating in one direction. For a more advanced example check out “Sending data with LocalConnection in Flash“.

To learn more about LocalConnection check out the Local Connection page on Adobe LiveDocs.

Did you enjoy this post?

Why not subscribe via RSS or follow Ahrooga on Twitter for alerts on new posts :)

You may also like:

Comments

ganesh - 02 October, 2010

you tutorial is simple but so good it gives me an idea for doing something new

Arul - 22 March, 2011

hi thanks for your information.. I am in the same situation but i want to send NetConnection object to another swf... how can get it..? FYI my question link in stackoverflow http://stackoverflow.com/questions/5218580/publish-in-one-swf-and-play-in-another-swf-with-in-same-page

fabian rios - 24 July, 2011

hi thanks for the info, any ideas why it doesn't work if i publish .exe instead of .swf ?

Ed - 18 August, 2011

Unfortunately you may try to open this page twice and light will not be turned on/off by clicking on the place. Just because LocalConnection's, first - are buggy, second - works in context of the plugin and not a page or a tab.
Conclusion is simple - use LC is you just want to play with this feature otherwise it will bit you hard in production.

Tom - 07 November, 2011

Undependable is an understatement! It works almost perfect on your local machine, but online, I am lucky to get them to communicate 2 in every 10 tries. Same domain, same page, nothing else on the page but the two Very Basic files. I got real excited to see two work together locally, but scrapped the idea after trying it online. Thanks Ed, for the heads up.

Dave - 21 January, 2012

Any idea how to limit the local connection to only the same browser window?