Laravel uses a simple way to track whether users are online


The article was forwarded from the professional laravel developer community. Original link:

Today, my task is to add a green dot next to the user name on the user profile page of the laravel application to indicate whether they are online or not. The first thing I think about is that we will need to start a node. JS server and track each user’s active socket connections. Then with the current login user socket, we can update the online status in real time! The only problem is that this is our current requirement, a little over the top, and it’s not completely necessary until our function needs to achieve a second accuracy, such as real-time chat.

One colleague pointed out that the way MySpace deals with “online” features might be enough for the current needs. As far as we know, the way MySpace uses to show whether users are online is based on their last activity on the site. If their last activity is within X minutes, we will display the “online” badge, if not, we will not. Simple!

Let’s add a field to the user table for the user’s last activity and update it when each page is requested. Then when we need to check whether users are online, we can compare the time stamp with the current time stamp. If within X minutes, they are online! While this works well, depending on the application you are building, it will add unnecessaryWrite in, which will slow down your application to some extent. A good compromise is to store this information in the application cache The advantage of caching is that it can be simplified because you can set the cache to expire.

Now that we’ve decided to implement this feature with caching, the next question is where should this code run so that it runs on every request? I have two ideas:

  1. Create a basecontroller and let all your controllers inherit it
  2. Create a middleware

After some thinking and realizing that I need to add a call to the parent constructor in all the constructors I have written, I choose to implement it in middleware.

We have a plan, let’s enter 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
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 / kernel.php file. If you are using laravel 5.1 or earlier, you should place the code directly into the $middlewareArray If your version is 5.2. *, you should place the code in the$middlewareGroupsIn the web. Be sure to put the code inStartSessionUnder the middleware, otherwise the auth portal will not log correctly in the user. My update configuration settings are as follows:

protected $middlewareGroups = [
    'web' => [
    'api' => [

The last step is to add a method to our user object to detect the value. In APP / user.php, we add the following methods:

public function isOnline()
    return Cache::has('user-is-online-' . $this->id);

Now you can add the following methods to any page:

    user is online!!

Important – make sure to use use at the top of your file to bring in all facades!

I hope the above can help you to solve this problem!

Recommended Today

Summary of computer foundation in autumn 2021 interview – Java foundation, JVM, spring framework

Series articles: Summary of computer foundation in autumn 2021 interview algorithm, data structure, design pattern, Linux Summary of computer foundation in autumn 2021 interview – Java foundation, JVM, spring framework Summary of computer foundation for 2021 autumn recruitment interview database, redis Summary of computer foundation for 2021 autumn recruitment interview – operating system Summary of […]