Reduce server CPU usage with AJAX calls

I am working on an HTML5 multiplayer game that uses jQuery to query the server for the state of the game. I am currently requesting a server with a .ajax call every 2000 ms to impress real-time updates. I really don't push too much data through ... basically just the x / y position and a few character attributes ... with only a few players living in my game instance.

The server itself is a shared hosting server, and I would like to try to keep my processors as low as possible when I develop this game (and move it to something more powerful in the future).

Here is my current server polling method for data. Any suggestions are welcome, as server loading is a new area for me.

$.ajax({ type:'GET', url:"controller.php?a=player-info", dataType:'json', success: function(data){ //parse data } }); .... setInterval(getPlayerInfo,2000); 

One thing I should mention is that since this is on a shared server, I don't have free processes to open connections (e.g. Node.js).

+4
source share
2 answers

First, you say that you are not pushing too much data. For me you pull. This is not exactly the same as you may know.

You can use Websockets, long polling, or events sent by the server. ( Read this article )

+3
source

From what you have said so far, it is not clear that the processor will be a bottleneck for you, but here are some tips that can help you save server resources.

Consider using WebSockets instead of polling AJAX. This can eliminate many of the costs associated with useless "unchanged" requests by switching to a more push-oriented architecture.

You will also probably want to use memcached or some other level of RAM-based data storage (like Redis ) to avoid database bottlenecks.

+1
source

Source: https://habr.com/ru/post/1446327/


All Articles