Skip to content

Instantly share code, notes, and snippets.

@jch
Created March 20, 2012 21:16
Show Gist options
  • Save jch/2141384 to your computer and use it in GitHub Desktop.
Save jch/2141384 to your computer and use it in GitHub Desktop.
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
@shurikk
Copy link

shurikk commented Apr 25, 2014

where Grape::Stream comes from?

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