My angular application uses a websocket to communicate with the backend.
In my test case I have 2 client components. The Observable timer prints two different clie
You need to use the share
operator to share it among the subscribers.
this.observable = Observable.create(
(observer: Observer) => {
socket.onmessage = observer.next.bind(observer);
socket.onerror = observer.error.bind(observer);
socket.onclose = observer.complete.bind(observer);
return socket.close.bind(socket);
}
).share();
Also make sure this service is a singleton.
Doc: https://github.com/Reactive-Extensions/RxJS/blob/master/doc/api/core/operators/share.md