M
M
Menjoy2014-07-12 13:45:49
JavaScript
Menjoy, 2014-07-12 13:45:49

Socket.io: how to get a specific socket by id?

Good afternoon.
There is the following situation.
From server.js I forward to another file io = require('socket.io')(server);
A request comes from another server with a cookie containing the id of the desired socket.
The goal is to get the socket object knowing its id, because you need to edit his session.
Getting through io.sockets.sockets[socketId] and through io.sockets.socket(socketId) fails.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
M
Menjoy, 2014-07-12
@Menjoy

sockets.io 1.0.6
I got it like this.

if (io && io.sockets && io.sockets.sockets) {
        var sockets = io.sockets.sockets,
            socket = null;

        if (sockets.length) {
            for (var i = 0; i < sockets.length; i++) {
                var _socket = sockets[i];

                if (_socket.id && _socket.id === socketId) {
                    socket = _socket;
                    break;
                }

            }
        }

        return socket;
    }

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question