blob: a5852c8ce8848523bb06ebbc1b8d348d95044215 [file] [log] [blame]
Matteo Scandolo035c5932016-12-14 09:55:15 -08001import * as io from 'socket.io-client';
2import {Subject, Observable} from 'rxjs/Rx';
Matteo Scandolo828d1e82017-01-17 14:49:38 -08003import {IXosAppConfig} from '../../../index';
Matteo Scandolo035c5932016-12-14 09:55:15 -08004
5export interface IWSEvent {
6 model: string;
7 msg: {
8 changed_fields: string[],
9 object?: any,
10 pk?: number
11 };
12}
13
14export interface IWSEventService {
15 list(): Observable<IWSEvent>;
16}
17
18export class WebSocketEvent {
Matteo Scandolo828d1e82017-01-17 14:49:38 -080019
20 static $inject = ['AppConfig'];
21
Matteo Scandolo035c5932016-12-14 09:55:15 -080022 private _events: Subject<IWSEvent> = new Subject<IWSEvent>();
23 private socket;
Matteo Scandolo828d1e82017-01-17 14:49:38 -080024 constructor(
25 private AppConfig: IXosAppConfig
26 ) {
27 this.socket = io(this.AppConfig.websocketClient);
Matteo Scandolo035c5932016-12-14 09:55:15 -080028 this.socket.on('event', (data: IWSEvent): void => {
29 this._events.next(data);
30 });
31 }
32 list() {
33 return this._events.asObservable();
34 }
35}