"Online Users" is a Laravel package designed to effortlessly track and display the real-time count of users currently active on your web application. With seamless integration, this package provides a quick and reliable solution for monitoring and presenting the dynamic online user presence, enhancing the overall user experience on your Laravel-powered website.
You can install the package via composer:
composer require klevze/online-users
You can publish and run the migrations with:
php artisan vendor:publish --tag="online-users-migrations"
php artisan migrate
To enable the "Online Users" middleware in your Laravel application, follow these steps:
-
Open the
app/Http/Kernel.php
file in your Laravel project. -
Locate the
$middlewareGroups
property, specifically within theweb
middleware group. -
Add the following line to the
web
middleware group:protected $middlewareGroups = [ 'web' => [ // ... other middleware entries \Klevze\OnlineUsers\Middleware\TrackUserActivity::class, // ... other middleware entries ], // ... other middleware groups ];
The "CleanupInactiveUsers" console command provided by the "Online Users" package allows you to remove inactive users from the user_activities
table. Follow the steps below to integrate and schedule the cleanup task.
-
Open the
app/Console/Kernel.php
file in your Laravel project. -
Locate the
schedule
method and add the following entry to schedule thecleanup:inactive-users
command every five minutes:protected function schedule(Schedule $schedule) { // ... other scheduled tasks $schedule->command('cleanup:inactive-users')->everyFiveMinutes(); // ... other scheduled tasks }
-
Save the changes to the
Kernel.php
file.
Now, the "CleanupInactiveUsers" console command will run every five minutes, cleaning up inactive users from the user_activities
table.
Once package is installed, you can use the OnlineUsers
class to get the number of active users. For example, the following code will get the number of active users:
$activeUsers = OnlineUsers::getActiveUsers();
Or you can use it directly in blade view:
<p>Currently browsing: {{ OnlineUsers::getActiveUsers() ?? 0 }}</p>
You can see a working demo at these sites:
- The Wallpapers: https://thewallpapers.net
- Joke Station: https://jokestation.org
The MIT License (MIT). Please see License File for more information.