I'm creating an Angular application which sends and receives data over a websocket connection. I have one service SocketService which opens and keeps the connection, that service is injected into other services.
Each message on the websocket looks like {ctrl: string, payload: any} and a Subject is created for each controller (ctrl) in the SocketService, to which other services can subscribe, so each service subscribes only to the messages intended for it.
With an increasing amout of services and therefor controllers I have an increasing amount of Subjects in the SocketService and now I was wondering if it was more efficent to let each service subscribe to the websocket messages directly and have the service itself check if the message was intended for it or not. But that would mean that each message on the socket triggers lots of subscribers.
// in the socketservice
private createSocketSubscription(): void {
// Snipped from the socket service when receiving messages on the socket connection
this._subscription = this._subject.pipe(map(msg => JSON.parse(msg.data))).subscribe((data) => {
const ctrl = data[this.CTRL] || 'common';
this._registry[ctrl].next(data[this.PAYLOAD]);
}
}
public registerController(ctrl: string, def?: any) {
if (!(this._registry[ctrl]))
this._registry[ctrl] = new BehaviorSubject(def?def:{});
return this._registry[ctrl].asObservable();
}
// in some service
constructor(private socketService: SocketService) {
this.socketService.registerController('something').subscribe(payload=> {
// do stuff with the payload
});
}
versus
// in some service
constructor(private socketService: SocketService) {
this.socketService.getSubject().subscribe(data => {
if(data['ctrl'] === 'something') {
// do stuff with the payload
}
});
}
Which approach is more efficient or does it even make a difference? I left out stopping and unsubscribing processes, I only wonder if it is better to have few subscriptions to many subjects or many subscriptions to a single subject?