Luiz Rossi
Luiz Rossi

Reputation: 892

Check if a Moodle user is online

I need to know if a user is on-line at Moodle, there is a Moodle block for it, but actually it just check if the user had an activity on Moodle in the last X minutes, but in my case that is not useful. There is anyway to know it?

Upvotes: 1

Views: 1556

Answers (2)

Russell England
Russell England

Reputation: 10241

As the other answerers say, it isn't possible to see if a user is currently online. As you mentioned, the online users block checks the value of mdl_user.lastaccess. It's probably the best option but even then it depends on the user accessing or refreshing a page. They could be reading a page for 10 minutes for example, so technically still online but it won't show that they are online. Or vice versa, they could have looked at a page and immediately closed it, so it will show they are online but technically not.

Also in my experience, a lot of user's don't log out of Moodle, they just close the page. So its not a reliable way to see if they are currently logged in or not.

For reference, mdl_user.lastaccess is updated by the following sequence:

Going to index.php or any page that uses require_login() which is pretty much most of the pages in Moodle.

This will call user_accesstime_log()

If the mdl_user.lastaccess time hasn't been updated within the last 60 seconds (set by LASTACCESS_UPDATE_SECS) then the mdl_user.lastaccess is updated with the current time()

Upvotes: 1

JasonJensenDev
JasonJensenDev

Reputation: 2407

HTTP doesn't allow you to see who else is online in realtime. You send a request, and get a response back from the server. That's it. Of course, you could send a request and get a response back every second to try to simulate a realtime app, but that gets a little messy.

The best way to get real-time connections is through web sockets, which can be accomplished with a few libraries in node. Just google 'node websocket tutorial' and you'll find what you're looking for.

If you want to outsource the websocket stuff, I highly recommend pusher.com, which only requires that you include some javascript on your webpage to interact with their websocket service.

Of course, if you're trying to get this for Moodle, you may be SOL. But I thought I'd try to explain what you would need if you were to do something in real time. Best of luck to you!

Upvotes: 0

Related Questions