Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to implement dual communication between server and client via http

I have a AJAX client which must receive messages from server. Some messages from server not like request-response type. For example, imaging game in which players can enter. Server must send to client info about player entering. But how can server send message to client via http without request from client? Only decision that i can invent: client send request to server (getNewPlayerEnter request) with big timeout, server checks state of player set, if in set new players are, then server send info to client, if not server 'sleeps' on some time, and after 'sleeping' server checks players set again. I think my desicion a little stupid(maybe not little). How implement it right?
p.s. sorry for my english

like image 367
Alex Sebastiano Avatar asked Oct 20 '25 03:10

Alex Sebastiano


1 Answers

What you are referring to is "polling" where your client side code will make a request to the server every X seconds to check for updates.

It's an easy to implement solution, which is why you see it so often. However, a better solution is to use Comet.

In 2006, Alex Russell coined the term "Comet" to identify a technique whereby the client makes and HTTP request to the server and the server keeps that connection open indefinitely. With this open connection, the server can then push updates to the client in the response when there are updates.

When the response is received, the client immediately opens a new long-lived HTTP connection.

I encourage you to do a search for "Comet" and whatever server-side language you are using to find a framework that will get you started.

Comet Daily has some really great demos, from chat applications to games, that use Comet. If you have a tool like Firebug or Chrome Debugger, you can observe the response/request cycle of these real-time applications.

http://cometdaily.com/2009/10/23/concrete-comet-examples/

like image 158
jmort253 Avatar answered Oct 21 '25 18:10

jmort253



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!