Jump to content
  • Advertisement
Sign in to follow this  
Kobaltic

How do I push a php return to all clients

This topic is 2023 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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?

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites

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

Edited by Kobaltic

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites

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

Share this post


Link to post
Share on other sites
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 :-)

Share this post


Link to post
Share on other sites

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.

Share this post


Link to post
Share on other sites

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.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!