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.

io.sockets.sockets deprecated - get all sockets in v3.

See original GitHub issue

It seems that io.sockets.sockets is deprecated in v3 and following does not work anymore:

socket.userId = id;

// Later
for (let s in io.sockets.sockets) {
	let socket = io.sockets.sockets[s];
	if (socket.userId === ...) {
		socket.emit(f, ...params);
	}
}

Tried also following (https://socket.io/docs/server-api/#namespace-clients-callback)…

io.clients((error, clients) => {
	if (error) throw error;
	console.log(clients);
});

But:

Uncaught TypeError: io.clients is not a function

Also io.engine.clients does not seem to expose client’s socket object, so if that’s the only way, I’d need to make custom client-id to userId table and try to keep it in sync, which feels bad idea. And if it really is, how it should be used exactly?

socket.on('v3test', function() {
	console.log(socket.id);
	for (let s in io.engine.clients) {
		let socket2 = io.engine.clients[s];
		console.log(socket2.id);
	}
});

In localhost with 1 client you’d expect 2 identical ids, but:

a9C3bHfHsfoA1aPJAAAB
FrOECbewjLGBnAzFAAAA

Please advice how to go through all connected clients in v3?

(and to clarify, the room approach is not viable in this case as the exact recipients are checked case-by-case)

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Reactions:1
  • Comments:6 (2 by maintainers)

github_iconTop GitHub Comments

1reaction
darrachequesnecommented, Nov 2, 2021

It is allowed to make io.on('connection', (socket) => { an async function?

Yes, totally 👍

0reactions
oshihiriicommented, Nov 2, 2021

I’m having to do a major refactor on multiple package implementations after a hiatus from a project.

I used to have:

io.clients((error, clients) => {
    if (error) throw error;
    console.log("io.clients list is currently:");
    console.log(clients);
    // this returns an array of socket id strings eg:
    // [6em3d4TJP8Et9EMNAAAA, G5p55dHhGgUnLUctAAAB]
});

within:

io.on('connection', (socket) => {
//...
}); 

In order to use await (per the examples here):

const ids = await io.allSockets();
console.log("io.allSockets() is currently:");
console.log(ids); 

The containing function needs to be async.

It is allowed to make io.on('connection', (socket) => { an async function?

Read more comments on GitHub >

github_iconTop Results From Across the Web

Migrating from 2.x to 3.0 - Socket.IO
The connected object (used to store all the Socket connected to the given Namespace) could be used to retrieve a Socket object from...
Read more >
How do I get a list of connected sockets/clients with Socket.IO?
In Socket.IO 0.7 you have a clients method on the namespaces. This returns an array of all connected sockets. API for no namespace:...
Read more >
ssl — TLS/SSL wrapper for socket objects — Python 3.11.1 ...
SSL version 3 is insecure. Its use is highly discouraged. Deprecated since version 3.6: OpenSSL has deprecated all version specific protocols. Use the...
Read more >
@types/socket.io - npm
This package has been deprecated. Author message: This is a stub types definition. socket.io provides its own type definitions, ...
Read more >
Reading from and Writing to a Socket (The Java™ Tutorials ...
The EchoClient example creates a socket, thereby getting a connection to the echo server. It reads input from the user on the standard...
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