Skip to content

Instantly share code, notes, and snippets.

@typewriter
Created November 23, 2017 13:12
Show Gist options
  • Save typewriter/b4415867adcbf589407cefd0f3550de7 to your computer and use it in GitHub Desktop.
Save typewriter/b4415867adcbf589407cefd0f3550de7 to your computer and use it in GitHub Desktop.
Server Sent Events (SSE) Server with Ruby
# 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