0

I'm trying to study FOSUserBundle in Symfony. I searched on google "how to check if other users are logged into their account", but I failed to do it. I want to add a green coloured mark if a specific user is logged in, the way Facebook does it.

I don't know if FOSUserBundle has a code that will check whether a user is logged in. I found this (other question on SOF) but it only checks if the current user is logged in.

I don't know it's a good idea to add this property in my entity

   /**
     *
     * @ORM\Column(name="log_status", type="boolean", nullable=true)
     */
    protected $logStatus;

This will set the status of a user (logged in or not) and I'll add a column to the fos_user table to save it to the database.

Fabrice Kabongo
  • 671
  • 10
  • 23
user3818576
  • 2,979
  • 8
  • 37
  • 62

2 Answers2

3

To tackle this issue, you would first need to define what logged in really means in your case. Because your code is normally only called once per request, the user can be gone 5 seconds after that, or he can have the page opened for an hour, but in both cases the user might still be logged in in the session.

So what you probably want to do is checking if the user is currently active. There are multiple ways to do this, and depending on how real-time the information has to be, you have to choose the best option for you.

Easiest (imho) would be to ping every X seconds to the server with an AJAX call (Polling or Long-Polling), and than make a ruling on how long before the status will change to offline. There should be some rome for the AJAX call to fail.

Other options, which will give you a more real-time overview would be to use either WebSockets, or Server Sent Events.

There might be other options, like using a service like PubNub to handle the connections, and use their API to determine if a user gets inactive. But that you would need to figure out for yourself.

Nico Kaag
  • 1,876
  • 12
  • 12
  • This is good idea for real time. but is that ok for adding a another field in fos_user table that is logStatus? – user3818576 Jul 31 '17 at 09:58
  • My idea is when the user login, I update the field logStatus to active. that will check me if the user login then I will use socket.io or anything that will do an ajax – user3818576 Jul 31 '17 at 09:59
1

adding that field it's good know you can just go a query to find user that status its true
but the probleme its how to keep update this data you can't do that by symfony
for me i use a ping evry X min like Heartbeat to see if user its Active you can use Socket.io

Barkati.med
  • 620
  • 5
  • 11
  • 1
    Thanks for this idea of socket.io. My idea now is to update the field logStatus to active when user login. then using socket.io that will check me the user – user3818576 Jul 31 '17 at 10:01