Skip to content

Commit

Permalink
initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
carlhoerberg committed Mar 8, 2012
0 parents commit 4cbec82
Show file tree
Hide file tree
Showing 5 changed files with 109 additions and 0 deletions.
7 changes: 7 additions & 0 deletions Gemfile
@@ -0,0 +1,7 @@
source :rubygems

gem 'thin'
gem 'sinatra'
gem 'sinatra-contrib'
gem 'haml'
gem 'amqp'
46 changes: 46 additions & 0 deletions Gemfile.lock
@@ -0,0 +1,46 @@
GEM
remote: http://rubygems.org/
specs:
amq-client (0.9.2)
amq-protocol (>= 0.9.0)
eventmachine
amq-protocol (0.9.0)
amqp (0.9.4)
amq-client (~> 0.9.2)
amq-protocol (>= 0.9.0)
eventmachine
backports (2.3.0)
daemons (1.1.8)
eventmachine (0.12.10)
haml (3.1.4)
rack (1.4.1)
rack-protection (1.2.0)
rack
rack-test (0.6.1)
rack (>= 1.0)
sinatra (1.3.2)
rack (~> 1.3, >= 1.3.6)
rack-protection (~> 1.2)
tilt (~> 1.3, >= 1.3.3)
sinatra-contrib (1.3.1)
backports (>= 2.0)
eventmachine
rack-protection
rack-test
sinatra (~> 1.3.0)
tilt (~> 1.3)
thin (1.3.1)
daemons (>= 1.0.9)
eventmachine (>= 0.12.6)
rack (>= 1.0.0)
tilt (1.3.3)

PLATFORMS
ruby

DEPENDENCIES
amqp
haml
sinatra
sinatra-contrib
thin
1 change: 1 addition & 0 deletions Procfile
@@ -0,0 +1 @@
web: bundle exec ruby app.rb -p $PORT -e production
39 changes: 39 additions & 0 deletions app.rb
@@ -0,0 +1,39 @@
require 'sinatra'
require 'sinatra/streaming'
require 'haml'
require 'amqp'

configure do
EM.next_tick do
AMQP.connect ENV['CLOUDAMQP_URL'] || 'amqp://guest:guest@localhost'
end
end

get '/' do
haml :index
end

post '/publish' do
AMQP::Channel.new do |channel|
channel.fanout("f1").publish "Hello, world!"
end
204
end

get '/stream', provides: 'text/event-stream' do
stream :keep_open do |out|
AMQP::Channel.new do |channel|
channel.queue do |queue|
queue.bind(channel.fanout("f1")).subscribe do |payload|
out << "data: #{payload}\n\n"
end
end
timer = EM.add_periodic_timer(20) { out << ":\n" }
out.callback do
p 'closing'
timer.cancel
channel.close
end
end
end
end
16 changes: 16 additions & 0 deletions views/index.haml
@@ -0,0 +1,16 @@
!!!
%html
%head
%script{src: 'http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js', type: 'text/javascript'}
%body
%input#publish{type: 'button', value: 'Publish'}
%ul#messages
:javascript
var es = new EventSource('/stream');
es.onmessage = function(e) {
$('#messages').append("<li>" + e.data + "</li>")
}
$("#publish").click(function () {
$.post("/publish")
})

0 comments on commit 4cbec82

Please sign in to comment.