Several Websocket servers (in the same process)
See original GitHub issueIn WebsocketServer
class, the clients
attribute is defined at the class level, not as an instance attribute. Is there any good reason for this design?
This means that when you use several WebsocketServer
(on different ports) and use the send_message_to_all
method on a server instance, the message is sent to all clients, including the clients that connected to another server !
The fix is trivial and I can make a Pull Request if this could be changed.
Thanks for this very useful lib BTW ! 😄
Issue Analytics
- State:
- Created 5 years ago
- Comments:8 (1 by maintainers)
Top Results From Across the Web
Multiple websocket servers on one machine - Stack Overflow
Is there anyway to use multiple socket cluster running on same machine on same port? No. Not directly. You need some sort of...
Read more >A Simple Multi-Client WebSocket Server - Forty Years of Code
Just four methods are needed for a complete, correct echo server: Start and Stop , which are self-explanatory, Listen which implements the HTTP ......
Read more >Writing WebSocket servers - Web APIs | MDN
WebSocket servers are often separate and specialized servers (for load-balancing or other practical reasons), so you will often use a reverse ...
Read more >WebSocket - The Modern JavaScript Tutorial
On the other hand, wss:// is WebSocket over TLS, (same as HTTPS is HTTP over TLS), ... else { // e.g. server process...
Read more >Using multiple nodes | Socket.IO
the WebSocket transport does not have this limitation, since it relies on a single TCP connection for the whole session. Which means that...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
all code blocks are pseudo code. They have not been tested specifically in the websocket_server library
OK so this an idea that works in a test setup of simple class instances and it should as well in the websocket_server lib.
Now what i did here was I had the instance override the class method of the parent class. This way it can be used both ways.
I think this would be the most elegant solution. and the one that makes the most sense.
being able to access one instance of a websocket server from another instance I do not believe is the best idea. You may even consider changing the clients container to have a mangled name. Then add a property that will iterate the mangled name container and only return clients that pertain to it.
In the original code there was no way to tell what client belong to what websocket connection even if you iterated over the devices container. No marker that was easily had. Without really digging into it but I think that client[‘handler’].server might point back to the websocket instance, but I am not sure. Event if you do not make clients a mangled name at least make it a private one.
This adds the mangled name which offers protection against inadvertently sending data to the wrong websocket connection if accessed from a websocket instance.
So the original design simply dealt with a single instance. But all this makes sense so
v0.5.1
should have this fixed.