Today, my task is to add a green dot next to the username on the Laravel app user profile page to indicate whether they are online. The first thing that comes to mind is that we will need to start a server and track active socket connections for each user. Then use the currently logged in user socket, and we can update the online status in real time! The only problem is that this is what we currently require, a little bit beyond the top and is not entirely necessary until our features need to reach a second accuracy, such as live chat.
A colleague pointed out that the way MySpace uses to handle "online" functionality may be enough for the current demand. As far as we know, the way MySpace uses to show whether users are online is based on their last activity on the website. If their last event is within X minutes, we will display the "online" badge and if not, we won't. Simple!
Let's add a field in the user table for the last activity of the user and update it when requesting each page. Then when we need to check if the user is online, we can compare that timestamp to the current timestamp, if within X minutes, they are online! While this can work well, it depends on the application you are building, it adds unnecessary writes to the database, which can slow down your application to some extent. A good tradeoff is to store this information in the application cache. The advantage of caching is that this approach can be simplified because the cache can be set to expire.
Now that we have decided to implement this feature with cache, the next question is where should this code run so that it runs on every request? I have two ideas to achieve:
- Create a BaseController so that all your controllers inherit it
- Create a middleware
After some thought and realizing that I need to add calls to the parent constructor in all the constructors I have chosen to implement in the middleware.
We have a plan, let's get into the code!
First, we need to create a middleware. Enter the following command in the terminal:
php artisan make:middleware LogLastUserActivity
Next we open the following php file
app/Http/Middleware/.
Add the following code to the handle method:
if(Auth::check()) { $expiresAt = Carbon::now()->addMinutes(5); Cache::put('user-is-online-' . Auth::user()->id, true, $expiresAt); }
Next, we open the app/Http/ file. If you are using Laravel 5.1 or earlier, you should place the code directly to$middleware
in the array. If your version is 5.2.* , you should place the code to$middlewareGroups
in the web. Note that you must put the code below the StartSession middleware, otherwise the Auth facade will not log the log in the user correctly. My update configuration settings are as follows:
protected $middlewareGroups = [ 'web' => [ \App\Http\Middleware\EncryptCookies::class, \Illuminate\Cookie\Middleware\AddQueuedCookiesToResponse::class, \Illuminate\Session\Middleware\StartSession::class, \Illuminate\View\Middleware\ShareErrorsFromSession::class \App\Http\Middleware\VerifyCsrfToken::class, \App\Http\Middleware\LogLastUserActivity::class, ], 'api' => [ 'throttle:60,1', ], ];
The last step is to add a method to our user object to detect this value. In app/ we add the following method:
public function isOnline() { return Cache::has('user-is-online-' . $this->id); }
Now you can add the following method in any page:
@if($user->isOnline()) user is online!! @endif
Important Tips -- Make sure to use use to introduce all facades at the top of your file!
Summarize
The above is the simple method used in Laravel in Laravel to track whether users are online. I hope it will be helpful to everyone. If you have any questions, please leave me a message and the editor will reply to everyone in time. Thank you very much for your support for my website!
If you think this article is helpful to you, please reprint it. Please indicate the source, thank you!