Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Socket.IO 4.0.1: Get socket by id

I used

const my_socket = io.sockets.connected['YnwlYH-gCKT2K9jEAAAu'];

a lot.

https://stackoverflow.com/a/24464450/1707015

Is it broken in 3.X and 4.0.1?

$ npm list socket.io
[email protected]

$ npm list socket.io-client
[email protected]
# Object.keys(io.sockets):
_events,_eventsCount,_maxListeners,sockets,_fns,_ids,server,name,adapter

# io.sockets.connected:
undefined

# io.sockets.connected['YnwlYH-gCKT2K9jEAAAu']:
TypeError: Cannot read property 'YnwlYH-gCKT2K9jEAAAu' of undefined
    at Socket.<anonymous> (/path:row:char)
    at processTicksAndRejections (internal/process/task_queues.js:86:5)

I can get it run with version 2 (instead of 3 or 4):

$ npm install --quiet --save-dev [email protected] [email protected]

Yes, this is literally an X - for the newest sub version.

Do you have any refactoring instructions?

like image 955
qräbnö Avatar asked Mar 03 '26 21:03

qräbnö


1 Answers

This question and answer is probably a (newer/better) duplicate of https://stackoverflow.com/a/66835961/1707015. Sorry, I only saw it now. Credits to Sanjay Nishad.


const my_socket = io.sockets.sockets.get('YnwlYH-gCKT2K9jEAAAu');
like image 64
qräbnö Avatar answered Mar 06 '26 11:03

qräbnö