Search code examples
angularwebsocketstore

Should a Websocket instance trigger store actions or should the store subscribe to socketIO events


Let us suppose that I have an application that has a couple of stores (no statemanagment library is used, the stores are services that use BehaviorSubjects to store data and expose observables and method to components so that the components can receive the data and trigger changes)

Now I want to integrate a real-time behaviour so I use a websocket and send events to the clients, when data has changed.

In my angular app I want to have one websocket-service that receives and handles all the events.

Now my question is the following:

I could store one Behavioursubject for each event in the socket-service and expose observables so that the stores can subscribe to the events and update their data.

Another approach would be to trigger store events from inside the websocket-service just like I do in my components.

Is any of this approaches better than the other one or are both flawed and I should implement it otherwise.

Thank you in advance.


Solution

  • Here In the first approach you are just looking to create a new Event Type as a behaviourSubject in socket-service which is unconventional. If we want the code to be consistent, we can go with the second approach and just trigger the events from socket-service, so that the service written to handle these events takes care of everything.