{"id":233,"date":"2024-01-11T14:06:39","date_gmt":"2024-01-11T11:06:39","guid":{"rendered":"http:\/\/antyat.ru\/?p=233"},"modified":"2024-01-11T14:13:11","modified_gmt":"2024-01-11T11:13:11","slug":"laravel-how-to-show-number-list-of-users-currently-online","status":"publish","type":"post","link":"http:\/\/antyat.ru\/?p=233","title":{"rendered":"Laravel: How to Show Number\/List of Users Currently Online"},"content":{"rendered":"\n<p>If you want to show the number or list of users currently online on your website, there are a few ways to do it. In this tutorial, we will show you two of them.<\/p>\n<blockquote>\n<p><strong>Notice<\/strong>: To be fair, the most accurate would be to use web sockets and transfer that &#171;live&#187; information with tools like\u00a0<a href=\"https:\/\/laraveldaily.com\/post\/websockets-laravel-soketi-real-time-updates-example\">Soketi<\/a>, but it requires way more configuration. In this tutorial, we went for\u00a0<strong>simple<\/strong>\u00a0options.<\/p>\n<\/blockquote>\n<p>So, our actual goal is to save the last activity of the user. We can achieve it with a built-in Laravel session driver or manually with Middleware.<\/p>\n\n\n\n<h2>Option 1: Change the Session Driver to &#171;Database&#187;<\/h2>\n<p>Create a\u00a0<code>sessions<\/code>\u00a0table and migrate the database.<\/p>\n<div class=\"relative code-block-wrapper\">\n<pre>php artisan session:table<br \/>php artisan migrate<code class=\"torchlight\" data-theme=\"olaolu-palenight\" data-lang=\"shell\"><\/code><\/pre>\n<\/div>\n\n\n\n<p>Update the\u00a0<code>SESSION_DRIVER<\/code>\u00a0value in the environment file to\u00a0<code>database<\/code>.<\/p>\n<p><strong>.env or in config session.php\u00a0<\/strong>instead of evp<\/p>\n<p>SESSION_DRIVER=database<\/p>\n\n\n\n<p>Now you can retrieve the total online visitor count:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">use Illuminate\\Support\\Facades\\DB;\n\nfunction getOnlineUserCount()\n{\nreturn DB::table(config('session.table'))->count();\n}<\/pre>\n\n\n\n<p>Retrieve logged-in users:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">use App\\Models\\User;\r\nuse Illuminate\\Support\\Facades\\DB;\r\n \r\nfunction getLoggedInUsers()\r\n{\r\n    return DB::table(config('session.table'))\r\n        ->distinct()\r\n        ->select(['users.id', 'users.name', 'users.email'])\r\n        ->whereNotNull('user_id')\r\n        ->leftJoin('users', config('session.table') . '.user_id', '=', 'users.id')\r\n        ->get();\r\n}<\/pre>\n\n\n\n<p>Get active users in the last five minutes:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">use Carbon\\Carbon;\r\nuse Illuminate\\Support\\Facades\\DB;\r\n \r\nfunction getActiveUsersInLastMinutes(int $minutes)\r\n{\r\n    return DB::table(config('session.table'))\r\n        ->distinct()\r\n        ->select(['users.id', 'users.name', 'users.email'])\r\n        ->whereNotNull('user_id')\r\n        ->where('sessions.last_activity', '>', Carbon::now()->subMinutes($minutes)->getTimestamp())\r\n        ->leftJoin('users', config('session.table') . '.user_id', '=', 'users.id')\r\n        ->get();\r\n}<\/pre>\n\n\n\n<h2>Option 2: Middleware to Save Last Activity<\/h2>\n<p>Add the\u00a0<code>last_activity<\/code>\u00a0column to the\u00a0<code>users<\/code>\u00a0table.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">php artisan make:migration add_last_activity_column_to_users_table<\/pre>\n\n\n\n<pre class=\"wp-block-preformatted\">Schema::table('users', function (Blueprint $table) {\r\n    $table->integer('last_activity')->index();\r\n});<\/pre>\n\n\n\n<p>Do not forget to add the\u00a0<code>last_activity<\/code>\u00a0value everywhere you create the user.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">User::create([\r\n    \/\/ ...\r\n    'last_activity' => now()->getTimestamp(),\r\n]);<\/pre>\n\n\n\n<p>And add it into the\u00a0<code>fillable<\/code>\u00a0array on the User Model:<\/p>\n<p><strong>app\/Models\/User.php<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">protected $fillable = [\r\n    \/\/ ...\r\n    'last_activity',\r\n];<\/pre>\n\n\n\n<p>Create the\u00a0<code>UpdateLastActivity<\/code>\u00a0<a href=\"https:\/\/laravel.com\/docs\/middleware\">Middleware<\/a>.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">php artisan make:middleware UpdateLastActivity<\/pre>\n\n\n\n<p><strong>app\/Http\/Middleware\/UpdateLastActivity.php<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">namespace App\\Http\\Middleware;\r\n \r\nuse Carbon\\Carbon;\r\nuse Closure;\r\nuse Illuminate\\Http\\Request;\r\nuse Symfony\\Component\\HttpFoundation\\Response;\r\n \r\nclass UpdateLastActivity\r\n{\r\n    \/**\r\n     * Handle an incoming request.\r\n     *\r\n     * @param  \\Closure(\\Illuminate\\Http\\Request): (\\Symfony\\Component\\HttpFoundation\\Response)  $next\r\n     *\/\r\n    public function handle(Request $request, Closure $next): Response\r\n    {\r\n        if ($user = auth()->user()) {\r\n            $user->timestamps    = false;\r\n            $user->last_activity = now()->getTimestamp();\r\n            $user->saveQuietly();\r\n        }\r\n \r\n        return $next($request);\r\n    }\r\n}<\/pre>\n\n\n\n<p>We set\u00a0<code>$user-&gt;timestamps = false<\/code>\u00a0only for this call to keep the\u00a0<code>updated_at<\/code>\u00a0field untouched. Otherwise,\u00a0<code>updated_at<\/code>\u00a0and\u00a0<code>last_activity<\/code>\u00a0will refer to the same time, and technically we are not updating the user.<\/p>\n<p>The user model is saved by calling the\u00a0<code>saveQuietly<\/code>\u00a0method not to fire any observers registered with the user model with every request. This would be an unwanted side effect.<\/p>\n<p>Finally, register\u00a0<code>UpdateLastActivity<\/code>\u00a0Middleware.<\/p>\n<p><strong>app\/Http\/Kernel.php<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">protected $middlewareGroups = [\r\n    'web' => [\r\n        \/\/ ...\r\n        \\App\\Http\\Middleware\\UpdateLastActivity::class, \r\n    ],<\/pre>\n\n\n\n<p>Now you can query Users by their activity using Eloquent, disregarding what session driver you use.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">User::where(\r\n    'last_activity',\r\n    '>',\r\n    now()->subMinutes(5)->getTimestamp()\r\n)\r\n->get();<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>If you want to show the number or list of users currently online on your website, there are a few ways to do it. In this tutorial, we will show you two of them. Notice: To be fair, the most accurate would be to use web sockets and transfer that &#171;live&#187; information with tools like\u00a0Soketi, [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-233","post","type-post","status-publish","format-standard","hentry","category-1"],"_links":{"self":[{"href":"http:\/\/antyat.ru\/index.php?rest_route=\/wp\/v2\/posts\/233","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/antyat.ru\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/antyat.ru\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/antyat.ru\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/antyat.ru\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=233"}],"version-history":[{"count":3,"href":"http:\/\/antyat.ru\/index.php?rest_route=\/wp\/v2\/posts\/233\/revisions"}],"predecessor-version":[{"id":236,"href":"http:\/\/antyat.ru\/index.php?rest_route=\/wp\/v2\/posts\/233\/revisions\/236"}],"wp:attachment":[{"href":"http:\/\/antyat.ru\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=233"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/antyat.ru\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=233"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/antyat.ru\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=233"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}