Created
March 20, 2012 21:16
-
-
Save jch/2141384 to your computer and use it in GitHub Desktop.
This file contains 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
REDIS = Redis.new | |
class API < Grape::API | |
# allow swappable transports, defaults to http | |
imbue Grape::Stream, transport: :http | |
# mark an endpoint as streaming | |
get '/firehose', stream: true do | |
# can still set headers | |
headers 'X-API' => 'somekey' | |
REDIS.subscribe 'events' do |on| | |
on.message do |channel, message| | |
flush Event.find_by_id(message) | |
end | |
end | |
# irresponsible client, write a message and close the connection | |
flush! "boom: all done" | |
# hitting the end of a streaming endpoint will | |
# set the correct headers and keep the connection | |
# alive | |
end | |
post '/events' do | |
evt = Event.create!(params[:event]) | |
REDIS.publish 'events', evt.id | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
where Grape::Stream comes from?