Permalink
Browse files

Add pusher, remove websockets.

This commit replaces websockets as the means of updating
whats playing right now in the web client.

GEMFILE changes
'eventmachine' removed
'pusher' added

bin/realtime replaces realtime.rb which previously updated
now_playing via websockets. It has been setup to run in a similar
mannar as bin/clean.

Play.yml has new config options for pusherapp.com api credentials
for pusher.

pusher_app_id: __PUSHER_APP_ID__
pusher_key: __PUSHER_KEY__
pusher_secret: __PUSHER_SECRET__
  • Loading branch information...
1 parent 77daf10 commit 579bb0fdb7313690a19caa43c1ed60a981b20ced @joeyw joeyw committed Feb 12, 2012
Showing with 80 additions and 66 deletions.
  1. +1 −1 Gemfile
  2. +8 −1 Gemfile.lock
  3. +1 −0 Rakefile
  4. +4 −0 app/app.rb
  5. +1 −0 app/boot.rb
  6. +6 −11 app/frontend/scripts/realtime.js.coffee.erb
  7. +8 −5 app/play.rb
  8. +0 −45 app/realtime.rb
  9. +1 −0 app/templates/layout.mustache
  10. +47 −0 bin/realtime
  11. +0 −3 config.ru
  12. +3 −0 config/play.example.yml
View
@@ -10,8 +10,8 @@ gem 'sinatra_auth_github', '~>0.3.0'
gem 'sprockets'
gem 'sass', '~>3.1'
gem 'coffee-script'
-gem 'eventmachine', '~>1.0.0.beta.4'
gem 'websocket-rack'
+gem 'pusher'
if RUBY_PLATFORM.downcase.include?("darwin")
gem 'rb-appscript'
View
@@ -30,6 +30,10 @@ GEM
oauth2 (0.5.2)
faraday (~> 0.7)
multi_json (~> 1.0)
+ pusher (0.9.2)
+ multi_json (~> 1.0)
+ ruby-hmac (~> 0.4.0)
+ signature (~> 0.1.2)
rack (1.3.6)
rack-protection (1.2.0)
rack
@@ -40,9 +44,12 @@ GEM
redis (2.2.2)
rest-client (1.6.7)
mime-types (>= 1.16)
+ ruby-hmac (0.4.0)
sass (3.1.12)
shotgun (0.9)
rack (>= 1.0)
+ signature (0.1.2)
+ ruby-hmac
sinatra (1.3.2)
rack (~> 1.3, >= 1.3.6)
rack-protection (~> 1.2)
@@ -80,9 +87,9 @@ PLATFORMS
DEPENDENCIES
SystemTimer (~> 1.2.3)
coffee-script
- eventmachine (~> 1.0.0.beta.4)
mocha
mustache (~> 0.99.4)
+ pusher
rack (~> 1.3.0)
rack-test
rake (~> 0.9.2.1)
View
@@ -34,6 +34,7 @@ end
desc "Start the server"
task :start do
sh "bin/clean &"
+ sh "bin/realtime &"
sh "thin start -p5050"
end
View
@@ -26,6 +26,10 @@ class App < Sinatra::Base
:client_id => Play.config.client_id,
}
+ Pusher.app_id = Play.config.pusher_app_id
+ Pusher.key = Play.config.pusher_key
+ Pusher.secret = Play.config.pusher_secret
+
set :public_folder, "#{dir}/frontend/public"
set :static, true
set :mustache, {
View
@@ -12,6 +12,7 @@
require 'yajl'
require 'sass'
require 'appscript' if RUBY_PLATFORM.downcase.include?("darwin") && !ENV['CI']
+require 'pusher'
require 'play'
@@ -1,15 +1,10 @@
play = exports ? this
-socket = new WebSocket('wss://<%= Play.config.hostname %>/realtime')
+pusher = new Pusher('<%= Play.config.pusher_key %>')
+channel = pusher.subscribe('now_playing_updates')
-socket.onopen = () ->
-
-socket.onmessage = (message) ->
- song = JSON.parse(message.data).now_playing
-
- if song
- play.renderNowPlaying(song)
-
- list = listFromJson(JSON.parse(message.data))
+channel.bind 'update_now_playing', (message) ->
+ play.renderNowPlaying(message.now_playing)
+ list = listFromJson(message)
songs = Mustache.to_html(templates.list,list,templates)
- $('#songs').html(songs)
+ $('#songs').html(songs)
View
@@ -6,11 +6,14 @@ module Play
# Returns an OpenStruct so you can chain methods off of `Play.config`.
def self.config
OpenStruct.new \
- :secret => yaml['gh_secret'],
- :client_id => yaml['gh_key'],
- :stream_url => yaml['stream_url'],
- :office_url => yaml['office_url'],
- :hostname => yaml['hostname']
+ :secret => yaml['gh_secret'],
+ :client_id => yaml['gh_key'],
+ :stream_url => yaml['stream_url'],
+ :office_url => yaml['office_url'],
+ :hostname => yaml['hostname'],
+ :pusher_app_id => yaml['pusher_app_id'],
+ :pusher_key => yaml['pusher_key'],
+ :pusher_secret => yaml['pusher_secret']
end
private
View
@@ -1,45 +0,0 @@
-require 'rack/websocket'
-class Realtime < Rack::WebSocket::Application
- def initialize(options = {})
- super
- end
-
- def on_close(env)
- @timer.cancel
- end
-
- def on_message(env, msg)
- end
-
- def on_error(env, error)
- end
-
- def on_open(env)
- last = nil
-
- @timer = EM::PeriodicTimer.new(1) do
- if last != last=current
- send_data last
- end
- sleep 0.5
- end
- end
-
-private
-
- # The current JSON representation of Play that we send back.
- #
- # Returns a String, JSON-encoded.
- def current
- now_playing = Play::Player.now_playing
- now_playing = now_playing.to_hash if now_playing
-
- songs = Play::Queue.songs
- songs.shift
-
- Yajl.dump({
- :now_playing => now_playing,
- :songs => songs.map {|song| song.to_hash }
- })
- end
-end
@@ -13,6 +13,7 @@
<link href='//fonts.googleapis.com/css?family=Open+Sans:300italic,400italic,600italic,700italic,400,300,700,600' rel='stylesheet' type='text/css'>
<link href='//fonts.googleapis.com/css?family=Open+Sans+Condensed:300' rel='stylesheet' type='text/css'>
+ <script type="text/javascript" src="http://js.pusher.com/1.11/pusher.min.js"></script>
<script type="text/javascript" src="/js/application.js"></script>
<title>Play</title>
View
@@ -0,0 +1,47 @@
+#!/usr/bin/env ruby
+#
+# realtime
+#
+# Sends now_playing to pusher to update web clients
+
+require 'app/boot'
+
+class Realtime
+ def initialize
+ @last = nil
+ end
+
+ def update
+ if @last != @last=current
+ Pusher['now_playing_updates'].trigger('update_now_playing', @last)
+ end
+ end
+
+private
+
+ # The current JSON representation of Play that we send back.
+ #
+ # Returns a String, JSON-encoded.
+ def current
+ now_playing = Play::Player.now_playing
+ now_playing = now_playing.to_hash if now_playing
+
+ songs = Play::Queue.songs
+ songs.shift
+
+ Yajl.dump({
+ :now_playing => now_playing,
+ :songs => songs.map {|song| song.to_hash }
+ })
+ end
+end
+
+sleep 2
+
+realtime = Realtime.new
+
+while true
+ realtime.update
+ sleep 0.5
+ break if `ps aux | grep thin | grep -v grep`.lines.count < 1
+end
View
@@ -7,9 +7,6 @@ stylesheets.append_path 'app/frontend/styles'
javascripts = Sprockets::Environment.new
javascripts.append_path 'app/frontend/scripts'
-require 'realtime'
-map('/realtime') { run Realtime.new }
-
map("/css") { run stylesheets }
map("/js") { run javascripts }
@@ -1,4 +1,7 @@
gh_key: __OAUTH_KEY__
gh_secret: __OAUTH_SECRET__
+pusher_app_id: __PUSHER_APP_ID__
+pusher_key: __PUSHER_KEY__
+pusher_secret: __PUSHER_SECRET__
office_url: http://example.com/office.macs
stream_url: http://example.com:8000/listen

0 comments on commit 579bb0f

Please sign in to comment.