How to update a User’s Status in realtime?

I have a system which has two types of users. One is a seller and the others are the customers. I want all the users to get the details about all the seller who are currently online in real-time.
Basically, like a chat system where the users know if the users are online or not.
Whenever the seller logs into the my project, the customer should know that he is online. The same procedure should occur when the user exits the project (The customer should know that the seller went offline) .
Remeber that offline scenario could be that the seller logged out or closed the project tab or closed the browser. In any case if the seller leaves the project, others must be aware of it.

How can I achieve this ?

Can anyone help here ?

You could keep a list of active sessions/sellers in a database table with last time of activity.

On each request to server action you should update last activity time on the seller that made the request. “Online sellers” are on this table with, lets say, last activity < 1 min ago

This is not exactly real-time though, maybe you can make someting real time with the sockets but don’t have an idea atm how.

If we are talking about real real time, then we are talking about sockets. It’s very easy to do what you need with sockets. To do this, we use two preset events in the server actions in the sockets section:

  1. When the client is connected, we run the following steps:

    Step 1 determines the socket id of the connected client, and step 2 update the correct data on the authorized user to the database.
    Inside the update, everything is simple:
    There is a special field that reflects whether the user is online now or not. And there is also a field in which we store the current socket id (in case we need to send something via socket to this particular client, we will have his address).

  2. An even simpler step when disconnecting the client:

    Inside the update, we simply change the entries to 0, which means that the user is not online and his address is missing:


Thank you @Mr.Rubi. It worked

I have few doubts here.

  1. Will the socket id be unique to a user.

  2. If a user opens multiple tabs then multiple times socket will be connected, How can I handle this.

  3. How can I handle disconnect when there are multiple tabs

  4. Which is the best place to add socket, to start the connection

1 Like

I’m glad that everything worked out!

For questions:

  1. Yes, each socket is unique. And not only in the context of users, but also in the context of different tabs. The logic is simple - each new socket = a new unique id.

  2. I did not solve this problem, because I used sockets mainly in SPA. But if you think about the problem, the first thing that comes to mind is that I would make a separate table for records in which the user status and socket are recorded. Thus, if the user opens several tabs, several records will be created in this table. As a result, we will be able to have the id of all sockets belonging to a specific user.

  3. The approach above will help to do this. If there is a separate entry for each tab, then when the tab is closed, the entry will be deleted. However, there will be entries for those tabs that are still open.

  4. I will add a socket to the layout that is used for pages that are after authorization. The authorization page does not use sockets, because my applications work only with authorized users.

1 Like

Thank you for your reply.

Issues with the solution of 2 and 3. As you said, I will create multiple rows of data of a user with different socket id. Here remeber that, same user is opening multiple tabs with the same account. Which means multiple rows will be cloned for the same user with different socket id. And if I show this details in front end the user will get confused when they see same user repeated multiple times. So, What should I do here ?

1 Like

I wasn’t talking about the user table. I wrote that it is necessary to create a separate table where records of open sockets will be created. Each open socket entry will refer to one user entry in the user table. The data from the socket table does not need to be shown to the user at all, since they serve only for your internal technical things. In this case, it will be easy to determine whether the user is online or not - if there is at least one entry in the socket table, it means online. At the same time, we will always have socket addresses as long as the user has at least one open window. That’s what I meant.

1 Like

I understand your point of view. But let’s just say you have to use this data in front end. Like you want to list the users who are online. How can I achieve this without any data duplication ?

A separate table with sockets is made only in order to solve the problem of opening/closing several tabs in the browser by the user.

The list of users with the status (online/offline) is based on another table (users table). At the same time, if you have data duplication when building such a list in the application, this only means that you are incorrectly creating a query to the database and are not using other workarounds in your server action to get around it.

How to build the server logic correctly so that the list of users with the status is without duplicates, if an additional table with several records on sockets is used?

It’s simple:

  1. The simplest option is a properly composed query:

    We associate the table with sockets with the user table (join left). We take all the necessary fields from the user table, and only one from the socket table (for example, record id). Next, we summarize all the values by field from the socket table. In this case, all duplicates will collapse and the request will return only one record to each user, while the sum of the ids of all records will be indicated in the field from the socket table. Thus, we will easily identify the on-line/offline user at the moment. If the field will contain any value = online. If the field is empty = offline. And no duplicates!

  2. The second option is also not complicated, but less effective because it will additionally load the web server. In the server action, you use a repit, the data source for which is the user table, and inside the repit you are already checking for each individual user. The check is also very simple - if there is at least one entry in the socket table = online user, if there are no entries = offline user.

To summarize. The table with sockets does not prevent you from creating a list of users with the status. However, it helps to solve the problem if your application is not a SPA and the user can open/close several tabs in it.