question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Properly getting a socket by ID

See original GitHub issue

I’ve asked on IRC what the proper way is to get a socket by id from the normal namespace. Supposedly it would be io.sockets.client(id). Unfortunately, this method does not exist. I figured it’d be a typo, and it should be io.sockets.clients(id). However, the clients method expects a room and then returns an array of all sockets in that room.

Thus, one could get a socket by id like so:

function getSocketById(id) {
    var clients = io.sockets.clients(),
        client  = null;
    clients.forEach(function(_client) {
        if (_client.id === id) return (client = _client);
    });
    return client;
}

This is obviously rather excessive. A much more efficient way would be to access the socket by id through a library (an object where id is the key).

From the source, another way to access a socket by id is io.sockets.socket(id);. Unfortunately, there are 2 problems here:

  • the method is flagged private
  • the method creates a new socket when the given id does not exist

Thus, this method cannot be used as passing an invalid or non-existant id would trigger the creation of a dead socket.

Is there no proper way to get a client from a namespace by id at the moment?

Issue Analytics

  • State:closed
  • Created 12 years ago
  • Comments:18 (2 by maintainers)

github_iconTop GitHub Comments

3reactions
fselcukcancommented, Oct 9, 2018

io.sockets.sockets is an array of sockets, so you can

var socket = _.findWhere(io.sockets.sockets, {id: 'mySocketId'});

using underscore or

var socket;
for (var i = 0; i < io.sockets.sockets; i += 1) {
  if (io.sockets.sockets[i].id === 'mySocketId') {
    socket = io.sockets.sockets[i];
  }
}

more general. Have in mind that io.sockets is the “/” namespace. For specific namespace: io.of(‘/ns’).sockets.

@romelperez But whole this comments thread is based on a comment of @tommedema stating that using forEach in your case some underscore.js function, is excessive. Not an array but a hash would be more efficient or convenient.

3reactions
romelperezcommented, Jan 21, 2015

io.sockets.sockets is an array of sockets, so you can

var socket = _.findWhere(io.sockets.sockets, {id: 'mySocketId'});

using underscore or

var socket;
for (var i = 0; i < io.sockets.sockets; i += 1) {
  if (io.sockets.sockets[i].id === 'mySocketId') {
    socket = io.sockets.sockets[i];
  }
}

more general. Have in mind that io.sockets is the “/” namespace. For specific namespace: io.of(‘/ns’).sockets.

Read more comments on GitHub >

github_iconTop Results From Across the Web

how to get socket.id of a connection on client side?
To get client side socket id for Latest socket.io 2.0 use the code below let socket = io(); //on connect Event socket.on('connect', ...
Read more >
The Socket instance (client-side)
Socket #id​. Each new connection is assigned a random 20-characters identifier. This identifier is synced with the value on the server-side.
Read more >
Learn Socket.io In 30 Minutes - YouTube
Socket.io is an amazing library for client/server communication, but it can be a bit confusing to get started with. In this video I...
Read more >
Everything you need to know about Socket.IO - Ably Realtime
This is the minimum setup to get the Socket.IO connection working. Let's go a bit further to get messages sent back and forth....
Read more >
Node.js Socket.io Complete Tutorial - Codedamn
We will divide our setup of Socket.IO into several steps. As a result, we will be able to get a general idea of...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found