How can I reliably close websocket connections (using the Ruby API)?


It seems that whenever I start a connect to a stream using a websocket through DataSift’s Ruby streaming API, the websocket stays open even after my program closes. When I start my program up again, it starts a new websocket that connects to the same stream as before, piling up the number of websocket connection and making my billing rate go up significantly.

I’m not sure if I’m just using websockets incorrectly as I haven’t used them before, but I was wondering if this is a common issue with a known solution, or if I just need to be very careful that I close the websocket when the Ruby kernel closes. Having to close the websocket like that makes me nervous, though…

If it helps, my code looks something like this right now:


Our Ruby client library does not currently support WebSockets. We will be doig some more work on them next week, and hope to add WebSocket support then.


Oh, my mistake, I guess I’m actually using the HTTP access streaming library rather than the websocket one. Still, how can I make sure to stop all stream connections when my app closes? Is it best/safest to disconnect the stream when the app closes using Kernel.at_exit, or is there a better solution?


Take a look at some of the examples in our Ruby GitHub repo.

The best way to disconnect is to call the "consumer.stop()" method, before the app closes. Doing this explicitly closes the HTTP socket.

Have you tried running these examples, and if so, do you encounter the same disconnect issues when doing so?


Hey Jason, thanks for those examples. I’m now making sure to keep track of all my stream consumers in separate threads so that I can manually stop them all when the app closes. I don’t seem to be having any disconnect issues anymore. I also read that right now you guys only support multiple streams for Java and Node.JS/PHP. Are there any plans to add multi-streaming for the Ruby client library?


We would like to add multiple streaming to all of our client libraries at some point. We do plan to do some more work on all of our client libraries next week, so keep an eye on @DataSiftDev to stay up to date with all our latest news about our client libraries and platform updates.