Skip to content

Instantly share code, notes, and snippets.

@rye
Created December 16, 2014 16:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rye/09ca51abf3f177a74f98 to your computer and use it in GitHub Desktop.
Save rye/09ca51abf3f177a74f98 to your computer and use it in GitHub Desktop.
Messing around with HTTP Streaming responses.
require "date"
require "json"
require "sinatra"
require "time"
module StreamMessageFormatter
def self.format(object)
return JSON.generate(object) << "\r\n"
end
end
get '/stream' do
headers "Access-Control-Allow-Origin" => "*"
stream do |out|
while true do
out << StreamMessageFormatter.format({time:Time.now.utc.iso8601,message:"heartbeat, baby!"})
sleep 1
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment