Comments (12)
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.
@einaros Any progress on this?
from ws.
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.
I'll consider that for the next version :) Shouldn't be a problem.
from ws.
Thanks. :)
from ws.
+1
from ws.
i do need a specefic client id ... so i could use it after close the client connection
from ws.
+1
from ws.
+1
from ws.
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.
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.
Is there have a solution? Who can give me a URL to lean about this question..
from ws.
Related Issues (20)
- Sec-WebSocket-Accept not found HOT 7
- ws issues with custom hostname HOT 2
- WebSocketServer.address() error needs more context HOT 2
- clientTracking - the client is not destroyed if the server closes or terminates the connection. HOT 2
- The code isn't working. HOT 14
- Is there anyway to disable the websocket closeTimeout? HOT 9
- Unhappy TypeScript when using compilerOptions: module: Node16 || NodeNext HOT 1
- Support sending Blob HOT 13
- WebSocket Ping-Pong Timeout and Connection Closure Failure HOT 3
- terminate() doesnt terminate instantly HOT 6
- ws doesn't work with sveltekit's adapter-cloudflare HOT 4
- Websocket opens randomly not everytime. HOT 5
- While building websocket-api:9.4.48.v20220622 with UAT, failed. (Test Case Failure) HOT 7
- query: difference between ws.onmessage = handler and ws.on('message', handler) HOT 2
- Messages are dispatched while microtask queue is not empty HOT 6
- Uhhh, what does .isAlive do again? HOT 2
- RangeError: Invalid WebSocket frame: MASK must be set HOT 1
- Invalid dns names should not cause an uncatchable fatal exception HOT 2
- Incorrect/incomplete documentation HOT 3
- Catching message exceeding maxPayload without automatically closing the connection HOT 4
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from ws.