Created
September 25, 2011 22:47
-
-
Save rweald/1241279 to your computer and use it in GitHub Desktop.
A simple Goliath server that sends data to client using SSE after receiving a message on a redis pubsub channel
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
require 'goliath' | |
require 'em-hiredis' | |
class EventGenerator < Goliath::API | |
def response(env) | |
redis = EM::Hiredis.connect | |
redis.subscribe(:one) | |
redis.on(:message) { |channel, message| | |
puts [:message, channel, message] | |
# env.stream_send(["event:signup", "data:signup event ##{rand(100)}\n\n"].join("\n")) | |
env.stream_send("data:received message on <b>#{channel}</b> with content <i>#{message}</i>\n\n") | |
} | |
streaming_response(200, {'Content-Type' => 'text/event-stream'}) | |
end | |
end | |
class StatsGenerator < Goliath::API | |
def response(env) | |
[200, {}, "Hello World"] | |
end | |
end | |
class SSE < Goliath::API | |
use Rack::Static, :urls => ["/index.html"], :root => Goliath::Application.app_path("public") | |
get "/events" do | |
run EventGenerator.new | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment