Hello!

In the process of creating one application (game) on JS, it became necessary to instantly exchange data with the server (data, for example, this is the current position on the playing field, which often changes), which then must also be quickly read by other clients. I tried to implement this through an Ajax request to a PHP server handler. At first I tried an asynchronous request - an insane delay (which is logical)! Then, it is not asynchronous - all game processes began to “freeze” (after all, the subsequent JS code in this case will be executed only after data is received from the server). In general, I am in a hopeless situation ... Tell me please, is there a way to instantly exchange data with the server? How is the instant data exchange implemented in other online games, in which changing various game properties is quite frequent?

Thank you =)

  • websockets for example, event server (not php usually) - zb '
  • The bottom line is that there is support for dynamic queries. With sockets from js is this possible? - AseN
  • @ 0xFFh yes, sockets are the most. Response within playability. - lampa
  • @eicto, @lampa, ok, since sockets are the only way out, could you give me a link to the docks, where you can learn about it in as much detail as possible. And, yes, if the event server is usually written not in PHP, then what? - AseN
  • @ 0xFFh to start wikipedia: ru.wikipedia.org/wiki/WebSocket I repelled from them. If node.js, then by no means use socket.io because of its monstrosity. I would prefer github.com/Worlize/WebSocket-Node - lampa

2 answers 2

You need real-time interaction, not asynchrony. The HTTP protocol, through which the interaction is inherently based on a request-response system, is not intended for a channel connection, that is, when a permanent channel is established between subscribers.

As written above, use websockets, but not everyone has it yet (I mean on the client).

There is also comet technology - when a channel connection is emulated over http.

  • Yes, thanks, I already figured it out. The most suitable solution is socket technology, that is, an open two-way data transfer channel. This can be done, for example, using NodeJS on a dedicated server. - AseN
  • Once you figure it out, write in more detail what was used on the client and on the server, maybe pieces of code. If support degradation when the client does not support sockets? - vdann pm

Well, actually, you need asynchronous sending. For an asynchronous request is just that and asynchronous, which does not suspend the execution of the program until it is executed, but allows the program to run further, upon completion by calling the callback function.
If the speed of the asynchronous XHttpRequest request is not enough for you, then I don’t even know what to offer.
By the way, a little bit of information about your query:
Ajax requests / responses: how to make them lightning fast?

And a little more on the topic:
How to make a fast, lightweight, economic online chat with PHP + JS + (MySQL?) + (AJAX?)

  • This is hardly relevant for my asynchronous request, which, by the way, is executed every 0.01 seconds! This is bad for both the server and the client. - AseN