skip to Main Content

I can access and update the last seen of both users through web sockets.

I’m not able to set proper conditions to check if message is seen by the other user. I need advice to when should I update the last seen of user. I have set the condition to mark the message seen if last seen time of other user is after message’s create time. I’m updating the lastseen of current user on entering the chat so by this all messages before entering is marked seen.

I’m also updating the current user’s last seen at the time of sending message. What I’m currently lacking is how to know if the messages of being seen by other user when both of them are in the chatroom.

2

Answers


  1. In your message entry in the database, add a "seenBy" field as a array and add the users id as a single string entry or a object with the user id and timestamp when it was seen there.

    Login or Signup to reply.
  2. When a user connects to the chatroom the wsclient.onconnection event will be raised and when a user disconnects or leave the chatroom wsclient.onclose event will be raised so we can create a bool for any user which is connected

    const ws = new require('ws');
    const wss = new ws.Server({noServer: true});
    
    const clients = new Set();
    const clientsStatus = [false, false];
    const clientUsernames = ["User1","User2"];
    wss.on("connection",(ws) => {
        // ws here is the client which connected to server
        var thisIndex = -1;
        clients.add(ws);
        ws.on('message', function(message) {
          if(message[0]){
            if(message[0] == "Name"){
              for(var i in clientUsernames){
                if(message[1] == clientUsernames[i]){
                  clientsStatus[i] = true;
                  thisIndex = i;
                  // whenever a user connects we need to detect who is it
                  // you should say in the client that whenever client connected to server he should send his username to the websocket like this ["Name","User1"]
                }
              }
            }
          }
          else{
            for(let client of clients) {
              client.send(message);
            }
            if(clientsStatus[0] && clientsStatus[1]){ // when client status of both were true
              // any message must be marked as seen
            }
          }
        });
      
        ws.on('close', function() {
          clients.delete(ws);
          clientsStatus[thisIndex] = false;
          // whenever user disconnects the status will be offline
        });
    });
    

    In this example i said that its a private chatroom between 2 people which are named User1 and User2

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search