faye / faye-websocket-ruby

Standards-compliant WebSocket client and server
Other
1.04k stars 96 forks source link

Finding a connected socket via its id #119

Closed navidmatin closed 4 years ago

navidmatin commented 4 years ago

I'm using this library and I was wondering if there is a way on the server side to find a connected socket via its object_id, or send message to a specific socket by its object_id?

I am asking this because I'd like to send messages to specific socket from the server, and wait for the client response via the same socket. Right now I store all of the connected clients on the server in a map which is not the best. I'd like to store the object_ids in redis and just use that information to find the correct socket when the server needs to send a message to a client.

jcoglan commented 4 years ago

This library does not maintain a record of existing sockets -- your call to Faye::WebSocket.new is the first time the library code is entered, so it doesn't know about the server's connection pool, or track its own instances which would likely lead to memory leaks.

If you need to identify particular clients you usually need to invent a protocol for doing that. As you said, you can store them in a map if you're running a single server, but if you're running multiple servers backed by Redis this gets trickier.

You might want to look at Faye, the product this library was extracted from, which hides all the network-level details and lets you address messages to channels that clients can subscribe to.