1
1
1programmer2019-06-21 17:34:47
React
1programmer, 2019-06-21 17:34:47

How can I set a timer for the user status?

There is a user status function, online/offline. These statuses are transmitted via sockets, that is, if the interlocutor connects via the channel, his status changes, if he disconnects, then the status is offline.
Now this function works with one problem, if the interlocutor reloads the page, his status will first become offline then online, that is, this is a waste of resources, if one user did not go where, if 1000, then dispatch will constantly go to delete / add status. How can I set a timer for deletion, so that if the user left, then his status only became offline after 5 minutes. And not with any reboot, the interlocutor's page jumped.
Function code below

listenOnlineUsers(conversation) {
    for (let i = 0; i < conversation.members.length; i++) {
      let member = conversation.members[i];
      if (member.id == this.props.user.id) continue;
      let channel = 'IsOnline.' + member.id;

      // this.leaveChannel(channel);
      Echo.join(channel)
        // все пользователи в канале
        .here((users) => {
          for (let i = 0; i < users.length; i++) {
            if (users[i].id != this.props.user.id) {
              if (!this.props.onlineUserIds.includes(users[i].id)) {
                this.props.pushOnlineUserId(users[i].id)
              }
            }
          }
        })
        // просоединившийся пользователь
        .joining((user) => {
          if (this.props.onlineUserIds.includes(user.id)) return
          this.props.pushOnlineUserId(user.id)
        })
        // ушедший
        .leaving((user) => {
        
            if (!this.props.onlineUserIds.includes(user.id)) return
            this.props.removeOnlineUserId(user.id)
        
        });
    }
  }

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question