The life and times creating engaging media experiences.
« Previous | Main | Next »

SWF to SWF Communication Issue Using LocalConnection Class

There is a rather straightforward way to have two SWFs on a single web page communicate with one another, as described in this Adobe TechNote. However, there is a limitation with this simple approach: the communication does not work properly when there are multiple browser windows open at the same time. The problem even occurs when there is one Internet Explorer window open and one Firefox window open. You can easily see this issue by opening the Adobe TechNote in multiple browser windows; the demo application will work on only one of the browser windows.

The reason for the problem involves the name for the connection that is specified in both SWFs when the LocalConnection class is instantiated in the code. Apparently, this connection name is not unique to a browser session, but is unique to the user's machine. Therefore, with multiple browser windows open at the same time, they all try to communicate within the first browser window that was opened.

The solution is to devise a way to define a unique connection name for each browser window that is opened. The same unique connection name must be declared in both SWFs in the same browser window. The approach I took to solve this problem is to generate a random number in JavaScript and then pass that number into both of the SWFs on the page through a querystring variable when providing the URL of each SWF.

For instance, the random string can be defined in JavaScript as follows:

     var ranString = Math.random().toString();
     ranString = ranString.substr(ranString.length - 10, 10);

... and, the random string can be passed into the SWF as follows (assuming that the SWF is being presented using SWFObject):

     var swfurl = 'movie.swf?rs=' + ranString;
     var so = new SWFObject(swfurl, 'swfname', 100, 100, '7', '#ffffff');
     so.write('divname');

Then, whenever you reference the name of the connection in ActionScript, you append the random string that was passed into the SWF, for example:

     incoming_lc.connect("lc_example" + _root.rs);

Now, whenever a browser page loads, the two SWFs on the page reference the same connection name, and this name is unique to that particular browser window. Multiple browser windows can be open with the same page loaded, and the SWF to SWF communication will work as desired.

« Previous | Main | Next »