by David LeMieux
I would suggest starting here: The Problem With Local Connection
At the end of the above-linked post I said "In the mean time we are working on a solution using SharedObject and timed polling [to replace LocalConnection]." So it should be no surprise to announce that we have successfully done so.
We replaced LocalConnection with SharedObject
How is it done?
The premise is a simple one, instead of directly calling a method on another swf file as you would with LocalConnection, we have created a polling system that uses SharedObject to relay messages from one swf to another. SharedObject is like a cookie that Flash Player can use on a users machine. Taking that in to consideration we built a controller that uses SharedObject to save and retrieve data at timed intervals and in that way shares commands between different swf files.
Here is a basic rundown of what happens:
SWF One - writes an instruction to the SharedObject using the controller.
SWF Two - is set up to listen for changes of the SharedObject and then parse new values for instructions.
There are some "gotchas"
First, if you have lots of SWFs on the page you don't want them to accidentally overwrite any previously set but unread value. Because the sending isn't instantaneous it is very possible that multiple instructions could be send before the receiver had a chance to poll for more of them. To overcome this we implemented a basic multi-thread like system of mutexes and semaphores. Essentially, different actions (reading and writing, depending on the circumstance) block others and the controller knows how to queue up commands. The controller also knows how to interpret queued commands.
Second, there are people who don't allow memory for SharedObject on their machines. In this case we had to build in a LocalConnection backup. This is ironic because we build the SharedObject controller to replace LocalConnection, but it is more of a Plan B than a surefire backup.
Third, related to the first, there can be some lag between when a command is sent and when it is received. Fractions of a second (in our case) but still lag nonetheless.
Fourth, because of the way we implemented queuing sometimes there can be a high increase in CPU usage. We are working on tweaking this.
How can you do it yourself?
That is a very excellent question. We are currently stress testing our solution and are about to launch it on an even larger scale. We now have a very scalable solution for sending messages to multiple swfs on a page. We have tested hundreds of swfs on a page communicating with one another with a 100% success rate. Comparing that to the small range (5-12 or so) of LocalConnection successes and I feel like we have a real winner on our hands.
I cannot say if we will ever release the code and we wouldn't currently because of how new and untested it is (and undocumented and still needing refinement and a million other reasons). I do wish to say we have found a solution, however, and that If you familiarize yourself with SharedObject and think about it for a while you will be able to come up with your own solution. Our controller is literally around 50-100 lines of code; not very big.
If you come up with something you would like to share please do. I am also willing to answer questions if they arise.
Best of luck.