1. I'm trying to hold a connection open for 30 seconds for long polling. Is this the right way to do it ? https://gist.github.com/2476212 (Note: Holding it open for only 5 seconds in this gist).
2. Which redis driver should I use with goliath? em-synchrony recommends the official redis-rb driver which has synchrony built in but in https://github.com/jwarchol/redis_pubsub (Goliath with redis pub-sub), the author recommends using EM hiredis driver.
Nope. That will close the connection immediately. See this example: https://github.com/postrank-labs/goliath/blob/master/examples/content_stream.rb#L37
2. Which redis driver should I use with goliath? em-synchrony recommends the official redis-rb driver which has synchrony built in but in https://github.com/jwarchol/redis_pubsub (Goliath with redis pub-sub), the author recommends using EM hiredis driver.If you're streaming, you can use redis_pubsub just fine.. Look at the example above. You likely just want to connect the Redis callback to the stream callback and let them pipe the data automatically (also look at the amqp example in the repo.. it does exactly this).
I think the confusion arose because I mentioned redis pubsub. I'm not using redis pubsub (yet) but I'm using redis and I want to know what's the right driver.
Why bother holding the connection open if the client is just going to poll periodically anyway?
Instead of use long-polling, you're probably much better off using SSE: http://www.igvita.com/2011/08/26/server-sent-event-notifications-with-html5/There are good polyfills for SSE on older browsers as well (check comments on the post).
Thanks. I think I will use that.I'm curious though. What is the recommended way to handle Ajax long polling in Goliath?
I'm curious though. What is the recommended way to handle Ajax long polling in Goliath?Return "streaming response" constant in response_env, and then hook up some event emitter to your env.stream_send function, in which you can also call connection close. Pretty simple. :-)Like this? https://gist.github.com/9a2d05804eb6a321ad26What are the advantages over just looping over with a EM::Synchrony sleep inside the loop ? One advantage is that it is easy to keep the connection alive by emitting a response frequently.
Yes, that's pretty close, but a few improvements:- You need to cancel the timer in your on_close callback, otherwise the client can close the connection but the timer will continue firing- If possible, instead of doing polling, use a pub-sub system to get notifications of updates -- although this is obviously dependent on the semantics of the service you're checking.In theory, yes you can use a loop { EM::Synchrony.sleep n }, but you'll be better off using the timers - it's more idiomatic evented code.ig
Thanks for your patience Ilya :) I would like to use redis pub sub but I haven't yet figured out how to use multiple channels yet. All the examples showredis subscribing to a single channel and publishing to a single EM::Channel.
One reason to use EM::Channel in a single channel setup is toreduce the number of connections opened to redis right? Are there any other?