views:

366

answers:

1

I am working on a project for a client whereby their cms will be flex 3 and php. There will be multiple clients using it at once and I am trying to make it to where when one person makes a change, the others will see it.

My Question Is: If I am using Flex 3 and PHP, how can I implement something that acts like 'server-push'. Is long polling a possible/good option? Or are there other options out there that can help me do the same task?

Any help would be greatly appreciated!

+2  A: 

The problem with long polling (aka AJAX Comet) and PHP is that PHP lacks a threading architecture that really makes it possible. Many of the other traditional languages are built to get a request and respond quickly in a serial manner.

Newer languages are getting to a point where they can support the concept fully. In fact you really want a framework and webserver that uses EPOLL(Event based/Async Socket IO) and/or microthreaded handling of connections so that you can handle thousands and of open connections at once without having to dedicate a full operating system thread to each open connection hitting your server. (OS threads have a finite resources)

I did a video on explaining the concepts of AJAX Comet/Long polling. You can see more on it here: http://www.youngtechstars.com/?p=466

Now... I did create really hacky proof of concept for way to do long polling with PHP. It required the use of a custom NGINX mod. First the request would come into a NGINX server which would dispatch the request off to a PHP process (using FastCGI). If I wanted the connection to stay open I would return a response with a custom HTTP header (in this case "X-NGINX-WAIT-UNTIL-KEY: a10x39" or something). That would signal NGINX to hold on to the connection and not return a response to the user yet. At some point later I would open a socket back to NGINX from PHP (or another process) when data was ready and pass in the key. Doing this would cause NGINX to do a second request and hit the PHP process again with the same request internally to the get the response for the user. It was a proof of concept and worked flawlessly but I never released it. There are issues with this model in clustered web environments because you have to track the server that has the open connection.

Flex, as with doing XmlWebRequest in plain-old javascript, isn't any issue with long polling since the request is handled asynchronously.

There a number of frameworks that can support this out of the box however:

  • Jetty WebServer - a pure Java web server that makes use of continuations and non-blocking IO (java.nio) to support lots of open connections.
  • XEP-0206: XMPP Over BOSH - XMPP (Jaber)’s HTTP Binding/Bosh system spec
  • ApacheMQ (Message Queue) - AJAX Comet/HTTP Long polling page
  • liberator - commercial web server built for AJAX Comet
  • lightstreamer - commercial web server built for AJAX Comet and HTTP Streaming
Zac Bowling