-
-
Save Thorsson/8668272 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
source 'http://rubygems.org' | |
gem "hiredis", "~> 0.3.1" | |
gem "em-synchrony" | |
gem 'em-hiredis' | |
#gem "redis", "~> 2.2.0", :require => ["redis/connection/synchrony", "redis"] | |
gem "goliath" | |
#gem 'em-synchrony', :git => 'git://github.com/igrigorik/em-synchrony.git' |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html> | |
<html> | |
<body> | |
<h3>Hello SSE!</h3> | |
<script> | |
function getQuerystring(key, default_) | |
{ | |
if (default_==null) default_=""; | |
key = key.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]"); | |
var regex = new RegExp("[\\?&]"+key+"=([^&#]*)"); | |
var qs = regex.exec(window.location.href); | |
if(qs == null) | |
return default_; | |
else | |
return qs[1]; | |
} | |
var channel = getQuerystring('channel') || "foo"; | |
var source = new EventSource('/events?channel=' + channel); | |
// new connection opened callback | |
source.addEventListener('open', function(e) { | |
console.log('connection opened'); | |
}, false); | |
// subscribe to unnamed messages | |
source.onmessage = function(e) { | |
console.log(e); | |
document.body.innerHTML += e.data + '<br />'; | |
alert(e.data); | |
}; | |
// connection closed callback | |
source.addEventListener('error', function(e) { | |
if (e.eventPhase == EventSource.CLOSED) { | |
console.log('connection closed'); | |
} | |
}, false); | |
</script> | |
</body> | |
</html> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# | |
# This is now a working example! | |
# | |
require 'rubygems' | |
require 'em-hiredis' | |
require 'goliath' | |
class Wizard < Goliath::API | |
use Rack::Static, :urls => ["/index.html"], :root => Goliath::Application.app_path("public") | |
use Goliath::Rack::Params | |
attr_accessor :redis | |
def response(env) | |
path = env[Goliath::Request::REQUEST_PATH] | |
return [404, {}, "Not found"] unless path == "/events" | |
channel = env.params[:channel] | |
return [404, {}, "Channel required"] if channel.nil? || channel == "" | |
env.logger.info "Connecting to channel '#{channel}' for updates" | |
env['redis'] = EM::Hiredis.connect | |
env['redis'].subscribe(channel) | |
env['redis'].on(:message) do |chn, msg| | |
env.logger.info "Message received from channel #{chn}: #{msg} (We're looking for #{channel})" | |
if chn === channel | |
res = env.stream_send("data:#{msg}\n\n") | |
end | |
end | |
streaming_response(200, {'Content-Type' => 'text/event-stream'}) | |
end | |
def on_close(env) | |
path = env[Goliath::Request::REQUEST_PATH] | |
return unless path == "/events" | |
channel = env.params[:channel] | |
return if channel.nil? || channel == "" | |
env.logger.info "Connection closed, Unsubscribing." | |
env['redis'].unsubscribe(channel) | |
env['redis'].close | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment