Created
August 5, 2013 17:25
-
-
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
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 '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