import { Injectable } from '@angular/core' ; import { Observable } from 'rxjs' ; import { Observer } from 'rxjs' ; import { Message } from '../model/message' ; import { Event } from '../model/event' ; import * as socketIo from 'socket.io-client' ; const SERVER_URL = 'http://localhost:8080' ; @ Injectable ( ) export class SocketService { private socket ; public initSocket ( ) { this . socket = socketIo ( SERVER_URL ) ; } public send ( message ) { this . socket . emit ( 'message' , message ) ; } public onMessage ( ) { return new Observable < Message > ( observer => { this . socket . on ( 'message' , ( data ) => observer . next ( data ) ) ; } ) ; } public onEvent ( event ) { return new Observable < Event > ( observer => { this . socket . on ( event , ( ) => observer . next ( ) ) ; } ) ; } }