Chron's avatar
Level 6

Track user's last activity

I want to track user's last activity. I'm using Vue, Inertia, and Laravel.

I think having a session table or adding a last_activity column in users table is fine. Then update that every request.

Wouldn't it be expensive performance-wise, since it creates transaction to the database every single request? What about recording that data to JS sessionStorage instead?

0 likes
13 replies
Glukinho's avatar

Laravel already has last_activity timestamp column in sessions table by default; I believe it updates on every request. Is that what you need?

1 like
Chron's avatar

That's probably it. Would it break if I remove ip_address, user_agent, and payload? I only need last_activity

Glukinho's avatar

Yes it would. Don't "fix" Laravel internal mechanisms, they work fine. Just use them.

imrandevbd's avatar

Writing to the DB on every single request will definitely crush your database once you start seeing real traffic.

JS sessionStorage won't work if you actually need the server to know their status (for example, to show an "online" badge to other users or for backend analytics), since it only lives in the client's browser.

JussiMannisto's avatar

Writing to the DB on every single request will definitely crush your database once you start seeing real traffic.

That's a massive hyperbole, unless your definiton of "real traffic" is thousands of requests per second. And then you're hitting other bottle necks first.

If Laravel accesses the DB during each request, e.g. by pulling a user model, the additional cost of updating a single timestamp is minimal. Most of the overhead comes from establishing a database connection.

2 likes
Chron's avatar

I'm only using database as the session driver. Would it cause problem?

JussiMannisto's avatar

It's no problem if you don't see any problems.

If you start seeing database errors due to a high volume of traffic, you can switch to something like Redis.

As already mentioned, the sessions table already has a last_activity column (Unix timestamp) that's updated on every request.

1 like
Chron's avatar
Level 6

I'll use database driver for now. Would it break if I remove the cols other than the FK and last_activity, though?

JussiMannisto's avatar

Yes it would. Where would the session data be stored? And why are you even thinking about removing columns?

Chron's avatar

I just want the time when the user was last active. The sessions table provided by laravel could suffice it. Would it break if I remove the cols other than the fk and last_activity?

Chron's avatar

The time when user was active

Please or to participate in this conversation.