Skip to content

Instantly share code, notes, and snippets.

@carlhoerberg
Created December 19, 2011 14:41
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save carlhoerberg/1497500 to your computer and use it in GitHub Desktop.
Save carlhoerberg/1497500 to your computer and use it in GitHub Desktop.
require 'rack'
require 'json'
require 'torquebox-messaging'
class EventStream
def self.call(env)
path = Rack::Utils.unescape env["PATH_INFO"]
[200, {
'Content-Type' => 'text/event-stream',
'X-Accel-Buffering' => 'no',
},
TopicStreamer.new(path)]
end
class TopicStreamer
def initialize(route)
name = "/topics#{route}"
TorqueBox::Messaging::Topic.start(name)
@topic = TorqueBox::Messaging::Topic.new(name)
end
def each(&blk)
blk.call("retry: 1000\n")
while true
msg = @topic.receive(:timeout => 20_000)
blk.call(msg ? "data: #{msg.to_json}\n\n" : ":\n")
end
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment