import { IMessage , Message , Room } from "../../models" ; export class MessageSocket { nsp : any ; data : any ; socket : any ; constructor ( io , public room ) { this . nsp = io . of ( "/messages/" + encodeURIComponent ( this . room . name ) ) ; this . nsp . on ( "connection" , ( socket ) => { console . log ( "Client connected to room:" , this . room . name ) ; this . socket = socket ; this . listen ( ) ; } ) ; } private listen ( ) { this . socket . on ( "disconnect" , ( ) => this . disconnect ( ) ) ; this . socket . on ( "create" , message => this . create ( message ) ) ; this . socket . on ( "list" , ( ) => this . list ( ) ) ; } private disconnect ( ) { console . log ( "Client disconnected from room:" , this . room . name ) ; } private create ( params ) { params . room = this . room . name ; Message . create ( params ) . subscribe ( message => this . nsp . emit ( 'item' , message ) , error => console . error ( 'Message sending failed' , error ) ) ; } private list ( ) { this . room . messages ( ) . map ( messages => messages . reverse ( ) ) . subscribe ( messages => messages . map ( message => this . socket . emit ( message ) ) ) ; } }