Check if user is in real time

I want to check if the User is currently located. This should be displayed in the user profile.

I got it in my application_controller

after_filter :user_activity

private

  def user_activity
    current_user.try :touch
  end

I defined it in user-model

def online?
 updated_at > 10.minutes.ago
end

I can check the view if the user is online: (HAML)

- if @user.online?

This works, but there is always a page reload required to update this information. How can this be done with realtimecheck? Perhaps using ActionCable?

PS: using devise

+4
source share
3 answers

Yes, you can use Action Cable for this. The Rails manual has (half) an example of this here: Action Cable Overview - 6.1 Example 1: User Appearance

, AppearanceChannel, . appear current_user, (, ), , , BroadcastAppearanceJob. ( ).

+4

Action Cable, (.disappear , , , ..)

, 5 , javascript:

setInterval(function(){
  var xhttp = new XMLHttpRequest();
  xhttp.open("GET", "/i_am_still_here", true);
  xhttp.send();
}, 5*60*1000);

..

def i_am_still_here
  current_user.update(last_seen_at:DateTime.now)
end

User.rb

def is_online?
  last_seen_at > 5.minutes.ago
end
+2

. , .

, , ( ).

, websockets , JavaScript, Obj-C/Swift Java.

+1

Source: https://habr.com/ru/post/1654387/


All Articles