thelovemsg / chatting_app

0 stars 0 forks source link

How can we check If user is online or not? #26

Open thelovemsg opened 1 year ago

thelovemsg commented 1 year ago

Objective

How can we check If user is online or not?

Detailed work content

  • Let's talk about how application detect if user is online or not?
  • It's quite important because we need to send notification if user haven't read the message yet.
thelovemsg commented 1 year ago

I'm curious how we can know others are online or not.

Whether or not a user is online in a chat application can be determined by various means. Here are a few methods

1. Websockets

As you mentioned, when a user opens the chat application in a browser or on their device, a WebSocket connection can be established between the client and server. The server can then keep track of which connections are currently open and treat those users as being online. When the WebSocket connection is closed, either because the user closed the application or due to a network issue, the server would then mark the user as offline.

2. Last Activity Timestamp

Another approach is to track the timestamp of the user's last activity (e.g., sending a message, reading a message). If the timestamp is within a certain period (e.g., the last 5 minutes), the user can be considered online. This method doesn't require a persistent connection like a WebSocket, but it may not be as accurate since a user could leave the app open without performing any actions and still be considered online.

3. Periodic Polling or Heartbeat

The client application could periodically send a "heartbeat" or "ping" message to the server to indicate that it's still online. If the server hasn't received a heartbeat from a client within a certain timeframe, it could consider the client to be offline. This could be more resource-intensive than the other methods, particularly if the client application needs to send heartbeats frequently to keep the online status up-to-date.

4. Presence Information from a Third Party

If your chat application is integrated with a larger system, like a social network or a gaming platform, it might be able to get presence information (i.e., whether each user is online) from that system.

In practice, a chat application might use a combination of these methods to determine which users are online, depending on its specific requirements and constraints.

thelovemsg commented 1 year ago

Periodic poilling might be the great choice to check user is online or not.

Me

I think periodic polling is better for me to know whether is online or not. Unlike chatting with others, just noticing if user is online or not doesn't need real-time work. I think checking per 5 minutes if user is online is fine.

ChatGPT

That's a reasonable decision. Periodic polling (or sending "heartbeat" signals) at a specified interval can indeed serve the purpose of monitoring user's online status.

The process could be implemented in the following way:

  1. Client Side The client application periodically (every 5 minutes, as per your preference) sends a heartbeat signal to the server. This can be a simple API call indicating that the user is active.

  2. Server Side The server, upon receiving this signal, updates the 'last active' timestamp for that user in the database.

  3. Check Online Status When needed, the server checks the 'last active' timestamp of the user to determine their online status. If the timestamp is within the last 5 minutes (or whatever your threshold is), the user is considered to be online.

Keep in mind that if a user closes their app or loses internet connectivity, the server won't receive a "user is offline" signal. That's why the server considers a user offline if it hasn't received a heartbeat signal for a certain amount of time.

As always, be aware of the trade-offs. Sending heartbeat signals adds extra network traffic and can increase load on the server, but as you've noted, for an application where real-time presence information isn't critical, this can be an acceptable trade-off.

thelovemsg commented 1 year ago

How we check user's last action?

ME

When I want to know what user is online or not by checking last active timestamp, What behaviors can be thought as last active?

chatGPT

Deciding what constitutes user activity can be a tricky balance. On one hand, you want to accurately represent when a user is actively engaged with your application. On the other hand, you don't want to overwhelm your server or your users' devices with constant updates.

Here are a few user actions that might be considered "activity" in a chat application:

  1. Sending a message This is a clear indicator that the user is actively engaged with your app.

  2. Reading a message If the user is actively reading messages, even if they're not sending anything, they are still using your app.

  3. Opening the app When a user opens the app, it's reasonable to assume they're planning on using it.

  4. Switching conversations If a user is switching between different conversations, that indicates they're actively using your app.

  5. Interacting with the app's UI This could include things like opening menus, setting preferences, or any other interactions with your application beyond just sending and reading messages.

Remember, you don't need to update the user's activity status after every single action. You might decide to only update the status if the last update was more than a certain amount of time ago (e.g., 1 minute). This can help prevent excessive updates while still giving a reasonably accurate picture of the user's activity.

Also, be mindful of privacy considerations. Make sure your users are aware of what data you're tracking and why, and provide them with options to control it if possible.

thelovemsg commented 1 year ago

It might be okay to check if user is sending message or not.

Me

I think just checking if user is sending message or not is enough.

ChatGPT

That's perfectly fine. Simplicity is key in design, and if checking for message sending activity meets the needs of your application, then that's the way to go. This would mean that a user's 'active' or 'online' status would update every time they send a message.

You can manage this easily by updating the 'last active' timestamp in your user's data each time a message is sent. Then, your application can check if the last active timestamp was within a certain threshold (like 5 minutes ago) to determine if the user is considered online.

This kind of activity detection is straightforward and effective for many chat applications. However, keep in mind that this method assumes that all active users are frequently sending messages, which might not always be the case. Depending on the nature of your application and its users, you may need to consider other types of activity as well.