views:

1368

answers:

6

I'm trying to create a small and basic "ajax" based multiplayer game. Coordinates of objects are being given by a PHP "handler". This handler.php file is being polled every 200MS, by using ajax.

Since there is no need to poll when nothing happens, I wonder, is there something that could do the same thing without frequent polling? Eg. Comet, though I heard that you need to configure server side applications for Comet. It's a shared webserver, so I can't do that.

Maybe prevent the handler.php file from even returning a response if nothing has to be changed at the client, is that possible? Then again you'd still have the client uselessly asking for a response even though something hasn't changed yet. Basically, it should only use bandwidth and sever resources if something needs to be told to the client, eg. the change of an object's coordinates.

Any ideas? :)

+1  A: 

The server must take part in this. Check with the hosting provider what modules are available. Or try to convince them to support Comet.

Maybe you should consider a small Virtual Private Server (VPS) for this.

PEZ
+7  A: 

Comet is generally used for this kind of thing, and it can be a fragile setup as it's not a particularly common technology so it can be easy not to "get it right." That said, there are more resources available now than when I last tried it ~2 years ago.

I don't think you can do what you're thinking and have handler.php simply not return anything and stop execution: The web server will keep the connection open and prevent any further polling until handler.php does something (terminates or provides output). When it does, you're still handling a response.

You can try a long polling technique, where your AJAX allows a very large timeout (e.g. 30 seconds), and handler.php spins without responding until it has something to report, then returns. (You'll want to make sure the spinning is not resource-intensive). If handler.php "expires" and nothing happens, have it exit and let AJAX poll again. Since it only happens every 30 seconds, it will be a huge improvement over ~5 times a second. That would keep your polling to a minimum.

But that's the sort of thing Comet is designed for.

Adam Bellaire
+5  A: 

As Ajax only offers you a client server request model (normally termed pull, rather than push), the only way to get data from the server is via requests. However a common technique to get around this is for the server to only respond when it has new data. So the client makes a request, the server hangs on to that request until something happens and then replies. This gets around the need for frequent polling even when the data hasn't changed as you only need the client send a new request after it gets a response.

Since you are using PHP, one simple method might be to have the PHP code call the sleep command for 200ms at a time between checks for data changes and then return the data to the client when it does change.

EDIT: I would also recommend having a timeout on the request. So if nothing happens for say 2 seconds, a "no change" message is sent back. That way the client knows the server is still alive and processing its request.

David Arno
Interesting - however, what if something does change within eg. 200ms? It'll then take a relatively long time before the new check is done, since the interval is so long, right? Any way to fix that, or did I misunderstood you?
Tom
+3  A: 

Since this is tagged “html5”: HTML5 has <eventsource> and WebSocket, but the implementation side is still in the future tense in practice.

Opera implemented an old version of <eventsource> called <event-source>.

hsivonen
+1  A: 

One thing to add on the long polling suggestions: If you're on a shared server, this solution will have limited scalability, as each active long poll will keep a connection (and a server-side process to service that connection) active. Your provider most likely has limits (either policy-defined or de facto) on the number of connections you can have open at a time, so you'll hit a wall if you have more sessions/windows than that playing concurrently.

Dave Sherohman
+3  A: 

Here's a solution - use a SaaS comet provider, such as WebSync On-Demand. No server resources to worry about, shared hosting or not, since it's all offloaded, and you can push out the information as needed.

Since it's SaaS, it'll work with any server language. For PHP, there's already a publisher written and ready to go.

jvenema
I've accepted it though I will not be using it as it is not free nor open source.
Tom
If you only have a small # of users, you can use the 10 free users at no cost forever if you want, but I understand that if its a personal project, solutions with a cost associated are not always viable.
jvenema
@Tom, SaaS at a minimum has hardware and maintenance costs, leaving out software licensing altogether.
Anton