Jump to content

  • Log In with Google      Sign In   
  • Create Account

How do I push a php return to all clients


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
9 replies to this topic

#1 Kobaltic   Members   -  Reputation: 217

Like
0Likes
Like

Posted 23 March 2014 - 03:03 PM

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?



Sponsor:

#2 rip-off   Moderators   -  Reputation: 8726

Like
1Likes
Like

Posted 23 March 2014 - 03:42 PM

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.

#3 Kobaltic   Members   -  Reputation: 217

Like
0Likes
Like

Posted 23 March 2014 - 03:51 PM

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, 23 March 2014 - 03:52 PM.


#4 rip-off   Moderators   -  Reputation: 8726

Like
1Likes
Like

Posted 23 March 2014 - 04:22 PM

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.

#5 Kobaltic   Members   -  Reputation: 217

Like
0Likes
Like

Posted 23 March 2014 - 06:49 PM

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



#6 hplus0603   Moderators   -  Reputation: 5706

Like
1Likes
Like

Posted 23 March 2014 - 07:15 PM

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 };

#7 Sudi   Members   -  Reputation: 711

Like
0Likes
Like

Posted 30 March 2014 - 01:21 AM

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.



#8 hplus0603   Moderators   -  Reputation: 5706

Like
0Likes
Like

Posted 30 March 2014 - 11:03 AM

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


enum Bool { True, False, FileNotFound };

#9 Sudi   Members   -  Reputation: 711

Like
0Likes
Like

Posted 30 March 2014 - 12:55 PM

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.



#10 hplus0603   Moderators   -  Reputation: 5706

Like
0Likes
Like

Posted 31 March 2014 - 05:43 PM

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 };




Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS