How do I push a php return to all clients

Started by
8 comments, last by hplus0603 10 years ago

I have client side written in javascript using http://phaser.io. I use jQuery AJAX to send information to my php server. I then process it in PHP and MySQL as needed. Then I use json_encode to encode the php returns and then echo out the result back to AJAX. I take it back in then parse it back out for javascript.

My problem is that it works great for one player. I am not sure how to set up so that the PHP echo is sent to all the clients to update them. I was thinking about setting up a game id and recording all the clients IP addresses. Store them into the database. Then see if PHP has a function to send the information back to the IP address. Not sure if it will work though. Any other ideas?

Advertisement
It won't be possible to do this the way you describe. Assuming a basic LAMP setup, each client connection essentially creates a new, independent PHP process for the duration of the connection - it is torn down at the end. Web browsers do not listen for incoming connections from servers, so it isn't possible for the server to open a connection to a former client. In addition, most clients will be behind NAT and firewall devices, even if they were actively listening for requests.

One of the simplest things you can do is have each client periodically make an AJAX request to the server, checking for recently updated information. The frequency of this polling is something you'll have to tune to the desired responsiveness versus the bandwidth required.

A single long running custom server process, in conjunction with newer APIs such as WebSockets, or using plugins like Java or Flash, would be able to do something like what you describe. The feasibility of these potential solutions depends on your hosting options and what kind of browser support you want to achieve, as well as the time it might take you to master any unfamiliar technologies.

Can you describe your game in a bit more detail? A fast paced action game has a much different set of networking requirements than a turn based strategy game, for example.

Thanks for some more information. The game is a turned based trading card game so time and bandwidth won't be much of an issue. However I do want to do faster games down the road and would like the correct way to handle this.

I have my own VPS and I can put anything needed on it. I don't have much of a problem learning new technologies.

Could I use something like node.js or websockets to handle connections and still use PHP as the server?

edit: corrected typos

I haven't actually developed games with these technologies, so I'll refrain from recommending any specific one.

That said, I'd probably go with simple AJAX polling for such a game. For a fast paced action game, I'd strongly consider writing a dedicated server process of some kind. I personally wouldn't write such a server in PHP, but I'm sure it could be done.

Thanks again for the information. I think I am going to look into some different server options and see what I come up with.

How this is typically solved, on top of PHP, is that players poll every once in a while (using client-side JavaScript or whatever.)
If you want something better than that, you need to use websockets, either using socket.io on node.js, or usign something custom. We ended up building a custom system on top of Erlang, because we have to serve > 150,000 simultaneous users. node.js doesn't quite do that :-)
enum Bool { True, False, FileNotFound };

I would really suggest you take a look at firebase(https://www.firebase.com/). Its a great service that allows for this realtime updating in browser apps.

Another technology that allows this is Meteor: https://www.meteor.com/

enum Bool { True, False, FileNotFound };

But meteor requires a meteor server. I understood a php backend is required. firebase uses the firebase service and you can use any backend you like.

meteor requires a meteor server


No, the whole point of Meteor is that it runs anywhere there is node.js servers. Which is many hosting places.

If all you get is PHP, then neither Meteor nor FireBase will work, because PHP hosting doesn't allow persistent processes, period -- so you have to do (long) polling.
enum Bool { True, False, FileNotFound };

This topic is closed to new replies.

Advertisement