Permalink
Browse files

use pusher presence channels to keep track of users in the room

  • Loading branch information...
1 parent 7cde2b5 commit 1c1dfc3c92dd3b1c870a6c7e08510c019edd4593 Keith Norman committed Apr 17, 2011
@@ -17,7 +17,7 @@ def load_room
end
def channel_name
- "groupon_go_#{ @room.to_param }_#{ Rails.env }"
+ "presence-groupon_go_#{ @room.id }_#{ Rails.env }"
end
helper_method :channel_name
@@ -0,0 +1,20 @@
+class PusherController < ApplicationController
+ before_filter :load_user
+ protect_from_forgery :except => [:auth]
+
+ def auth
+ channel = Pusher[params[:channel_name]]
+
+ if @user.present?
+ response = channel.authenticate(params[:socket_id], {
+ :user_id => @user.id,
+ :user_info => @user.attributes
+ })
+ render :json => response
+ else
+ render :text => "Not authorized", :status => '403'
+ end
+ end
+
+end
+
@@ -27,6 +27,9 @@ a {
margin: 20px 0;
outline: none;
position: relative;
+ form {
+ @include pie-clearfix;
+ }
input {
font-size: 14px;
outline: none;
@@ -176,4 +179,4 @@ a {
}
}
}
-}
+}
@@ -5,6 +5,6 @@
%meta{ :name => "viewport", :content => "width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no" }
= stylesheet_link_tag 'mobile', :media => 'all and (max-width: 480px)'
= stylesheet_link_tag 'screen', :media => 'all and (min-width: 481px)'
- = javascript_include_tag 'jquery', 'pusher', 'mustache', '/mustache_templates.js', 'application'
+ = javascript_include_tag 'jquery', 'http://js.pusherapp.com/1.8/pusher.min.js', 'mustache', '/mustache_templates.js', 'application'
%body
= yield
@@ -19,6 +19,11 @@
You can't submit an empty message!
%span.loading
+ %p
+ %span#count
+ users in the room
+
+
%ul#chat_data
- @posts.each do |post|
@@ -1,5 +1,6 @@
#container
= content_tag :span,'', :id => 'channel', 'data-value' => channel_name, :class => 'js_attribute'
+ = content_tag :span,'', :id => 'room', 'data-value' => @room.id, :class => 'js_attribute'
#intro
%h1
@@ -19,6 +20,10 @@
You can't submit an empty message!
%span.loading
+ %p
+ %span#count
+ users in the room
+
%ul#chat_data
- @room.posts.order('created_at DESC').each do |post|
View
@@ -40,5 +40,11 @@ class Application < Rails::Application
# Configure sensitive parameters which will be filtered from the log file.
config.filter_parameters += [:password]
+
+ Pusher.app_id = '3638'
+ Pusher.key = '534d197146cf867179ee'
+ Pusher.secret = '4a0cf79a75eaff29cfc7'
+
end
end
+
@@ -1,4 +0,0 @@
-Pusher.app_id = '3638'
-Pusher.key = '534d197146cf867179ee'
-Pusher.secret = '4a0cf79a75eaff29cfc7'
-
View
@@ -11,6 +11,7 @@
match '/logout' => 'sessions#destroy', :as => 'logout'
match 'oauth_callback' => 'sessions#oauth_callback', :as => 'oauth_callback'
match '/mustache_templates.js' => MustacheCacher
+ match '/pusher/auth', :to => 'pusher#auth'
match '/:name', :to => 'rooms#show'
root :to => "rooms#show"
@@ -25,10 +25,11 @@ $(function() {
}
});
+ var room = $('#room').data('value');
var pusher = new Pusher('534d197146cf867179ee');
var channel = pusher.subscribe($('#channel').data('value'));
- pusher.bind('new_post',
+ channel.bind('new_post',
function(data) {
var tmpl = MustacheTemplates['posts/_post'];
var post = {
@@ -42,6 +43,25 @@ $(function() {
$('#chat_data').prepend(Mustache.to_html(tmpl, post));
}
);
+
+ channel.bind('pusher:connection_disconnected', function(){
+ if(channel.members.count === 0)
+ $.post('/rooms/' + room, {'_method': 'delete'});
+ });
+
+ channel.bind('pusher:member_removed', function(){
+ $('#count').html(channel.members.count);
+ if(channel.members.count === 0)
+ $.post('/rooms/' + room, {'_method': 'delete'});
+ });
+
+ channel.bind('pusher:member_added', function(){
+ $('#count').html(channel.members.count);
+ });
+
+ channel.bind('pusher:subscription_succeeded', function() {
+ $('#count').html(channel.members.count);
+ });
$('#post_message').focus();

0 comments on commit 1c1dfc3

Please sign in to comment.