Giter Site home page Giter Site logo

Broadcast / client id about ws HOT 12 CLOSED

michael avatar michael commented on May 7, 2024
Broadcast / client id

from ws.

Comments (12)

einaros avatar einaros commented on May 7, 2024 1

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!

from ws.

IonicaBizau avatar IonicaBizau commented on May 7, 2024 1

@einaros Any progress on this?

from ws.

michael avatar michael commented on May 7, 2024

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];
},

from ws.

einaros avatar einaros commented on May 7, 2024

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

from ws.

michael avatar michael commented on May 7, 2024

Thanks. :)

from ws.

vishr avatar vishr commented on May 7, 2024

+1

from ws.

zsj2145676 avatar zsj2145676 commented on May 7, 2024

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

from ws.

gblazex avatar gblazex commented on May 7, 2024

+1

from ws.

cdupetit avatar cdupetit commented on May 7, 2024

+1

from ws.

0cv avatar 0cv commented on May 7, 2024

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.

from ws.

lpinca avatar lpinca commented on May 7, 2024

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.

from ws.

XiaoChenYung avatar XiaoChenYung commented on May 7, 2024

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

from ws.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.