Reputation: 475
I think Laravel solves this problem as well, but would like to extend my question a little bit.
Is it possible in Laravel to show how many users are currently logged in, in real time?
Like if the 10 users are logged then the counter should show 10 users are logged in. This will be shown to all the ten users , but in real time.
So if some users which logs out, counter should decrease.
The approach i used is like after attempting to login, we can increment the counter of the one of the column in DB. We can use another table, say logged in table.
When users are logged out, we would decrement the counter by deleting that Auth::id of the user and the count will get reduced.
But then i want to try it in a real time, how is that possible with laravel itself??
Upvotes: 0
Views: 6271
Reputation: 691
I solved this by writing to a DB table with a user_id every time a page was loaded.
I then created a view that showed a list of everyone who had been active in the past 8 minutes. My logic was that no updates to the activity table in 8 minutes meant they had left the site.
It worked pretty well. I don't know how efficient it would be on a huge site with lots of traffic. The project I was working on only had a few hundred people per day logging into it.
Upvotes: 0
Reputation: 499
It is not that simple as you think. You can of course increment some kind of number stored in DB every time user is logged in, and decrease the number every time user hit the logout url, but what with all those users who don't use logout url and are just closing the window? It will generate false data in short period of time. Laravel default keeps sessions in files and they can stay on servers hard drive for weeks/months/years even if the user isn't active.
BUT
it's possible to store sessions in database [ http://laravel.com/docs/4.2/session#database-sessions ]
As you can see in Laravel docs, Laravel uses last_activity
field to store users activity data, by accessing this field you can simply check how many users were active in selected period of time.
By knowing that you can easily display this in your view. I'm not going to write the code for you, I think it's not so hard to prepare query to get the data. I hope I helped you.
Upvotes: 0
Reputation: 4089
You can use the Event Broadcasting features of Laravel 5 to do it in real-time.
Every time an user log in, you will need to broadcast an event. You can follow this tutorial: http://www.sitepoint.com/real-time-apps-laravel-5-1-event-broadcasting/
Then on the client side, you will need to use JavaScript to listen for that event.
Upvotes: 1