-1

I have a private websocket, this websocket brings me information about the user like balance, notification and other things. all application needs to use this websocket. I have a component where I connected in this websocket and i created an emitter to broadcast when a new message is received. example: websocket component receive message and emitter a event to component x.

Is this the best way to implement this?

I`m using angular 6 to build this application.

2 Answers2

0

As I know that's like the best way, put the websocket in a common componente that the application uses.

Just take care about the information that you use in that websocket, I mean just emmit the information that you need, when your application has a lot of websockets, it could crash, it will depends on how you handle the websockets, if you are emmiting that just to the user that should receive that, if you send just the information that you need, if you don't make not necessary websockets calls.

Joseph Arriaza
  • 12,014
  • 21
  • 44
  • 63
0

My recommendation is to provide a service which will house the socket connection and setup.

The idea is to let the service dispatch the socket data across as many components that needs the data. Additionally by doing this as a service you can guarantee that Angular can make sure there is only one copy of the service and hence remove any possible duplicate connections that aren't necessary.

Mike Tung
  • 4,735
  • 1
  • 17
  • 24