Hi
I am making a voip app and want to save users sockets in memory probably in redis. I want to ask how can I save ws references in redis? Each users can have multiple sockets at a time (logged in from different devices) and I want to send message to that user's sockets. Is there any id etc of socket that I can save that be used again to send message to that user?
There is no id, but you can add it yourself if needed, see https://github.com/websockets/ws/issues/859#issuecomment-253197752
Ok I can assign an id to the socket but if I have an id of socket that belongs to user how can I send message via that socket if I only know its id?
I need something like this, https://stackoverflow.com/questions/27055989/socket-io-1-0-x-get-socket-by-id
const map = new Map();
wss.on('connection', (ws) => {
const id = uuid.v4();
map.set(id, ws);
ws.on('close', () => map.delete(id));
});
function getSocketByid(id) {
return map.get(id);
}
Please use SO for this kind of questions.
Haha thanks a lot. Sorry for bothering. I wanted to ask if there is some built in function like socket.io for this. Thanks a lot for helping :)
No, there isn't. This library only implements the WebSocket API and nothing else. If you need more advanced features use a framework like Socket.IO, Primus, SocketCluster, etc.
Most helpful comment
Please use SO for this kind of questions.