I am currently developing a chat application using Angular 2 and I have a question. How can I trigger the finish chat command to the backend when the user closes the chat window?
Within my component, I have a method that interacts with the backend service to end the chat session:
endChat() {
this.chatService.endChat(this.chatSessionInfo).subscribe(
result => this.OnGetMessages(result),
error => this.OnChatEndError(error)
);
}
My concern is how can I automatically execute this code when the user closes the chat window? Is there a way to detect the closing event of the window?
I attempted to use ngOnDestroy within my Component.ts file but unfortunately, the code did not run as expected.
import { Component, OnInit, AfterViewChecked, ElementRef, ViewChild, OnDestroy } from '@angular/core';
export class ChatComponent implements OnInit, AfterViewChecked, OnDestroy {
In order to address this issue, I added the following block of code:
ngOnDestroy() {
this.endChat();
}
Your insights on this matter would be greatly appreciated!