uNetworking / uWebSockets.js

μWebSockets for Node.js back-ends :metal:
Apache License 2.0
7.86k stars 569 forks source link

missing `ws` unique id for each connection string #844

Closed StarMonk closed 1 year ago

StarMonk commented 1 year ago

I am trying to store unique ws for each connected client so that I can re-use it to send messages to the connected client.

How isSubscribed handles and stores each connection string?

e3dio commented 1 year ago

https://github.com/uNetworking/uWebSockets.js/discussions/843

let id = 0;
const wsMap = new Map();

app.ws('/', {
   open: ws => {
      ws.id = ++id;
      wsMap.set(id, ws);
      console.log('open', ws.id);
   },
   close: (ws, code) => {
      wsMap.delete(ws.id);
      console.log('close', ws.id, code);
   }  
});

wsMap.get(id).send('Hello');
StarMonk commented 1 year ago

I didn't realize, it was that simple.

Thanks