Skip to content

Instantly share code, notes, and snippets.

@shokai
Created October 31, 2012 21:02
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 shokai/3989831 to your computer and use it in GitHub Desktop.
Save shokai/3989831 to your computer and use it in GitHub Desktop.
Sinatra Comet

Sinatra Comet

Install Dependencies

% gem install sinatra thin sinatra-contrib

Run

% ruby sinatra-comet.rb

=> http://localhost:4567

Comet

GET

% curl http://localhost:4567/comet/test

=> wait response

then, POST

% curl -d 'data=hello' http://localhost:4567/comet/test
#!/usr/bin/env ruby
require 'rubygems'
require 'sinatra'
require 'thin'
require 'sinatra/streaming'
@@comet = Hash.new
post '/comet/:channel' do
channel = params['channel']
data = params[:data]
@@comet[channel] = data
data
end
get '/comet/:channel' do
channel = params['channel']
stream do |s|
data = nil
loop do
break if (data = @@comet.delete channel)
sleep 1
end
s.write data
s.flush
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment