Search code examples
javascriptnode.jssocket.ioqueueing

How to make a queue system with game rooms using node.js and socket.io?


I'm trying to implement this idea but I don't know what's the best way to do it.

There should be a button to queue up for a game, and there should be game rooms with 3 players in each.


Solution

  • Since your question is rather vague, I will give you a general workflow.

    1. An Express/HTTP server starts with a listening websocket.
    2. User loads page. Page loaded and attempt to make a websocket connection with the server.
    3. Websocket established. Shows "Join Queue" button on the game.
    4. Server updates client with available rooms in real time. Game can choose to show each room or just hide them in the background so that when player clicks on "Join Queue", he will be connected to the first available room.
    5. User clicks on "Join Queue" and joins a room.

    Sample Code for joining a room after connection established.

    // io is exposed as a global variable in the client once you import the library.
    // Join an existing room on connection established
    io.on('connection', function(socket){
        socket.join('some room');
    });
    // Establish a websocket connection
    var socket = io();