Skip to content

Instantly share code, notes, and snippets.

@pirj
Created August 17, 2012 10:13
Show Gist options
  • Star 16 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save pirj/3377714 to your computer and use it in GitHub Desktop.
Save pirj/3377714 to your computer and use it in GitHub Desktop.
Sinatra streaming + Redis PubSub
require 'bundler'
Bundler.require
class App < Sinatra::Base
helpers Sinatra::Streaming
get '/' do
subscriber = EM::Hiredis.connect
subscriber.psubscribe '*'
stream :keep_open do |out|
subscriber.on(:pmessage) do |key, channel, message|
subscriber.punsubscribe '*' if out.closed?
out << "#{key}, #{channel}, #{message}" unless out.closed?
end
end
end
end
source 'https://rubygems.org'
gem 'sinatra'
gem 'sinatra-contrib', require: 'sinatra/streaming'
group :development do
gem 'thin'
gem 'pry-rails'
end
gem 'eventmachine', "= 1.0.0.rc.4"
gem "redis", "~> 3.0"
gem "hiredis", "~> 0.4.5"
gem 'em-hiredis'
@pirj
Copy link
Author

pirj commented Sep 7, 2013

Example client code (JS)

var xhr = new XMLHttpRequest()
xhr.open("GET", "/", true)
xhr.onprogress = function () {
  alert(xhr.responseText)
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment