From b33818d58cdadcfccc0d5ceb9667487d012c602f Mon Sep 17 00:00:00 2001 From: Alexandre Daoud Date: Mon, 14 Sep 2015 04:39:44 +0100 Subject: [PATCH 1/2] Add support for lifecycle callbacks. Currently the only events are 'user_connect' and 'user_disconnect' which are triggered whenever the user logs in or logs out. These events also trigger when the user navigates to the app with a valid session cookie or closes the window. --- app/volt/tasks/query_tasks.rb | 2 +- app/volt/tasks/user_tasks.rb | 6 +++++ lib/volt/server/socket_connection_handler.rb | 25 ++++++++++++++++++++ lib/volt/volt/server_setup/app.rb | 3 +++ lib/volt/volt/users.rb | 4 ++++ 5 files changed, 39 insertions(+), 1 deletion(-) diff --git a/app/volt/tasks/query_tasks.rb b/app/volt/tasks/query_tasks.rb index 2068c053..6a9b0d1b 100644 --- a/app/volt/tasks/query_tasks.rb +++ b/app/volt/tasks/query_tasks.rb @@ -7,7 +7,7 @@ def add_listener(collection, query) # For requests from the client (with @channel), we track the channel # so we can send the results back. Server side requests don't stay live, # they simply return to :dirty once the query is issued. - @channel.user_id = Volt.current_user_id + @channel.update_user_id(Volt.current_user_id) # live_query.add_channel(@channel) end diff --git a/app/volt/tasks/user_tasks.rb b/app/volt/tasks/user_tasks.rb index 0f9e5a61..6f16515f 100644 --- a/app/volt/tasks/user_tasks.rb +++ b/app/volt/tasks/user_tasks.rb @@ -21,4 +21,10 @@ def login(login_info) end end end + + def logout(logout_info) + # Remove user_id from user's channel + @channel.update_user_id(nil) if @channel + end + end diff --git a/lib/volt/server/socket_connection_handler.rb b/lib/volt/server/socket_connection_handler.rb index c1efaffd..1d594145 100644 --- a/lib/volt/server/socket_connection_handler.rb +++ b/lib/volt/server/socket_connection_handler.rb @@ -15,6 +15,26 @@ def initialize(session, *args) @@channels ||= [] @@channels << self + + end + + def update_user_id(user_id) + if !@user_id && user_id + # If there is currently no user id associated with this channel + # and we get a new valid user_id, set it then trigger a + # user_connect event + @user_id = user_id + @@dispatcher.volt_app.trigger!("user_connect", @user_id) + elsif @user_id && !user_id + # If there is currently a user id associated with this channel + # and we get a nil user id, trigger a user_disconnect event then + # set the id to nil + @@dispatcher.volt_app.trigger!("user_disconnect", @user_id) + @user_id = user_id + else + # Otherwise, lets just set the id (should never really run) + @user_id = user_id + end end def self.dispatcher=(val) @@ -83,6 +103,11 @@ def closed # Remove ourself from the available channels @@channels.delete(self) + # Trigger a user disconnect event even if the user hasn't logged out + if @user_id + @@dispatcher.volt_app.trigger!("user_disconnect", @user_id) + end + begin @@dispatcher.close_channel(self) rescue DRb::DRbConnError => e diff --git a/lib/volt/volt/server_setup/app.rb b/lib/volt/volt/server_setup/app.rb index 5e4b8a34..5be0ce79 100644 --- a/lib/volt/volt/server_setup/app.rb +++ b/lib/volt/volt/server_setup/app.rb @@ -10,6 +10,9 @@ module Volt module ServerSetup module App + # Include Eventable to allow for lifecycle callbacks + include Eventable + # The root url is where the volt app is mounted attr_reader :root_url # The app url is where the app folder (and sprockets) is mounted diff --git a/lib/volt/volt/users.rb b/lib/volt/volt/users.rb index 5b1ef2e5..6e16fe1e 100644 --- a/lib/volt/volt/users.rb +++ b/lib/volt/volt/users.rb @@ -114,6 +114,10 @@ def login(username, password) end def logout + # Notify the backend so we can remove the user_id from the user's channel + UserTasks.logout(user_id: Volt.current_app.cookies._user_id) + + # Remove the cookie so user is no longer logged in Volt.current_app.cookies.delete(:user_id) end From 861762e9aeb754eaabccc58a9b5cfea7f1670a49 Mon Sep 17 00:00:00 2001 From: Alexandre Daoud Date: Fri, 18 Sep 2015 20:53:13 +0100 Subject: [PATCH 2/2] user_disconnect event trigger should be in begin --- lib/volt/server/socket_connection_handler.rb | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/lib/volt/server/socket_connection_handler.rb b/lib/volt/server/socket_connection_handler.rb index 1d594145..5daecb02 100644 --- a/lib/volt/server/socket_connection_handler.rb +++ b/lib/volt/server/socket_connection_handler.rb @@ -103,13 +103,14 @@ def closed # Remove ourself from the available channels @@channels.delete(self) - # Trigger a user disconnect event even if the user hasn't logged out - if @user_id - @@dispatcher.volt_app.trigger!("user_disconnect", @user_id) - end - begin @@dispatcher.close_channel(self) + + # Trigger a user disconnect event even if the user hasn't logged out + if @user_id + @@dispatcher.volt_app.trigger!("user_disconnect", @user_id) + end + rescue DRb::DRbConnError => e # ignore drb read of @@dispatcher error if child has closed end