How to be consistent with variables using Action Cable?

30 views
Skip to first unread message

nate.t...@gmail.com

unread,
May 10, 2016, 10:03:42 AM5/10/16
to Ruby on Rails: Core
Let's assume we use something like current_user which is an instance of a ServiceClass which holds user model, session params and other info. 
The thing is that variable is being set during connection with websocket and being reused for all AC calls on _different_ subscriptions.

Then, at some point user decides to update his username, we make a call to current_user.update(new_username) and it works okay.

But other AC subscriptions under that user still use old user model. I suppose as each subscription works under their own thread, thus updating user model under one thread will not update them under other threads. What is the best approach for such case?



class ServiceClass

def initialize(session,...)
  @session = session
  @user = current_user
end

 def update!(username)
  @user.username = username
  @user.save!
 end

 ...
end

module ApplicationCable
  class Channel < ActionCable::Channel::Base
    def global_service
      @global_service ||= GlobalService.new(current_user)
    end
  end
end



Message has been deleted

nate.t...@gmail.com

unread,
May 10, 2016, 10:14:49 AM5/10/16
to Ruby on Rails: Core
Sorry this code snippet is correct:


module
ApplicationCable
class Channel < ActionCable::Channel::Base
  def current_user
    @current_user ||= ServiceClass.new(session, user)
  end
end
end


Reply all
Reply to author
Forward
0 new messages