Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Broadcast / client id #81

Closed
michael opened this issue Jun 10, 2012 · 12 comments
Closed

Broadcast / client id #81

michael opened this issue Jun 10, 2012 · 12 comments

Comments

@michael
Copy link

michael commented Jun 10, 2012

Do you have a concept of a unique client id built in? I wonder how I'd address certain clients and send them messages. For a broadcast I can iterate over wss.clients. But I wonder what's the best way to pick individual ones. What does actually happen if a client disconnects. It just gets removed from the wss.clients list? I guess the best way to do it is keeping track of clients manually, once they connect.

Something like that:

wss.on('connection', function(ws) {
  // Register client
  clients[numClients++] = ws;
});

Would that make sense?

I like that the lib is very low level and doesn't introduce much magic, but some recipes on how to do A or B would be nice to have. :)

@einaros
Copy link
Contributor

einaros commented Jun 11, 2012

wss.clients will always be the list of current clients. There's no such thing as a session which lives beyond an actual connection for WebSockets, so if you need to recognize the same client over and over again, you'll have to rely on parsing incoming cookies - or an initial handshake.

Similarly there's no such thing as an id - but neither should there be. If your application requires an id, you can easily add it as you show above. It makes perfect sense.

Regarding some recipes you are absolutely right. Some time should be spent writing documentation for different scenarios. I'll bring that into consideration for future versions!

@michael
Copy link
Author

michael commented Jun 11, 2012

Thanks. That's good to know. I'm working a slightly higher level JSON-based communication protocol to realize realtime applications. Very minimal somewhere between RPC and plain messages, with a straight-forward interface. :) I'll let you know once a working prototype is ready. I'd be interested in what you think. I could cover some by-example documentation by describing how I solved my problem using the ws module.

In order to do a reverse lookup by client-id I need to pollute the ws object, which is not exactly elegant. So I wonder if you'd consider assigning unique client-ids natively (just a counter would be fine). Maybe this makes sense to add to ws directly, shouldn't hurt too much and could be useful in general. What do you think?

// Register client on connect
openSession: function(ws) {
  ws.id = this.numClients += 1;
  this.clients[ws.id] = ws;
},

// Unregister client on disconnect
closeSession: function(ws) {
  delete this.clients[ws.id];
},

@einaros
Copy link
Contributor

einaros commented Jun 12, 2012

I'll consider that for the next version :) Shouldn't be a problem.

@michael
Copy link
Author

michael commented Jun 12, 2012

Thanks. :)

@vishr
Copy link

vishr commented Jun 22, 2012

+1

@zsj2145676
Copy link

i do need a specefic client id ... so i could use it after close the client connection

@IonicaBizau
Copy link

@einaros Any progress on this?

@gblazex
Copy link

gblazex commented Apr 24, 2015

+1

1 similar comment
@cdupetit
Copy link

cdupetit commented Oct 8, 2015

+1

@0cv
Copy link

0cv commented Dec 1, 2015

I'm doing something very similar, than the example above, just instead of storing a meaningless id, I use the sessionID provided by Express (and then I can do something like clients[req.sessionID].send(...) but this would be great if wss.clients could do that out of the box.

@lpinca
Copy link
Member

lpinca commented May 12, 2017

I think it's better to not add an id generator in this lib as there are multiple ways to generate ids and they depend on the application.
Closing, feel free to continue discussing on the closed thread.

@lpinca lpinca closed this as completed May 12, 2017
@XiaoChenYung
Copy link

Is there have a solution? Who can give me a URL to lean about this question..

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

10 participants