Local Connection Actionscript – Communicate between seperate Flash files | Tutorial

    Overview:

    Local Connection
    Communication between two separate flash files placed on the same page (or even running simultaneously on one machine) is a nice way to spread a project out. You can send variable, call functions, pretty much do anything in one swf from another. Easiest case use would be a navigation menu set up in one flash file to control the other one containing the content. I’ve made an example here showing how to send text from one to another. I’ve done it both directions here. Send text from the red swf to the blue swf, and from mr. Blue you send to the red flash file. I have named the flash functions in actionscript accordingly (or tried to, now I notice a few places I misspelled receive, ‘i’ before ‘e’, right? oh yea, except after ‘c’)…
    Anyways, try out the example here, I made it a little easier by putting a keyListener on ‘Enter’, so you don’t have to actually press the send button. Didn’t realize it before, but this is like a chat app built in flash! So go ahead and chat with yourself to prove that it works!

    Execute actionscript in one swf from another! Inter-swf communication.

    Example:

    Type here to send Red text to Blue flash file

    Get Adobe Flash player

    And see it received here, and go ahead and send some back to Red.

    Get Adobe Flash player

    Actionscript:

    Red:

    // Receiving
    //create a local connection for reciept of text
    var receiving_lc:LocalConnection = new LocalConnection();
    //function called from other swf
    receiving_lc.recieveBlueText = function(textRecieved:String) {
    feedback.text += textRecieved+"\n";
    };
    //receive connection of specified name
    receiving_lc.connect("fromBlue");

    //Sending
    sendButton.onRelease = function() {
    //create local connection for sending text
    var sending_lc:LocalConnection = new LocalConnection();
    //put text from input into a var
    var textToSend = inputText.text;
    //send through specified connection, call specified method, send specified parameter
    sending_lc.send("fromRed", "recieveRedText", textToSend);
    //set the input empty
    inputText.text = "";
    }

    Blue:

    // Receiving
    var receiving_lc:LocalConnection = new LocalConnection();
    receiving_lc.recieveRedText = function(textRecieved:String) {
    feedback.text += textRecieved+"\n";
    };
    receiving_lc.connect("fromRed");

    //Sending
    sendButton.onRelease = function() {
    var sending_lc:LocalConnection = new LocalConnection();
    var textToSend = inputText.text;
    sending_lc.send("fromBlue", "recieveBlueText", textToSend);
    inputText.text = "";
    }

    And the code to listen to the ‘enter’ key(this is in both files):

    //Enter button to send
    var keyListener:Object = new Object();
    keyListener.onKeyDown = function() {
    if (Key.getCode() == "13") {
    sendButton.onRelease();
    }
    };
    Key.addListener(keyListener);

    Download Source:

    localConnectionRedBlue.zip

    This entry was posted in tutorial and tagged , , , , , , . Bookmark the permalink. Post a comment or leave a trackback: Trackback URL.

    25 Comments

    1. 4dplane
      Posted April 8, 2008 at 10:19 pm | Permalink

      Hi, and thanks for this example. I have converted this into AS3 and put the code in .as files it works but in a strange way – here is a link to the problem if you are intersted.

      http://www.kirupa.com/forum/showthread.php?p=2310148#post2310148

      thanks again,
      4dplane

    2. Rajesh
      Posted May 16, 2008 at 6:23 am | Permalink

      I have a doubt.. Can we create local connection between three swf files.. Can u please clarify my doubt…

    3. Posted May 16, 2008 at 2:47 pm | Permalink

      Theoretically this is supported. Sorry, I haven’t tested it before, so I can’t say for sure of have an example, I’ll add it to my list though. =)

    4. Posted June 24, 2008 at 5:31 pm | Permalink

      Thanks so much! I’m glad that they’re helpful.

    5. Taylor Dodds
      Posted June 25, 2008 at 11:10 am | Permalink

      Is it possible to add this script to multiple buttons in a pamenu and call multiple flash vido files with it?

    6. Taylor Dodds
      Posted June 25, 2008 at 11:11 am | Permalink

      menu*

    7. julian
      Posted August 28, 2008 at 2:10 am | Permalink

      good job,, thanks for this tutorial…

    8. motu
      Posted September 25, 2008 at 1:27 pm | Permalink

      I too am interested to control a Flash video player from a separate Flash file (with buttons or menus sending the controls)…

      BTW, here’s an example with 4 Flash files communicating on one html page:
      http://home5.inet.tele.dk/nyboe/flash/localconnectiontest.html

      Thanks!!
      motu

    9. ruchi
      Posted November 1, 2008 at 7:58 am | Permalink

      its a good example ,easy to learn,understand……….

    10. Posted November 26, 2008 at 10:48 am | Permalink

      Well on Firefox for Mac your exemple does not work, weird….

    11. Posted November 29, 2008 at 4:43 pm | Permalink

      With ff3 on my mac it works for me ;)

    12. ameer
      Posted April 29, 2009 at 1:07 am | Permalink

      hey can any body help
      it is not working in ie6…plz help

    13. Posted April 30, 2009 at 7:16 pm | Permalink

      This example works great but what can be done about someone who has two browser events open at the same time. The connection will only address the first page that is opened.

    14. Vance
      Posted November 25, 2009 at 12:49 pm | Permalink

      you may want to check the code and solve for the problem that when someone opens your website up in two separate tabs, the local connection object fails. you did catch the error correctly however you didn’t give a second or third one to try when they fail. i’ve had this problem before and it saved me to have a second connection name to try if the first one failed. also if the user was at a website that used the same code you have, more specifically the same local connection name you used, one of the connections would fail.

      just something to think about when doing this. i’ve found the local connection is a real pain.

    15. Tim
      Posted January 20, 2010 at 6:05 pm | Permalink

      This code isn’t working for me on OS 10.5, Firefox 3.5.6, Safari 4.0.4, Flash Player 10,0,32,18. Not sure why.

      Works fine via Parrallels / Windowx XP Pro 2002, IE 6.

      Is this Flash Player Mac bug?

      • Posted January 21, 2010 at 11:55 am | Permalink

        Well this is almost 2 years old, so I wouldn’t be too surprised it new browser updates may have borked it. i’d try some as3 by now to do it. That may help as well

        • WE
          Posted March 5, 2010 at 12:49 pm | Permalink

          That’s the conclusion I’m starting to come to. I’ve been searching around for a working version of a flash localConnection tutorial for hours. I have yet to find a functioning example. Unfortunately, I’m also unable to find any verification that this is due to some new security feature, or other known issue (aside from the very common multiple open browser issue). Seems that this is a problem that has slipped through the cracks and isn’t being addressed anymore. Is there some new way to communicate between flash files on an HTML page?

    16. Matt D.
      Posted April 21, 2010 at 2:32 pm | Permalink

      Hi,
      I’m just starting to learn ActionScript & 3.0 and am working on a project that uses a set of 4 navigation buttons (swf1) that on user click need to play their respective content menu swf’s (swf 1-4) in the same html page. (basically the case that you describe in your overview above).
      Can you provide an example of how to code one of the 4 movie clips in swf1 to call it’s swf content menu and play it from frame 2?
      Any help would be greatly appreciated.
      Thanks in advance.
      Matt

    17. Posted October 20, 2010 at 2:14 am | Permalink

      hi do u have example with 3 swf and localconnection
      thanks

    18. Lou Parker
      Posted August 16, 2011 at 9:15 pm | Permalink

      The example does not work in FF6 or IE9. Any idea why that is?

    19. Posted March 14, 2012 at 1:44 pm | Permalink

      Is it possible to have the red flash in one computer and the blue flash in another computer communicating across the web?

      Great example
      Regards

      • Posted March 23, 2012 at 2:28 pm | Permalink

        @Miguel

        This is indeed possible, but this is not the method that will do that. You’d need to communicate with the server for something like that. Best of luck!

        • Posted March 23, 2012 at 3:33 pm | Permalink

          The idea is to control a remote swf by pressing buttons on a local swf. Let’s say I have a computer with a swf loaded on a browser and from across the web I press a button on another swf also loaded on a browser, an that pressing of the button makes an action on the first swf.

          • Posted March 24, 2012 at 8:54 am | Permalink

            Yes Miguel, this case would require some communication with the server. So each swf would talk to either a database or an xml or even a text file.you’d have one (our even both) writing data while the other reads it. Maybe look up chat apps in flash, I remember seeing some like that not to long ago (maybe on active Tuts . net), it would show you how to use a technique like this.

            • Posted March 24, 2012 at 9:09 am | Permalink

              Thanks Evan, I’ll look up for that kind of app.

              BTW Great Avatar! :D

    One Trackback

    1. By Flash LocalConnection Tutorials | tutorials blogs on November 14, 2010 at 12:01 pm

      [...] Local Connection Actionscript – Communicate between seperate Flash files | Tutorial [...]

    Post a Comment

    Your email is never published nor shared. Required fields are marked *

    *
    *

    You may use these HTML tags and attributes: <a href="" title="" rel=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

    • Recent Posts

      WordPress updates plugin directory

      New additions to the plugin directory include: favorites, incorporating support forums into it's own tab for each plugin as well as support stats being displayed! Great! I think we also need the ability to give plugins ratings and reviews (bonus points if it can be done from within a wordpress admin dashboard when installing plugins). [...]

      Short Head

      Use zipf's short head to tune your website rather than redesign the whole thing. To make a website successful it needs to meet the needs of the users. Find out what those needs are by using the short head philosophy to equate most searched things as the biggest needs of the users. Use personas to [...]

      Img Set?

      Great article at a list apart discusing the state of the industry regarding responsive images. This picks apart the set attribute of the img element from a surprisingly objective view coming from someone so close to the picture element. Insightful discussion about the principle behind the proposals than the actual solution too. If the working [...]

      Triudo

      A mesmerizing animated triangle-ish shape form. Embedded Link triduo triduo Tweet

      Git – the paradigm shift

      A great developer story about the differences on what Git is vs other version control and what Git is not. This is how we should learn it. I heard over and over that it was distributed, but never grasped what that meant, so here are a few links and explanations that will help unlearn version [...]

      Tweening Lib comes to Javascript!

      I'm very excited to share the news that the tween library from GreenSock (hands down the best tweening library I used in flash) is not ported for use in javascript! This will be great! I missed that simple syntax from as3 when animating javascript, and now I can have my cake and eat it too. [...]

      Responsive CSS Tricks

      Here are a few useful css tricks to remember when building responsive design sites from web designer wall Embedded Link 5 Useful CSS Tricks for Responsive Design Making the design to be responsive is very easy as shown in my Responsive Design in 3 Steps tutorial, but maintaining the elements to look aesthetically balanced on [...]

      Picture element of srcset attribute?

      Bruce details the reasons and story behind the srcset attribute which is now introduced as an alternative to the picture element. Some aspects of the attribute are nice (like the fact that it's an attribute and not a new element, so it's creating up new elements with for problems. It's adapting currently used elements to [...]

      SVG Preloader with Raphael JS

      Here's a very creative use of using a newly available technology. Using svg graphics which are very lightweight, for a website preloader. I like the animation used as well. Embedded Link Make a stylish preloader with SVG | Tutorial | .net magazine Many sites neglect users with slow connections. Ian Culshaw explains how to use [...]

      CSS3 Button/Icon set

      I've been secretly hoping to see a few of these pop up once the whole icon font idea spread through the nets. I really like this idea and it's a very nice implementation too! I only see some quality issues on a couple of the icons (such as youtube), but it's awesome and I hope [...]

    • Recent Comments

      Bruce Brownlee

      Bruce Brownlee

      Ah IE6. I'd have 2 more years of sleep without IE6. Margin doubling, no properties,...
      versaena

      versaena

      how to give color at runtime…… thank you
      Mobile Websites

      Mobile Websites

      I disagree, mobile websites are the future – desktop websites and mobile websites...
      Matt Fasick

      Matt Fasick

      That's cool. I like the ripple effect as well.
      Nico

      Nico

      hi! really great job guy! very impressive.. just a question… do u have a solution to do a refresh...
      Evan Mullins

      Evan Mullins

      Agreed! I've just seen some people get pretty heated about separating all functionality...