# How to implement Long Polling with PHP and flash?

I'm trying to make a multiplayer game but am a newbie to server side programming except that I know a little PHP. I learnt about this technique called long polling where the server can wait for fresh data to be available and then send it. How does one do long polling using PHP? Are there any libraries available. I'm already using ZendAMF, so how do I make zendamf and long-polling to co-exist?

• This would probably better fit in stackoverflow.com. Although there are many here who could help – brandon Apr 28 '11 at 17:08
• I am voting to close as off-topic..since we do need a 'migrate to stackoverflow' choice. This is related to gamedev in context, but the actual problem is all programming. – The Communist Duck Apr 28 '11 at 17:19
• @Duck while I agree its a programming question, I think that long-polling is quite common in turn-based multiplayer games... and by extension its gamedev enough to warrent staying. We don't turn away questions about moving sprites on a screen because thats a programming problem. – Nate Apr 28 '11 at 17:34
• @Nate Bross I know. The question I can see, however, applies to gamedev but would probably do better on SO..the close thing is democratic, so me alone does nothing :P – The Communist Duck Apr 28 '11 at 17:55

While you can, theoretically, write the server-side application to use the common request lifecycle (request -> processing -> response -> stop) fronted by a HTTP server, like you would would with a website, this approach tends not to be practically feasible for most games.

When writing a game, you will probably want to look into a shared everything design, as opposed to the shared nothing, stateless design used for a webpage. The reason for this is that you will want to be able to scale to a number of concurrent users for any given session (a match in Counter Strike, for example) where the game logic depends on a shared state.

Running one instance of PHP for every user will tie up a lot of resources, as well as force you to push the game state outside of the PHP process, neither of which goes very well with either scalability or performance.

Your best bet is likely to write a stateful, event-based application that is then run in PHP deamon mode. You would bypass the web server, and deal with network programming directly.

• While thats true, bypassing the web server means you must write your own server, the main advantage of using the web server is that its already written and preformance/stress tested and proven to be reliable. – Nate Apr 29 '11 at 16:38

On the client side (I do not have any experience using ZendAMF), you simply make a call with a very long timeout value, and as soon as you get a response, you make another call with another long timeout. The "long-polling" happens on the server.

On the server long-polling simply means that when your client application makes a "get" (or "post") to a PHP page, that page simply does not exit(); until there is data to be sent. So a short loop that continually checks the status of the data until it has changed. The loop that checks data will "block" until there is data.

There maybe libraries available that help with this, but it shouldn't be necessary unless your situation is quite complex.