Angular 2 - Executar código ao fechar a janela

Estou trabalhando em um aplicativo de bate-papo usando o angular 2.

Como posso enviar o comando finalizar bate-papo para o back-end quando o usuário fecha a janela?

Meu componente possui um método que chama o serviço de back-end para encerrar o bate-papo da seguinte maneira

 endChat() {
        this.chatService.endChat(this.chatSessionInfo).subscribe(
            result => this.OnGetMessages(result),
            error => this.OnChatEndError(error)
        );
    }

Como posso executar esse código ao fechar a janela? Como posso detectar o evento de fechamento da janela?

Eu tentei com o ngOnDestroy, mas por algum motivo o código não está sendo executado.

No meu Component.ts eu tenho.

import { Component, OnInit, AfterViewChecked, ElementRef, ViewChild,OnDestroy} from '@angular/core';

export class ChatComponent implements OnInit, AfterViewChecked,OnDestroy  {

e finalmente

 ngOnDestroy() { 
    this.endChat();
 }

Obrigado!

questionAnswers(2)

yourAnswerToTheQuestion