4

I have stream of data coming to me via an http hit. I want to update data in realtime. I have started pushing HTTP hits data to a redis pubsub. Now I want to show it to users.

I want to update user's screen as soon as I get some data on redis channel. I want to use ruby as that is the language I am comfortable with.

piyush
  • 601
  • 3
  • 23

2 Answers2

7

I would use Sinatra's "stream" feature coupled with EventSource on the client side. Leaves IE out, though.

Here's some mostly functional server side code pulled from https://github.com/redis/redis-rb/blob/master/examples/pubsub.rb (another option is https://github.com/pietern/hiredis-rb):

get '/the_stream', provides: 'text/event-stream' do
  stream :keep_open do |out|
    redis = Redis.new
    redis.subscribe(:channel1, :channel2) do |on|
      on.message do |channel, msg|
        out << "data: #{msg}\n\n" # This is an EventSource message
      end
    end
  end
end

Client side. Most modern browsers support EventSource, except IE:

var stream = new EventSource('/the_stream');
stream.onmessage = function(e) {
  alert("I just got this from the server: " + e.data);
}
bioneuralnet
  • 5,283
  • 1
  • 24
  • 30
1

As of I know you can do this via Faye check this link out

There are couple approach If you wish you can try

  1. I remember myself building a Long Polling server using thin and sinatra to achieve something like this now If u wish you can do the same

  2. I know of few like this and this flash client that you can use to connect directly to redis

  3. There is EventMachine Websocket implementation u can use and hook it up with HTML 5 and Flash for non HTML 5 browser

  4. Websocket-Rack

Other Approach you can try just a suggestion since most of them arent written in ruby

  1. Juggernaut ( I dont think it based on Redis Pub-sub Thing also there used to ruby thing earlier not sure of now)

  2. Socket.io

  3. Webd.is

  4. NULLMQ Not a redis pub sub but this is Zero MQ implementation in javascript

There are few other approach you can find If u google up :)

Hope this help

Community
  • 1
  • 1
Viren
  • 5,812
  • 6
  • 45
  • 98