Skip to content

Instantly share code, notes, and snippets.

@andrewhavens
Created August 5, 2013 17:25
Show Gist options
  • Save andrewhavens/6157725 to your computer and use it in GitHub Desktop.
Save andrewhavens/6157725 to your computer and use it in GitHub Desktop.
Working example of SSE using Sinatra and Redis, but with bad concurrency...please help to improve
require 'redis'
require 'sinatra/base'
class SSE < Sinatra::Base
def send_message(json)
"id: #{Time.now}\n" +
"data: #{json}" +
"\r\n\n"
end
get '/channels/:id/subscribe', provides: 'text/event-stream' do
channel_id = params['id']
stream(:keep_open) do |connection|
Redis.new.subscribe("channels:#{channel_id}") do |on|
on.message do |channel, json|
connection << send_message(json)
end
end
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment