Created
November 23, 2017 13:12
-
-
Save typewriter/b4415867adcbf589407cefd0f3550de7 to your computer and use it in GitHub Desktop.
Server Sent Events (SSE) Server with Ruby
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
# Server Sent Events (SSE) Server sample | |
# refs. https://gist.github.com/rkh/1476463 | |
require 'sinatra' | |
require 'thin' | |
set :bind, '0.0.0.0' | |
set :port, 8080 | |
connections = [] | |
get '/subscribe', provides: 'text/event-stream' do | |
stream(:keep_open) do |out| | |
connections << out | |
out.callback { connections.delete(out) } | |
end | |
end | |
get '/publish' do | |
connections.each { |out| out << "data: #{params[:m]}\n\n" } | |
204 # HTTP 204 No Content | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment