fcOnTheWeb Logo Web technologies, made easy.

fcOnTheWeb Red DotPassing data between two SWFs - Local Connection

Today we are going to take a look at a topic that is quite interesting - Local Connection.

Local Connection is really cool because it allows two (or more) separate SWFs on the same page to communicate between them!

As always, we have got this tutorial in both ActionScript 2 and ActionScript 3, so choose your poison and we will move on.

ActionScript 2

So, Local Connection allows to SWF files on the same page to communicate. While this communication can be two-way, for the sake of simplicity we are just going to look at one way communication today. We will give a brief insight into how to set up two-way communication at the end.

So we need two SWF files - a sender and a receiver. Unsurprisingly, the sender will send the data on the Local Connection, and the receiver will receive the data.

For the purposes of this example we will simply send some data - "fcOnTheWeb.com" - on a button press.

Let's take a look at this in action. On the top is the sender SWF with the button. Below is the receiver SWF. When the "Send" button is pressed on the sender SWF it will send "fcOnTheWeb.com" across the Local Connection which will be received and displayed by the receiver SWF below it. Try it now:

Your version of Flash Player is lower than the required 10.0.0.

Click below to upgrade to the latest version and then try again.

Get Adobe Flash player

Your version of Flash Player is lower than the required 10.0.0.

Click below to upgrade to the latest version and then try again.

Get Adobe Flash player

Now that we've seen how it functions, let us take a look at the code necessary to get this working. First, the sender SWF:

stop();

sending_lc = new LocalConnection();

my_btn.onRelease = function() {

sending_lc.send("my_lc_as2", "execute_this", "fcOnTheWeb.com");

}

So this code is quite simple. After stopping the frame (which isn't really necessary) we declare a new Local Connection. We declare this as sending_lc.

Next we simply set up the button code - our button is called my_btn and we set it up for the onRelease event.

On the onRelease event is where the magic happens. We tell our Local Connection variable to send (sending_lc.send) and we pass in the necessary elements. First the Local Connection name, which we have chosen as my_lc_as2. Next is the function we will call in the receiver SWF - in this case it is execute_this. And lastly is the data we want to transfer - in this case "fcOnTheWeb.com". If we wanted to pass more than one element, we could add them all here simply separating them with a comma.

And lastly we close the function.

That's all the code we need on the sender SWF. Now let's take a look at the receiver SWF:

stop();

receiving_lc = new LocalConnection();

receiving_lc.execute_this = function(new_text) {

my_text.text = new_text;

}

receiving_lc.connect("my_lc_as2");

The first thing we do here is to again stop the SWF.

Next we set up a new Local Connection variable, this time for receiving the data. We have called ours receiving_lc.

With that set up, we declare our function that will be called when the data is sent through. This needs the same name as we entered into the sender SWF - in our case execute_this. When the data is received this is the function that will be executed. This function also needs to be ready to accept all the data passed into it. In our example we are only passing through one piece of data, but if we were to pass through three or four we would need the correct number of variables in the function declaration to receive everything.

Inside the function we are simply assigning the text we received to a text box. Nothing fancy here.

And lastly we need to connect our Local Connection variable to the Local Connection we named on the sender - in this case my_lc_as2.

And that's all there is to it. Some very simple code to open up a very useful and flexible function.

To get two way communication working, you would need to put both the receiver and sender code in both SWFs and make sure you name everything appropriately. It is a good idea to use a different Local Connection name for each direction so things run more smoothly - so that would be one Local Connection for sending one way, and another Local Connection for sending the other way (that is the my_lc_as2 part).

You can download the FLAs used to make this tutorial below so you can see this code in action if you want:

local_connection_as2.zip

ActionScript 3

So, Local Connection allows to SWF files on the same page to communicate. While this communication can be two-way, for the sake of simplicity we are just going to look at one way communication today. We will give a brief insight into how to set up two-way communication at the end.

So we need two SWF files - a sender and a receiver. Unsurprisingly, the sender will send the data on the Local Connection, and the receiver will receive the data.

For the purposes of this example we will simply send some data - "fcOnTheWeb.com" - on a button press.

Let's take a look at this in action. On the top is the sender SWF with the button. Below is the receiver SWF. When the "Send" button is pressed on the sender SWF it will send "fcOnTheWeb.com" across the Local Connection which will be received and displayed by the receiver SWF below it. Try it now:

Your version of Flash Player is lower than the required 10.0.0.

Click below to upgrade to the latest version and then try again.

Get Adobe Flash player

Your version of Flash Player is lower than the required 10.0.0.

Click below to upgrade to the latest version and then try again.

Get Adobe Flash player

Now that we've seen how it functions, let us take a look at the code necessary to get this working. First, the sender SWF:

stop();

var sending_lc:LocalConnection;

sending_lc = new LocalConnection();

function send_it(evt:MouseEvent):void {

sending_lc.send("my_lc_as3", "execute_this", "fcOnTheWeb.com");

}

my_btn.addEventListener(MouseEvent.MOUSE_UP, send_it);

So this code is quite simple. After stopping the frame (which isn't really necessary) we declare a new Local Connection. We declare this as sending_lc.

Next, we set up the function that will be executed on the button press - our function is called send_it.

In the function is where the magic happens. We tell our Local Connection variable to send (sending_lc.send) and we pass in the necessary elements. First the Local Connection name, which we have chosen as my_lc_as3. Next is the function we will call in the receiver SWF - in this case it is execute_this. And lastly is the data we want to transfer - in this case "fcOnTheWeb.com". If we wanted to pass more than one element, we could add them all here simply separating them with a comma.

After we close the function, we set up the button event. We tell our button (my_btn to call our send_it function on the MOUSE_UP event.

That's all the code we need on the sender SWF. Now let's take a look at the receiver SWF:

stop();

var receiving_lc:LocalConnection;

receiving_lc = new LocalConnection();

receiving_lc.connect("my_lc_as3");

receiving_lc.client = this;

function execute_this(new_text:String):void {

my_text.text = new_text;

}

The first thing we do here is to again stop the SWF.

Next we set up a new Local Connection variable, this time for receiving the data. We have called ours receiving_lc.

Next we connect our Local Connection variable to our local connection - my_lc_as3.

Then we declare the client for our Local Connection variable - this declares where the callback functions are invoked.

With that set up, we declare our function that will be called when the data is sent through. This needs the same name as we entered into the sender SWF - in our case execute_this. When the data is received this is the function that will be executed. This function also needs to be ready to accept all the data passed into it. In our example we are only passing through one piece of data, but if we were to pass through three or four we would need the correct number of variables in the function declaration to receive everything.

Inside the function we are simply assigning the text we received to a text box. Nothing fancy here.

And that's all there is to it. Some very simple code to open up a very useful and flexible function.

To get two way communication working, you would need to put both the receiver and sender code in both SWFs and make sure you name everything appropriately. It is a good idea to use a different Local Connection name for each direction so things run more smoothly - so that would be one Local Connection for sending one way, and another Local Connection for sending the other way (that is the my_lc_as2 part).

You can download the FLAs used to make this tutorial below so you can see this code in action if you want:

local_connection_as3.zip

ferrari_chris

Omar Muhammad (http://www.blueGravityProd.com) says:

2009-07-22 00:51:18

Works great - thanks for sharing!

__________

Addis Abebayehu says:

2010-04-15 02:01:42

this is excellect. I have a question though. I have a separate videoplaylist swf and a separate player swf. I want to connect the two swfs. How do i do this? How is the mouse event and the video url be sent to the player. Would you please help me. Thank you!

__________

Chris (http://www.fcOnTheWeb.com) says:

2010-04-16 06:22:11

If you can't set an event (such as a mouse click) and you want the transfer to happen when the SWF is loaded, try adding some frames to the timeline then placing the 'send' code on a later frame.

You might need to practice a bit with time delays though, to allow time for both SWFs to load properly.

__________

David says:

2010-05-14 20:08:46

I downloaded the files, unzipped them and received the following message.

Failed to open document...

Oh well. I'll just recreate the as3 files.

__________

Chris (http://www.fcOnTheWeb.com) says:

2010-05-16 01:23:56

It works OK from here.

I'm using Flash CS4 on Windows Vista. What version are you using?

__________

Sagar says:

2010-06-28 08:07:39

Hi Chris,

Thanks for sharing. I have one query, how can we pass data when reciever is loaded in the sender swf and how will the client be named?

Thanks & Regards,
Sagar

__________

Sagar says:

2010-06-28 08:09:49

I am talking about as3 code! Thanks!

__________

Chris (http://www.fcOnTheWeb.com) says:

2010-06-29 09:57:22

Sagar,

If the receiver SWF is loaded inside the sender, you don't need to use LocalConnection for that - both SWFs will be running in the same Flash Player instance.

You will be able to access variables and pass data as per normal, just following the heirarchy rules.

The parent and child SWF will be able to pass data between themselves easily.

I haven't written an article on this yet, but it's something to look into.

Thanks.

__________

Adam says:

2010-10-09 01:59:59

Do you know how to do this if the sender is in an iframe and the receive is on the parent page? I tried the code you have but it's not working in this case.

__________

Chris (http://www.fcOnTheWeb.com) says:

2010-10-09 02:22:48

I don't know if that's possible as the two SWFs are on separate pages, and an iFrame is like a second browser window embedded inside the first one.

If you've followed the code above and it works when the SWFs are on one page but not when split into an iFrame, then I'd suggest it can't be done, unfortunately.

__________

mado says:

2011-01-03 10:53:43

Hi Chris,
Thanks for sharing,its a great tut.
but can I do the same if the 2 swfs on different computers but have a local connection Using AS3??

__________

Chris (http://www.fcOnTheWeb.com) says:

2011-01-13 08:39:50

mado: By two computers, I assume you mean two different domains? That's an interesting idea, and something I'll look into. Thanks.

__________

TedSki says:

2011-01-14 23:11:18

I'm interested to know how the following works:

sending_lc.send("my_lc_as2", "execute_this", "fcOnTheWeb.com");


receiving_lc.execute_this = function(new_text)

In my experience, the data being sent has to match the data being received, so I'm not certain how sending "fcOnTheWeb.com" and receiving new_text works, as the URL hasn't been defined as new_text; so where does the receiving SWF pull the new_text variable from?

__________

Chris (http://www.fcOnTheWeb.com) says:

2011-01-17 06:31:30

TedSki, only the third parameter is actually passed through - the first parameter declares which connection to use, the second declares which method to call on the receiving end, then the third parameter actually calls the data.

While you are right in part - the data sent does sometimes need to match the data received - it's not so in this case as the first two parameters are relevant to sending the data, rather than the actual data.

Hope that helps.

__________

Arul (http://arulraj.net) says:

2011-03-21 17:29:46

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

__________

antrish says:

2011-12-10 06:15:44

hi,
i have 2 exe's,
1-player and other is callData, in player on button click i want to launch callData.exe and after it open i want to send data from player to callData.
Thanks

__________

Chris (http://www.fcOnTheWeb.com) says:

2011-12-12 06:57:23

Hi Antrish.

I don't think the functionality you're after can be achieved with Local Connection. As far as I know, it only applies to communication in the browser.

You could achieve what you want with Adobe AIR perhaps?

__________

Abhi says:

2012-04-05 07:21:01

It was really helpfull.

__________

Abdulraheem (http://designpro-kw.com) says:

2012-06-14 23:58:49

Very clear and works great .. Thnx man ..

__________

Aleathab says:

2012-12-29 03:09:19

Thanks! This was exactly what I needed =)

__________

null says:

2013-01-05 23:10:42

Thanks so much man, I can't believe it was this easy.

__________

help says:

2013-08-07 05:42:18

hey, how do you do the same with adding movieclips instead of texts?

__________

Cheap Ralph Lauren UK (http://cheapralphlaurenuk.1minutesite.co.uk/) says:

2014-01-18 03:01:38

Gucci Shoes UK, http://www.guccivshoesfactory.net/
Polo Ralph Lauren Outlet Online, http://www.rralphlaurenoutlet.com/
Ralph Lauren Outlet Online, http://www.ralphlaurenipolo.com/
Ralph Lauren UK, http://www.polo-outletstoreonline.com/
Beats by Dre, http://www.discountbeatsbydresale.com/
Sacs Longchamp Pairs, http://www.saclongchamppairs.com/
Canada Goose Outlet, http://www.thecanadagooseoutlet.com/
Hollister UK Shops, http://www.cheaphollistersale.co.uk/
Michael Kors Outlet Online, http://www.michaelkors.so/
Marc Jacobs Outlet, http://www.marcjacobsonsale.com/
Michael Kors Outlet, http://www.michaelkorsr.com/
North Jackets Clearance, http://www.north-faceoutletonlines.net/
Burberry Outlet Online, http://www.burbagssale2013.com/
North Clearance Outlet Online, http://www.north-clearance.com/
Coach Factory, http://www.coach-factories.com/
Coach Outlet Online, http://www.coach-purseonsale.com/
Cheap Ralph Lauren UK, http://cheapralphlaurenuk.1minutesite.co.uk/
http://marcjacobsoutlet-2014.tripod.com/
http://www.michaelkorsoutletr.com/
Michael Kors Outlet
Discount Michael Kors
http://www.michael-korsfactorystore.com/
Michael Kors Outlet
Michael Kors Factory Store
http://www.michaellkorsfactoryonline.com/
Michael Kors Outlet
Discount micheal kors

__________

Robert Hannan says:

2014-05-12 16:37:51

How would you modify the code to pass a variable which will change the visibile to another option, for example on the receiver file you have a movie clip that is called myMovie_mc and the code is as follows:

myMovie_mc.visible = false;

if {
myMovie_mc.visible = true;
}

how would you create the variable in both the receiver and sender code to send the right variable to set the visibility to true?

__________

oakley sunglasses cheap (http://www.oakleysunglassescheap.us.com/) says:

2014-07-12 07:05:26

http://www.oakleysunglassescheap.us.com/ oakley sunglasses

cheap
http://www.coachoutletstoreonlineco.com/ coach factory outlet
http://www.abercrombie-andfitch.com/ abercrombie and fitch
http://www.michaelkors-outletsonline.com/ michael kors outlet
http://www.oakley-sunglassesoutlet.com/ oakley sunglasses

outlet
http://www.tory-burchoutlet.com/ tory burch outlet
http://www.abercrombieandfitchstore.com/ abercrombie & fitch
http://www.rayban--sunglasses.com/ ray ban sunglasses
http://www.fit-flops.co.uk/ fitflops
http://www.coachoutlet-factoryonline.com/ coach factory outlet
http://www.toryburch-shoes.com/ tory burch shoes
http://www.coachstoreonline.org/ coach outlet store online
http://www.coachfactory-outletinc.com/ coach factory outlet
http://www.cheapraybansunglasses.us/ cheap ray ban sunglasses
http://www.valentino.us.com/ valentino
http://www.fitfloponline.co.uk/ fitflop uk
http://www.coach-outletinc.com/ coach outlet
http://www.coachoutlet-factorystore.com/ coach outlet store

online
http://www.airjordan.us.com/ air jordan
http://www.timberlandshoesus.com/ timberland shoes
http://www.michaelkorsoutlet-online.us/ michael kors outlet
http://www.coachfactoryoutletnew.com/ coach factory outlet
http://www.coach-factoryoutletinc.com/ coach factory outlet
http://www.truereligionbrandjeans.us.com/ true religion outlet
http://www.truereligionjeans.us.com/ true religion jeans
http://www.coach--handbags.com/ coach outlet online
http://www.louisvuitton--outlet.us/ louis vuitton outlet
http://www.timberland--shoes.com/ timberland boots
http://www.isabelmarantshoes.us.com/ isabel marant shoes
http://www.coachoutletonline-factory.com/ coach outlet online
http://www.michaelkorsoutlet-online.net/ michael kors purses
http://www.louisvuitton-handbag.us/ louis vuitton handbags
http://www.coach-outletstoreonline.com/ coach outlet store

online
http://www.true-religion-jeans.com/ true religion jeans
http://www.coach-outletfactoryonline.com/ coach factory outlet

online
http://www.officialcoachfactoryoutlet.com/ coach factory outlet
http://www.louisvuitton-outlet.org/ louis vuitton outlet
http://www.coach-factoryoutlets.com/ coach factory outlet
http://www.coach-outletonline.us/ coach outlet online
http://www.michaelkorsoutletonlinestore.us/ michael kors outlet
http://www.michaelkorsoutletstoresco.com/ michael kors outlet

store
http://www.tiffanysjewelry-co.com/ tiffany and co jewelry
http://www.celineoutlet-online.com/ celine outlet
http://www.cheap-toms.net/ toms shoes
http://www.celine-handbags.com/ celine handbags
http://www.toms--shoes.us/ toms outlet
http://www.fitflopsandalssale.com/ fitflops
http://www.fitflopusaonline.com/ fitflop outlet
http://www.prada-handbag.com/ prada handbags
http://www.tiffanyandcoinc.com/ tiffany jewelry
http://www.prada-outlet.org/ prada outlet
http://www.michael-korsoutletinc.com/ michael kors
http://www.marcjacobs.us.com/ marc jacobs
http://www.supra.us.com/ supra shoes
http://www.ralphlauren.us.com/ polo ralph lauren
http://www.tiffanyjewelry.us.com/ tiffany jewelry
http://www.shoestoms.net/ toms shoes

__________

POLO says:

2014-08-04 07:56:47

http://www.superbagsmarket.com/ Super Bags Market
http://www.nike-jordanshoes.com/ Nike Jordan Shoes
http://www.thebeatsbydre.net/ Beats By Dre Outlet
http://www.michaelusakors.com/ Michael Kors Outlet Online
http://www.polo-tshirts.com/ Ralph Polo Outlet Online
http://www.northsclearance.com/ North Clearace Outlet
http://www.polo-ralphlaurenonline.com/ Ralph Polo Outlet
http://www.guccishoesuk-2014.net/ Gucci Shoes Outlet
http://www.michaelkorsstates.com/ Michael Kors Outlet
http://www.polooutlet-store.com/ Polo Outlet Store
http://www.ralph-laurenhome.com/ Ralph Lauren UK
http://www.marcjacobsonsale.com/ Marc Jacobs On Sale
http://www.mcmworldwides.com/ MCM Outlet
http://www.salongchamppairs.com/ Sac Longchamp Pairs
http://www.canada-gooser.com/ Canada Goose Outlet
http://www.burberrybags2014.com/ Burberry Outlet
http://www.michaelkors.so/ Michael Kors Outlet
http://www.oakley-sunglassfactory.com/ Oakley Sunglaases Factory
http://www.north-faceoutlet.us.com/ North Face Outlet Online
http://www.moncler-clearance.com/ Moncler Clearance
http://www.woolrich-clearance.com/ Woolrich Clearance
http://www.barbour-jacketsoutlet.com/ Barbour Jackets Outlet Online
http://www.moncler-jacketsoutletonline.com/ Moncler Jackets Outlet Online
http://www.monsterbeatsbydres.net/ Monster Beats Outlet
http://www.louis-vuittonblackfriday.com/ Louis Vuitton Outlet
http://www.lv-guccishoesfactory.com/ Louis Vuitton Factory
http://www.mcmoutlet-jp.com/ MCM 店铺
http://www.cheapdiscountoutlet.com/ Discount Clothes Sale
http://www.michael-korsfactoryonline.com/ Michael Kors Factory Online
http://www.coach-factoryoutlets.net/ Coach Factory Outlet
http://www.coach-blackfriday.net/ Coach Black Friday
http://www.coach-storeoutletonline.com/ Coach Store Outlet Online
http://www.coach-factoryoutletonlines.com/ Coach Factory Outlet Online
http://www.coachcoachoutlet.net/ Coach Outlet
http://www.coach-factory.net/ Coach Factory
http://www.coach-pursesoutlet.net/ Coach Purses Outlet Online
http://www.coach-outletonline.net/ Coach Outlet USA
http://www.mksfactoryoutlet.com/ Michael Kors Factory Outlet
http://www.zxcoachoutlet.com/ Coach Outlet Online USA
http://www.mischristmas.com/ Coach Factory Outlet Online
http://www.misblackfriday.com/ Black Friday
http://www.newoutletonlinemall.com/ Coach Purses Outlet Online
http://www.clickmichaelkors.com/ Michael Kors USA
http://www.cmichaelkorsoutlet.com/ Michael Kors Outlet USA
http://www.ralphlaurenepolo.com/ Ralph Lauren Polo

__________

hollister clothing store (http://www.rayban--sunglass.com/) says:

2014-08-05 07:26:07

http://www.rayban--sunglass.com/ ray ban sunglasses
http://www.customscopyright.com/ coach factory outlet
http://www.hollisterclothing-store.com/ hollister clothing store
http://www.michael--korsoutletonline.com/ michael kors outlet online
http://www.oakley--sunglass.com/ oakley sunglasses cheap
http://www.saclancelfrance.com/ sac lancel
http://www.katespade--outlet.com/ kate spade outlet
http://www.katespadeoutlet-online.com/ kate spade outlet online
http://www.katespade-online.com/ kate spade online
http://www.coachoutlet-onlinestore.com/ coach factory outlet
http://www.abercrombie-andfitch.com/ abercrombie and fitch
http://www.michaelkors-outletsonline.com/ michael kors outlet
http://www.sacportefeuille.com/ sac portefeuille
http://www.oakley-sunglassesoutlet.com/ oakley sunglasses outlet
http://www.michael--korsoutlet.com/ michael kors outlet
http://www.toryburch-outlet-online.com/ tory burch outlet online
http://www.abercrombieandfitchstore.com/ abercrombie & fitch
http://www.fit-flops.co.uk/ fitflops
http://www.coachoutlet-onlinefactory.com/ coach outlet online
http://www.coachfactoryoutlet-bag.com/ coach factory outlet
http://www.tory-burchshoes.com/ tory burch shoes
http://www.coachfactoryoutletclearance.com/ coach factory outlet
http://www.raybansunglasses-cheap.com/ cheap ray ban sunglasses
http://www.valentino.us.com/ valentino
http://www.fitfloponline.co.uk/ fitflop uk
http://www.coach-outletonlinefactory.com/ coach outlet
http://www.coachoutlet-factorystore.com/ coach outlet store online
http://www.air--jordans.com/ air jordan
http://www.timberlandshoesus.com/ timberland shoes
http://www.michaelkors--handbags.com/ michael kors handbags
http://www.coachfactoryoutletnew.com/ coach factory outlet
http://www.coachfactory-onlineoutlet.com/ coach factory outlet
http://www.truereligionbrandjeans.us.com/ true religion outlet
http://www.truereligionjeans.us.com/ true religion jeans
http://www.coach--handbags.com/ coach outlet online
http://www.louisvuitton--outlet.us/ louis vuitton outlet
http://www.timberland--shoes.com/ timberland boots
http://www.isabelmarantshoes.us.com/ isabel marant shoes
http://www.coachoutletonline-factory.com/ coach outlet online
http://www.michael-korsonlineoutlet.com/ michael kors outlet online
http://www.louisvuitton-handbag.us/ louis vuitton handbags
http://www.coach-outletstoreonline.com/ coach outlet store online
http://www.true-religion-jeans.com/ true religion jeans
http://www.coach-outletfactoryonline.com/ coach factory outlet online
http://www.coachfactoryoutlet-official.com/ coach factory outlet
http://www.louis-vuittonoutlets.com/ louis vuitton outlet
http://www.officialcoachonlinefactorysale.com/ official coach factory outlet
http://www.coachoutlet-storefactory.com/ coach outlet online
http://www.michael-korsoutletsonline.com/ michael kors outlet
http://www.tiffanysjewelry-co.com/ tiffany and co jewelry
http://www.celineoutlet-online.com/ celine outlet
http://www.celine-handbags.com/ celine handbags
http://www.toms--shoes.us/ toms shoes
http://www.fitflopsandalssale.com/ fitflops
http://www.fitflopusaonline.com/ fitflop outlet
http://www.prada-handbag.com/ prada handbags
http://www.tiffanyandcoinc.com/ tiffany jewelry
http://www.prada-outlets.com/ prada outlet
http://www.michael-korsoutletinc.com/ michael kors
http://www.marcjacobs.us.com/ marc jacobs
http://www.supra.us.com/ supra shoes
http://www.ralphlaurenpolo-inc.com/ polo ralph lauren
http://www.tiffanyjewelry.us.com/ tiffany jewelry


Add your comment on this article below:

Sorry, there's an error with your form entries. We really appreciate your comment, so please try again.

Form submitting now...

Name:

Website:

Email address (not displayed):

Enter your comment below: