import { Injectable } from '@angular/core' ; import { Socket } from "ngx-socket-io" ; import { Observable } from "rxjs" ; import { UserLogon } from "../Types/user.logon" ; @ Injectable ( { providedIn : 'root' } ) export class ChatMessagesService { constructor ( private socket ) { } private JoinRoom = ( room ) => { this . socket . emit ( 'joinRoom' , room ) ; } ; public SendMessage = ( message ) => { this . socket . emit ( 'message' , message ) ; } ; public GetMessages = ( room ) => { this . JoinRoom ( room ) ; return Observable . create ( ( ob ) => { this . socket . fromEvent < UserLogon > ( 'userLogOn' ) . subscribe ( ( user ) => { ob . next ( `${ user . user } logged on at ${ user . time }` ) ; } ) ; this . socket . on ( 'message' , ( msg ) => { ob . next ( msg ) ; } ) ; this . socket . on ( 'allMessages' , ( msg ) => { msg . forEach ( ( text ) => ob . next ( text . messageText ) ) ; } ) ; } ) ; } ; }