Game Development Stack Exchange is a question and answer site for professional and independent game developers. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

Instead of a game loop, I want something different: My game does not have much action going on; just moving users over a tile map with correct timers in between.

Since I am using Socket.IO, I want to run a "mini game loop" inside the io.sockets.on("connected"-part of my script. I am thinking to run a setImmediate checking a few variables to know what to do with the player.

The way I understand socket.IO, it would create this setImmediate function for each client that joins the server. (Because it runs inside the "connected" event handler.)

Let's say 1000 players join the game. If what I fear is true, this approach seems bad. Have I misunderstood setImmediate?

share|improve this question
It is late at night. Tell me if this makes no sense, so I can edit it. – Oliver Schöning Apr 21 '13 at 12:55
Your server architecture should strive to maintain asynchronous reading/writing capabilities for each client that is connected to it, as well as the ability to manage the overall state of the game. The issue with your suggestion is that it ties together the game logic with the networking back end. Ideally your game should simply talk to the networking back end through a facade, while the actual implementation is kept decoupled, and maintainable. – Evan Apr 21 '13 at 14:50
So what you are saying is: Gameloop & for gods sake don't block the IO ? :) – Oliver Schöning Apr 22 '13 at 7:03
Dont block the IO, and try to separate the game code from your networking back end. There is always going to be a point where your game code has to talk to the networking code, and this is where the facade comes in. – Evan Apr 22 '13 at 14:16
up vote 3 down vote accepted will emit connected event for every connected socket. So in your case you will create loads of loops there.
It is better to have list of sockets, and one loop separately.

As you've said - your case is simple - so keep it simple, do not overcomplicate.

var sockets = { };

server.on('connected', function(socket) {
    // runs for every connection
    sockets[] = socket;

    socket.on('disconnect', function() {
        delete sockets[];

var UPS = 20;
var gameLoop = setInterval(function() {
    var socket;
    for(var id in sockets) {
        socket = sockets[id];
        // ...
}, 1000 / UPS);
share|improve this answer
Hehe, this is a year old! Have some Karma that you came for and get out :p – Oliver Schöning Feb 21 '14 at 18:56
It's actually how I do it these days. That, and/or Array Iteration. – Oliver Schöning Feb 21 '14 at 18:56

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.