Search code examples
angulardatetypingindicator

Angular 8 - Typing indicator from websocket


I'm currently working on web chat in Angular 8. I am connected to websocket, and I'm receiving information about that someone is typing a message. It is called "Typing indicator". I get this information everytime someone is typing something to me. Unfortunately, I'm not receiving information that someone has stopped typing, and I need to implement logic, that if someone has stopped typing, I'm showing typing indicator for only 4 seconds long.

public showTypingIndicator() {
  const currentDate = new Date();
  currentDate.setSeconds(currentDate.getSeconds() - 4);

  return this.lastTypingDate !== null && this.lastTypingDate > currentDate;
}

My this.lastTypingDate I get from my service in case in my websocket I received a message with type "Typing-indicator", and then I set this to this.lastTypingDate = new Date();. This is how I pass data to this variable from my service: this.myDateService.getLastTypingDate().

This works, but it is showing my typing indicator for longer time than 4 seconds. Any thoughts what can be changed here to make it works as expected?

Thank you in advance!


Solution

  • I figured this out by using rxjs and BehaviorSubject. I'm passing value to BehaviorSubject to true, only when subscription receives some data.