I am working on a chat application using angular 2.
How can i send the finish chat command to the backend when the user closes the window?
My component has a method that calls the backend service to end the chat in the following way
 endChat() {
        this.chatService.endChat(this.chatSessionInfo).subscribe(
            result => this.OnGetMessages(result),
            error => this.OnChatEndError(error)
        );
    }
How can i execute that code when closing the window? How can i detect the window close event?
I tried with ngOnDestroy but for some reason the code is not being executed.
In my Component.ts I have.
import { Component, OnInit, AfterViewChecked, ElementRef, ViewChild,OnDestroy} from '@angular/core';
export class ChatComponent implements OnInit, AfterViewChecked,OnDestroy  {
and finally
 ngOnDestroy() { 
    this.endChat();
 }
Thanks!