Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Reducing server CPU usage with AJAX calls

I am working on a multiplayer HTML5 game that makes use of jQuery to poll a server for game state. At present, I'm querying the server with a .ajax call every 2000ms, to give the impression of real-time updates. I'm really not pushing too much data through...mainly just x/y position and a few character attributes...with only a few players being alive in my game instance.

The server itself is a shared-hosting server, and I'd like to try and keep my CPU hits as low as possible as I develop this game (and move it to something a bit higher-powered in the future).

Here is my current method of polling the server for data. Any and all suggestions are welcome, as server loading is kind of 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 cannot have free-running processes to open connections to (i.e. Node.js).

like image 589
the_e Avatar asked Oct 05 '22 23:10

the_e


1 Answers

First, you are saying that you are not pushing too much data. For me, you are pulling. Which is not exactly the same thing as you might know.

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

like image 182
Zakaria Avatar answered Oct 10 '22 03:10

Zakaria