8 Replies Latest reply on Mar 14, 2007 6:12 PM by Art Clifford

    LocalConnection Advanced use question

    Art Clifford
      Does anybody know if it is actually possible to have two flash movies talk to each other when one is in an iframe at a different domain and one is in the page containing the iframe?

      I spent quite a bit of time yesterday going through varoius permutaions on naming the connections. The closest I got to anythign was when I got my sender movie in Flash to successfully talk to the reciever movie in the browser, that was accomplished having an underscore in front fo the connection name in sender and reciever. But when the reciever swf is in a page containing an iframe with the sender swf nothing appears to happen.

      Since I got flash player (and an executable version of the sender) to commicate with the browser based reciever I figure this should be possible. But i can't seem to figure out what I'm missing in the browser based versions.

      The intent of this exercise is to allow technology I am helping to delop to be included in a page using an iframe while providing feedback to listener movie in the site the iframe is embedded in. From there we would provide an api for interacting with the listener movie so that sites wishing to embed our technology could handle certain events. I realize of course that interframe communication between frames at different domains is a violation of the same domain rule at the heart of most browser security. So, I wouldn't be surprised if it isn't possible. And yet, I could communicate between flash player and the listener so I still wonder at the possibility.

      Does anybody know if what I'm attempting is even possible? And if so, any clues as to what may be the problem? If anybody chooses to help with the problem or innovate a work around I would be happy to share the source files I've been playing with, which are simple bare bones swfs.
        • 1. Re: LocalConnection Advanced use question
          Level 7
          Art,

          > Does anybody know if it is actually possible to have
          > two flash movies talk to each other when one is in
          > an iframe at a different domain and one is in the page
          > containing the iframe?

          Shouldn't be a problem. The thing about LocalConnection is that the
          SWFs need to reside on the same *computer*; in theory, it doesn't matter if
          they're even running in different browsers (one in IE, say, and another in
          Firefox), or if one of them is running in a browser and the other is in a
          Projector (executable wrapper).

          The 3rd party debugging app, Xray ( http://osflash.org/xray), for
          example, operates on this principle.

          > I realize of course that interframe communication between
          > frames at different domains is a violation of the same domain
          > rule at the heart of most browser security. So, I wouldn't be
          > surprised if it isn't possible.

          Ahh, different domains. Well, that might just explain the trouble
          you're seeing, then. I would start by testing a page and iframe that reside
          on the same domain, get a proof of concept working, then branch out to see
          when it breaks.

          I'm curious, though, about your mention of success only with an
          underscore. The instance name of your LocalConnection object shouldn't make
          one bit of difference. Connection name, either.


          David Stiller
          Adobe Community Expert
          Dev blog, http://www.quip.net/blog/
          "Luck is the residue of good design."


          • 2. Re: LocalConnection Advanced use question
            Art Clifford Level 1
            Thanks for replying David,

            I have done a test and can confim that the sender movie if it is in an iframe within the same domain will work but still is not working in another domain. There are many examples of people using LocalConnection between frames and so I am not suprised by that.

            I have a test page @ http://www.artspad.net/lc/CartographInside.html, sorry it is less than pretty, but it is just a quick test.

            if you type a message in the text box and click the button in the left iframe the message will get to the reciever. But that won't happen in the right frame.

            As to your interest in the _ in the name. If you dig into the technotes and even some of the documentaion for LocalConnection.send, it says that if you do not put an underscore in front of your connection name then domains will be added to the connection name by flash. If you do have an underscore, flash won't prepend the domain.

            The language of the help and technotes suggest to me that going between domains should be possible. So, I'm sure this must be something I'm doing and that it probably lies in the guts System.security.

            I've attempted to attach the source I'm using for both movies to this message. It is very basic and pretty much lifted from the samples in the flash help docs.

            So, I'm guessing as I said that this has more to do with cross-domain issues. Which brings up questions like, do I need a cross-domain policy file? If so, for which movie (both?). I set in the param tag and embed tag the allowAccess parameter to "always" which didn't seem to impress anything. My tests right now aren't getting into http vs https, the two domains are both http. As you can see I do have an allowDomain function defined that, for now anyway I just have returning true.

            I'm open for ideas. I think that if this is possible then it opens up interesting possibilties. However, if it isn't I think it is important that it be stated somewhere in the docs.

            • 3. Re: LocalConnection Advanced use question
              Level 7
              Art,

              > I have a test page @ http://www.artspad.net/lc/CartographInside.html,
              > sorry it is less than pretty, but it is just a quick test.

              Something must be funky with the HTML on that page. Only the left
              iframe shows up for me in Firefox (both show in IE). You have a br tag
              before the iframes that appears like this ... </br>, and I wonder if that's
              throwing off the parser in Firefox (should be <br />).

              > if you type a message in the text box and click the button in
              > the left iframe the message will get to the reciever. But that
              > won't happen in the right frame.

              In IE, I see that. Agreed.

              > As to your interest in the _ in the name. If you dig into the
              > technotes and even some of the documentaion for
              > LocalConnection.send, it says that if you do not put an
              > underscore in front of your connection name then domains
              > will be added to the connection name by flash.

              Ah, good eye! I'm going to have to remember that. I'm not even sure
              I've seen that before.

              > So, I'm guessing as I said that this has more to do with
              > cross-domain issues. Which brings up questions like, do
              > I need a cross-domain policy file?

              My hunch is the same as yours. The first thing I'd try in this scenario
              is a cross-domain policy file, you bet.

              > If so, for which movie (both?). I set in the param tag and
              > embed tag the allowAccess parameter to "always" which
              > didn't seem to impress anything.

              That policy file (an XML file described in the documentation) needs to
              be in the root of the server of the "other domain." This policy file allows
              SWFs from elsewhere to serve up content from the domain making the request.
              The server that provides the right-side SWF needs to explicitly allow the
              server on the left side to request SWF files from it.

              > I'm open for ideas. I think that if this is possible then
              > it opens up interesting possibilties.

              I haven't tested the cross-domain policy business with LocalConnection
              like this, but I've done plenty with it in regard to Web Services, and --
              while it's annoying -- it does work.


              David Stiller
              Adobe Community Expert
              Dev blog, http://www.quip.net/blog/
              "Luck is the residue of good design."


              • 4. Re: LocalConnection Advanced use question
                Art Clifford Level 1
                Okay, the crossdomain thing wasn't necessary, that is more for loading content from another domain and seems to be more about accessing content at that domain which is only alowed if a crossdomain policy file exists. Just to clarify something though, as of Flash7 you can use System.security.loadPolicyFile("urlToFile") and specify a crossdomain policy file anywhere on a site, it doesn't have to reside at the root.

                Per: http://www.adobe.com/devnet/flash/articles/fplayer8_security_05.html
                And the section "Cross-Scripting Permissions"
                "A remote SWF scripting another remote SWF from a different domain; the accessed SWF must grant permission (the remote-to-remote rules have not changed from Flash Player 7)"

                So, to cover everything, I added an allowDomain and allowInsecureDomain function to both the send and reciever and things work now.

                As to firefox, I noted in IE that it didn't want to make iframes that were separated by br tags. And firefox apparently wants to load the iframes on top of each other. That is a learning curve and a bucket of worms that is beyond the scope of my current investigations. But if anybody figures out how to position iframes in a page or has a good link about it I'm sure having a post about it here wouldn't hurt anything.

                One more question along this topic of advanced usage of LocalConnections ... is there a reccomended method of dealing with multiple windows being open with the same reciever movie so that the sender movies aren't all sending back to the originally opened movie? I suppose I could do something like try connecting to name_0, and if that is there try name_1 until I've found a free connection. But then how do I let the sender movies know what movie they can connect to? I figure LocalConnections have been around long enough now somebody has to have figured it out.

                Anyway, thanks David for your help. Just knowing it should have been possible gave me the drive to make it happen. Its just a first step, but an important step.
                • 5. Re: LocalConnection Advanced use question
                  Level 7
                  Art,

                  > Okay, the crossdomain thing wasn't necessary [...]

                  Yay!

                  > Just to clarify something though, as of Flash7 you can use
                  > System.security.loadPolicyFile("urlToFile") and specify a
                  > crossdomain policy file anywhere on a site, it doesn't have
                  > to reside at the root.

                  You've inspired me, Art. I'm not often required to load content from
                  other domains, and out of laziness (probably) have shied away from spending
                  as much time as I should in the cross-domain security areas of the
                  documentation. This resulted in my inability to help you more fully on this
                  question. I'll be reading up on those dusty (to me) areas. :)

                  > One more question along this topic of advanced usage
                  > of LocalConnections ... is there a reccomended method
                  > of dealing with multiple windows being open with the
                  > same reciever movie so that the sender movies aren't all
                  > sending back to the originally opened movie?

                  What about sending an arbitrary id as one of the parameters with your
                  message? All movies listen, but when a message comes, each movie checks to
                  see if the id param matches a correlating id of its own. This could be a
                  variable stored in the root of each listening movie. (Am I movie "A"? Yes?
                  Okay, I'll follow these instructions.)


                  David Stiller
                  Adobe Community Expert
                  Dev blog, http://www.quip.net/blog/
                  "Luck is the residue of good design."


                  • 6. Re: LocalConnection Advanced use question
                    Art Clifford Level 1
                    "> One more question along this topic of advanced usage
                    > of LocalConnections ... is there a reccomended method
                    > of dealing with multiple windows being open with the
                    > same reciever movie so that the sender movies aren't all
                    > sending back to the originally opened movie?

                    What about sending an arbitrary id as one of the parameters with your
                    message? All movies listen, but when a message comes, each movie checks to
                    see if the id param matches a correlating id of its own. This could be a
                    variable stored in the root of each listening movie. (Am I movie "A"? Yes?
                    Okay, I'll follow these instructions.)"

                    The problem with the arbitrary id is that I have to send it somewhere, and I need to know the name of the connection to send it to, but each connection name must be unique. That is why if you open a new window messaging goes to the first window, the connection command in the second window will fail because the name will aready be taken in window one. And the other movies won't know that and will all send to the named connection which is still in window one.

                    The connect function will fail if a connection already exist.s So, on the one hand all I have to have is a loop that keeps trying to connect until it finds a free connection. The problem I am thinking about is having two movies in the same window, one in the page displayed, and one in an iframe, as I set up on that demo page. I'll want the one in the iframe to communicate with the one in the outer page for validation purposes. If all there is is one window then that's fine. But if sombody were to hit Ctrl+N in IE they would get a new browser window with the same page. The problem is how to make it so that the two movies in each window know to only communicate with each other in only that window?

                    My thought is that I can use the incremental connection id technique in each movie and figure that if the increment is firstMovie_2 in one movie then it should communicate with otherMovie_2. What I'm worried about in this scenario though is what happens if the user is caffienated and hits Ctrl+N five times? Will the two movies increment correctly for each browser window? Or will there end up a couple windows with a firstMovie_3 and secondMovie_2 because things didn't load in a controlled sort of way?

                    I think the incremental technique would be fine for if a window closed because that would free the connection id and the loop would always start at 0, so if you had five windows opened and closed the first one and opened a new one, the new window would use the ids with the _0 increment.

                    Anyway, its helping me to write this stuff out. I'm going to implement the incremental id thing and I'll report my findings.

                    As to looking deep into the technotes and docs. That http://www.adobe.com/devnet/flash/articles/fplayer8_security_05.html article was great for the table it has in the middle of it which outlines what is needed for the various ways you try to connect movies.

                    My only other reccomendation is the help docs and Excedrin since it has both Acetometaphin AND caffeine and both are useful for trudging through the fine points of the flash docs ;).


                    • 7. Re: LocalConnection Advanced use question
                      Level 7
                      Art,

                      > The problem with the arbitrary id is that I have to send
                      > it somewhere, and I need to know the name of the
                      > connection to send it to [...]

                      Gotcha. <slaps forehead> And now I see what you're up against. My
                      faulty memory was telling me it was possible for several receiving SWFs to
                      listen to the same connection. Not so.

                      > If all there is is one window then that's fine. But if sombody
                      > were to hit Ctrl+N in IE they would get a new browser
                      > window with the same page.

                      Wow. I really don't know how I might approach that issue, especially
                      as, like you said, the user might open quite a handful of browsers. Your
                      incremental id approach seems like a good one to me. It makes good sense to
                      consider an upper limit, I'm sure, but within reason, I could see using
                      enough false return values as an indication to disable event handlers, text
                      fields, and the like in duplicate sending SWFs.

                      > Anyway, its helping me to write this stuff out. I'm going
                      > to implement the incremental id thing and I'll report my
                      > findings.

                      I'll keep an eye on this thread; I'm looking forward to your
                      discoveries.

                      > My only other reccomendation is the help docs and Excedrin
                      > since it has both Acetometaphin AND caffeine and both are
                      > useful for trudging through the fine points of the flash docs ;).

                      lol


                      David Stiller
                      Adobe Community Expert
                      Dev blog, http://www.quip.net/blog/
                      "Luck is the residue of good design."


                      • 8. Re: LocalConnection Advanced use question
                        Art Clifford Level 1
                        Okay, here are my findings:

                        The technique I laid out where both the reciever and sender each set up connections incrementing by one seems to work. The test url I provided before is now demoing that capability, though i intentionally broke the iframe for the other domain (since we proved that works). So, if you do a Ctrl+N a few times you'll see that things communicate right.

                        So what happens is each of the two movies tries a new connect operation until it comes up with a free id. I did that with a simple for loop where the value is stored in a variable "i". And once a connection is made I break out of the loop. The sender then knows to send its message to "_receiver_"+i. And messages can be sent back to the sender via "_sender_"+i. To do this to support multiple iframes ( which is not currently my goal) I would have to ensure that each iframed movie had a unique identifier to use as the connection name so it would end up "_"+uid+"_"+i. At that point David, the notion of sending the name of the connection to whatever movies a response is desired from would be in order like you mentioned.