16 Replies Latest reply on Feb 5, 2008 6:50 PM by Damon Edwards

    Server to Flash communication

    rl4518
      I'm new to flash and actionscript and ajax technologies, but not new to programming or design. Before I spend a lot more time and money on flash, I'd like advise. I want to have a browser client load a SWF file that I have initially created with flash, that contains animations. After it starts up in the flash player I want to push information from the http server, such as
      parameters to the running flash SWF that will control it. I don't want to reload the page with http requests, etc.
      For example, say an animation is initially created of a stationary ball, loaded and displayed by the client browser. While the http connection is still up, the server writes back to the client browser in a way that can control the ball. First, lets say text appears that labels the ball as "my red Ball", then it bounces, or tweens,etc. Or maybe we jump to a different point in the animation. Can we do it and how? Thanks, Ray.
        • 1. Re: Server to Flash communication
          ooba Level 1
          Its been a few years since I last truly played around with the rich features of flash, but if you use the Flash Communicatio Server service, you can from the Server make calls directly to the swf file on the clients browser and execute methods on it. But only via the communication is this possible, as far as I know.
          • 2. Re: Server to Flash communication
            rl4518 Level 1
            Tank you for the information. At the risk of sounding ignorant, is the "flash communication server service" mentioned a separate software package? Exactly what is necessary to make it run and where might I go to read some documentation on it?

            -thanks!
            • 3. Re: Server to Flash communication
              Level 7
              Hi rl14518,

              Flash Communication Server is a separate piece of software sold by
              Adobe. It runs on the target web server and it's now called Flash Media
              Server. This is probably more approrpiate since it not only allows remoting
              (sending data and function calls back and forth), but also serves out video
              and audio streams. I do believe you can grab a free, single-seat version on
              Adobe's site for development purposes. You can also grab AMFPHP which is a
              port of the media server in PHP. It's not as robust and can't handle as much
              information, but it is open source and freely available. For remoting, it
              does the same thing (except it calls PHP functionality, not ColdFusion or
              Java).

              Regards,
              Patrick Bay
              BAY NEW MEDIA

              "rl4518" <webforumsuser@macromedia.com> wrote in message
              news:fnddq5$1gl$1@forums.macromedia.com...
              > Tank you for the information. At the risk of sounding ignorant, is the
              > "flash
              > communication server service" mentioned a separate software package?
              > Exactly
              > what is necessary to make it run and where might I go to read some
              > documentation on it?
              >
              > -thanks!
              >

              • 4. Re: Server to Flash communication
                rl4518 Level 1
                Patrick,
                Thanks so much for giving me the reference to AMFPHP. It opened up a lot more trails to follow. I found more open source variations on AMF and the Adobe communication server (notably this open source Red5) and also a perl module AMF::Perl.
                By the use of registering callbacks, I can see how actionscript written in a Flash SWF running in a client browser can call server side functionality that's written in server languages such as PHP and Perl, but does it work the other way around ?
                Can a PERL script executing on the SERVER side, communicate with a SWF in the browser, such that the SWF animation or movie is controlled? At first I thought perhaps the server could send back actionscript to the client once the AMF connection was established, but I'm still fuzzy about this. Is this possible in general from Flash Remoting?
                Not being a Flash or Actionscript developer (yet), I only have a broad understanding. I "believe" that I could create an animation using the Flash product and then control what it does using actionscript. For example, creating a bouncing ball in Flash, then using actionscript to move it around, make a label appear on it, or introduce a square that moves along side. But, could I do all of that from the server using AMF via AMFPHP or AMF::Perl?

                -confused,
                Thanks,
                Ray
                • 5. Server to Flash communication
                  abeall Level 3
                  To push data to a Flash client, you need to use Flash Communication/Media Server, or an open source alternative (Red5, etc). These formats allow you to push data to Flash, as well as stream Flash content using AMF.

                  Another way to push data to Flash is using XMLSockets, but again you need a special XMLSocket server to do so -- or you can make your own if you know Java or any language that can act as an XMLSocket server. In this case, you can only push string data (XML) to Flash, so no media like audio or video (that's the real appeal of Flash Media Server). Of course, you can push a string reference to a media file on your web server, and then have Flash progressively download it and play it.

                  A last way you can push data is using binary sockets. Again, you need a socket server to connect to.

                  But you can also use "polling" -- just keep sending HTTP POST/GET requests to the server every X seconds or X minutes, depending on how frequent you want the update. Each request will get a response, so that's your chance to send data back to Flash. I use this option a lot, because you can use your existing HTTP server, you don't need to get into buying, building, or installing additional servers on your web box.

                  Then there's remoting. Remoting gets back into AMF which just means you can communicate in native ActionScript language (ie not XML strings or URL strings), but it doesn't let you push data; it's really just request/response like HTTP POST/GET, except you are speaking in ActionScript data instead of POST/GET url variables.
                  • 6. Re: Server to Flash communication
                    rl4518 Level 1
                    Thanks Arron,

                    Your idea of "polling" is interesting. By polling, you mean that the action script in SWF is polling the web server for additional pages? I like the idea of not having to add anything special, but the direction of control I'm trying to create is from the web server to the flash player.

                    I keep using the bouncing ball scenario, but at least it's simple and that's because my understanding is simple at this point.

                    But, ... lets say an SWF has already been programmed with an animation and a bunch of action script routines that when called by "something", will move the ball, or label the ball, or cause new images to be displayed on the stage. I almost envision pre-programmed action script functions that when called will play the animation back in specific scenes , just like a movie script.

                    1) Browser asks http server for this SWF.
                    2) The flash player loads it and starts scene #1 (which might be the execution of a main() or default action script function)?
                    3) Now, nothing else happens and the user watches scene #1 repeat until the web server, which is running complex Perl or PhP scripts tells the Flash player to advance to scene #2.
                    4) Now, scene #2 might need parameters sent to it. Something not known when the SWF was created. Lets say the Ball gets a unique name that only the server side script knows after it begins to run, but after the SWF is loaded into the player. So in asking the flash player to run the action script for scene 2, the server might also pass the text string "herbert" and also a number X. The X might be how many units to move the ball and "herbert" is a label that should appear on the ball.
                    5) The scene #2 action script gets these parameters and does its magic to cause the name to appear on the ball and then move it X units.
                    6) The animation now stops in scene #2 awaiting the next server command.

                    The big question for me is can we do this? In reading the discussion here, I see the following technologies:

                    A) Flash and action script for creating the SWF and rendering the animation.
                    B) Flash Communication/Media Server and the open source Red5 for flash remoting and AMF communications
                    C) AMFPHP an open source version of the Flash Media Server
                    D) AMF::Perl an AMF Perl Module
                    E) Polling the Web Server
                    F) XML Sockets

                    Can anyone suggest from experience which of these (if any) might be able do accomplish what I need?

                    Thanks for all of your help so far!
                    Ray
                    • 7. Re: Server to Flash communication
                      abeall Level 3
                      > I like the idea of not having to add anything special, but the direction of
                      > control I'm trying to create is from the web server to the flash player.

                      Right, the simple answers is that if you want to push data from the server to the SWF, you need to have an stream with the SWF, and the only way you can do that is with a special server:
                      [ActionScript class] -> [Server needed]
                      - NetConnection/NetStream -> Flash Media Server/Red5/Wowza/etc
                      - XMLSocket -> ElectroServer/SmartFoxServer/Palabre/Make your own/etc
                      - Socket (binary) -> Anything that can act as a binary socket server...

                      You cannot push data with an HTTP server the way you describe.

                      Just to clarify a few things:

                      > A) Flash and action script for creating the SWF and rendering the
                      > animation.
                      Yep. :)

                      > B) Flash Communication/Media Server and the open source Red5 for
                      > flash remoting and AMF communications
                      Sort of... remoting is request/response business, which does not let you push data. FMS and Red5 are very different from remoting because they let you push data with an open stream connection, and what's more, they communicate with ActionScript code(AMF) so you don't have to convert anything when you receive it, AND you can stream Flash video/audio

                      > C) AMFPHP an open source version of the Flash Media Server
                      No, AMFPHP is open source remoting(there is actually a product Flash Remoting that MM used to make), ie request/response. You cannot push data to the SWF, which is what you want. However, the nice thing is even though this essentially just request/response, it uses AMF so you send/receive ActionScript with the server, which just makes the process smoother...

                      > D) AMF::Perl an AMF Perl Module
                      See above, AMF::Perl is AMF remoting again, like AMFPHP. You cannot push data from server to SWF.

                      > E) Polling the Web Server
                      I think you get this one.

                      > F) XML Sockets
                      Important to re-iterate: this *does* let you push data from the XMLSocket server to the SWF, but the disadvantage is you can't send native ActionScript code (AMF) and you HTTP server still can't do this, you need a XML Socket server (ElectroServer/SmartFoxServer/make your own/etc).

                      > Can anyone suggest from experience which of these (if any) might be
                      > able do accomplish what I need?

                      If you absolutely need to push data (in other words, it's not just a "nice to have" thing, you need it) from the server to the SWF:
                      - If you want free, I would and do use HTTP polling with PHP.
                      - If you have money, I would use Flash Media Server, but rather than buy the server and install it on my box, I use a FMS hosting service (I use Influxis, there are some others).

                      HTH
                      - aaron
                      • 8. Re: Server to Flash communication
                        rl4518 Level 1
                        Aaron,

                        Thanks for correcting me. I see now that AMFPHP is an open source remoting technology, whereas Red5 is the open source Communication Server technology. One is a request / response and the other is streaming and AMF and a whole lot more.

                        Is there a way we can communicate with running Flash Player via actionscript by using a Javascript API? Seems like I read this somewhere?

                        -ray
                        • 9. Re: Server to Flash communication
                          abeall Level 3
                          You can communicate between a running SWF and browser JavaScript via ExternalInterface. That doesn't get you any closer to being able to push data from the server to the SWF, because you can't push data to JS, either (afaik).
                          • 10. Re: Server to Flash communication
                            Level 7
                            Hi rl14518,

                            One of the drawbacks of AMFPHP (and related technologies) is that method
                            invocation is on the server only. This means that Flash only receives data.
                            I'm expanding a system called XMLs which allows for this type of
                            functionality in a unique way: method invocation that's bidirectional *AND*
                            nested (the return of one method can be fed into another mehod). It's XML
                            based and has an accompanying PHP module but it's still fairly early on in
                            development. Data types are present but method invokation is a bit tricker
                            than I thought. If you'd liek to help me develop/test it, I'd be happy to
                            put you on teh contributor's list. It's part of a larger open-source project
                            called BNMAPI hosted on SourceForge. Let me know if you're interested, I can
                            certainly use the help!

                            Regards,
                            Patrick Bay
                            BAY NEW MEDIA

                            "rl4518" <webforumsuser@macromedia.com> wrote in message
                            news:fne8s3$sgn$1@forums.macromedia.com...
                            > Patrick,
                            > Thanks so much for giving me the reference to AMFPHP. It opened up a lot
                            > more
                            > trails to follow. I found more open source variations on AMF and the Adobe
                            > communication server (notably this open source Red5) and also a perl
                            > module
                            > AMF::Perl.
                            > By the use of registering callbacks, I can see how actionscript written
                            > in
                            > a Flash SWF running in a client browser can call server side functionality
                            > that's written in server languages such as PHP and Perl, but does it work
                            > the
                            > other way around ?
                            > Can a PERL script executing on the SERVER side, communicate with a SWF
                            > in
                            > the browser, such that the SWF animation or movie is controlled? At first
                            > I
                            > thought perhaps the server could send back actionscript to the client once
                            > the
                            > AMF connection was established, but I'm still fuzzy about this. Is this
                            > possible in general from Flash Remoting?
                            > Not being a Flash or Actionscript developer (yet), I only have a broad
                            > understanding. I "believe" that I could create an animation using the
                            > Flash
                            > product and then control what it does using actionscript. For example,
                            > creating
                            > a bouncing ball in Flash, then using actionscript to move it around, make
                            > a
                            > label appear on it, or introduce a square that moves along side. But,
                            > could I
                            > do all of that from the server using AMF via AMFPHP or AMF::Perl?
                            >
                            > -confused,
                            > Thanks,
                            > Ray
                            >
                            >

                            • 11. Re: Server to Flash communication
                              Level 7
                              Hi again, rl14518,

                              Polling is not a great technique for client/server communication, in my
                              opinion. Unless something happens every time the server is polled, it does
                              generate a lot of needless traffic. If you wanted to keep a communication
                              channel opened like this, it would be better to do so via an open XML
                              socket. This would also reduce the total amount of traffic by elinimating
                              the extra HTTP headers that are sent with every request. Socket servers are
                              easy to write in almost any langauge but you should be working on a
                              dedicated server; shared servers usually don't allow people to open up
                              arbitrary socket listeners.

                              Regards,
                              Patrick Bay
                              BAY NEW MEDIA

                              "rl4518" <webforumsuser@macromedia.com> wrote in message
                              news:fnf1le$oi5$1@forums.macromedia.com...
                              > Thanks Arron,
                              >
                              > Your idea of "polling" is interesting. By polling, you mean that the
                              > action
                              > script in SWF is polling the web server for additional pages? I like the
                              > idea
                              > of not having to add anything special, but the direction of control I'm
                              > trying
                              > to create is from the web server to the flash player.
                              >
                              > I keep using the bouncing ball scenario, but at least it's simple and
                              > that's
                              > because my understanding is simple at this point.
                              >
                              > But, ... lets say an SWF has already been programmed with an animation
                              > and a
                              > bunch of action script routines that when called by "something", will move
                              > the
                              > ball, or label the ball, or cause new images to be displayed on the stage.
                              > I
                              > almost envision pre-programmed action script functions that when called
                              > will
                              > play the animation back in specific scenes , just like a movie script.
                              >
                              > 1) Browser asks http server for this SWF.
                              > 2) The flash player loads it and starts scene #1 (which might be the
                              > execution of a main() or default action script function)?
                              > 3) Now, nothing else happens and the user watches scene #1 repeat until
                              > the
                              > web server, which is running complex Perl or PhP scripts tells the Flash
                              > player
                              > to advance to scene #2.
                              > 4) Now, scene #2 might need parameters sent to it. Something not known
                              > when
                              > the SWF was created. Lets say the Ball gets a unique name that only the
                              > server
                              > side script knows after it begins to run, but after the SWF is loaded into
                              > the
                              > player. So in asking the flash player to run the action script for scene
                              > 2, the
                              > server might also pass the text string "herbert" and also a number X. The
                              > X
                              > might be how many units to move the ball and "herbert" is a label that
                              > should
                              > appear on the ball.
                              > 5) The scene #2 action script gets these parameters and does its magic to
                              > cause the name to appear on the ball and then move it X units.
                              > 6) The animation now stops in scene #2 awaiting the next server command.
                              >
                              > The big question for me is can we do this? In reading the discussion
                              > here, I
                              > see the following technologies:
                              >
                              > A) Flash and action script for creating the SWF and rendering the
                              > animation.
                              > B) Flash Communication/Media Server and the open source Red5 for flash
                              > remoting and AMF communications
                              > C) AMFPHP an open source version of the Flash Media Server
                              > D) AMF::Perl an AMF Perl Module
                              > E) Polling the Web Server
                              > F) XML Sockets
                              >
                              > Can anyone suggest from experience which of these (if any) might be able
                              > do
                              > accomplish what I need?
                              >
                              > Thanks for all of your help so far!
                              > Ray
                              >

                              • 12. Re: Server to Flash communication
                                rl4518 Level 1
                                Hi Patrick,

                                Thanks for the additional information. I'd be interested in your XMLs product. I might be able to do testing and validation, but right now I don't think I'd have much extra time for development. I'm still working out the issues I'm having with server-controlled Flash animation for a client that needs it now (you know how that is).

                                A few folks have mentioned the idea of an xml socket server to do communication from server to SWF. I can see how that would provide server control, but exactly what XML I send to do the communication is a mystery. I don't see much information on how to do that. I got lost in the Adobe documentation forest, so if it's there I'm probably looking in the wrong place. I've written binary socket servers in 'C' before so I understand the basics, but in the case of Flash, I'm not certain how to get the Player to attach to my socket and then read it in a language we both understand. Maybe that's AMF?
                                • 13. Re: Server to Flash communication
                                  abeall Level 3
                                  For an XMLSocket server, you just send XML documents back and forth as string data. In AS3 you would probably use E4X to manipulate the XML inside Flash. You use XMLSocket.send to send data back the server as a string, presumably formatted as a valid XML document (but I guess you could send any string data as long as your server can deal with it.)

                                  Sockets, on the other hand, are sending and receiving &quot;raw&quot; binary data. You can see all the ways you can read/write data to the socket in the documentation. Socket.readObject and Socket.writeObject serializes in AMF format, but you can also read/write in many other binary formats.
                                  • 14. Re: Server to Flash communication
                                    Level 7
                                    Hi rl14518,

                                    Email me at patrick (AT) baynewmedia (DOT) com and I'll forward you the
                                    XMLs parser. I'm reworking it for ActionScript 3 but this should suffice for
                                    most needs. About sockets, well, they're completely open. The data you send
                                    is entirely up to you. Unlike web servers which require specific protocols,
                                    or other serialization systems that require very specific XML formats,
                                    sockets can and do receive anything under the sun. Even simple text is okay.
                                    The XMLSocket isn't actually XML at all, it's just a socket that terminates
                                    its communication with a null character. Flash takes care of this
                                    automatically and most server-side socket listeners do too. If you wanted to
                                    send the string "gibberish", for example, that's perfectly fine, as long as
                                    your socket listener knows what to do with it. Certain implementations, like
                                    the Jabber protocol, require very specific formats so for those there's
                                    plenty of documentation. XMLs is fairly simple and compact for simple data
                                    transfers. Most languages should be able to make use of it *very* easily.
                                    It's even legible to humans. For example:

                                    <XMLs version="1.0">
                                    <string name="myStringvariable">This is a simple string</string>
                                    <number name="myNumbervar" value="13"/>
                                    <object name="myObject">
                                    <xml name="myXMLVariable"><a/><b/><c/></xml>
                                    </object>
                                    </XMLs>

                                    Here, a string, a number, an object, and an XML object within the object,
                                    are sent. XMLs is forgiving so that values can either be stuck into the
                                    "value" attribute or as a child text node. Parsing this and using it in
                                    almost any language should be pretty straightforward.

                                    Regards,
                                    Patrick Bay
                                    BAY NEW MEDIA

                                    "rl4518" <webforumsuser@macromedia.com> wrote in message
                                    news:fo8hgn$2ad$1@forums.macromedia.com...
                                    > Hi Patrick, Thanks for the additional information. I'd be interested in
                                    > your
                                    > XMLs product. I might be able to do testing and validation, but right now
                                    > I
                                    > don't think I'd have much extra time for development. I'm still working
                                    > out the
                                    > issues I'm having with server-controlled Flash animation for a client that
                                    > needs it now (you know how that is). A few folks have mentioned the idea
                                    > of
                                    > an xml socket server to do communication from server to SWF. I can see how
                                    > that
                                    > would provide server control, but exactly what XML I send to do the
                                    > communication is a mystery. I don't see much information on how to do
                                    > that. I
                                    > got lost in the Adobe documentation forest, so if it's there I'm probably
                                    > looking in the wrong place. I've written binary socket servers in 'C'
                                    > before so
                                    > I understand the basics, but in the case of Flash, I'm not certain how to
                                    > get
                                    > the Player to attach to my socket and then read it in a language we both
                                    > understand. Maybe that's AMF?
                                    >

                                    • 15. Re: Server to Flash communication
                                      abeall Level 3
                                      Patrick,

                                      Are you working on an XMLSocket server? What language?
                                      • 16. Re: Server to Flash communication
                                        Damon Edwards Level 3
                                        I would definitely be interested in helping with R&amp;D on this if you would like. Sounds very useful.