I start to build an application. There are a lot of components. Each of them need a part of data from 1 webSocket. The example of a receiveing object by webSocket:
Each Angular 2 component need 1 field from receiveing object. Is it possible to make 1 service, that will connect to webSocket, receive data and share it between all components ? I think it will be a good solution.
Now i'm using the next approach:
getConfigCallback() {
this.connectionSockets.telemetry = io(this.config.connections.telemetry);
this.connectionSockets.controlFlow = new WebSocket(this.config.connections.controlFlow.server + ':' + this.config.connections.controlFlow.port);
this.connectionSockets.telemetry.on('telemetry', function(data) {
console.log(data);
});
this.connectionSockets.controlFlow.onopen = function(data) {
console.log('onOpen', data);
};
this.connectionSockets.controlFlow.onmessage = function(data) {
console.log('onMessage', data);
};
}
I receive data in a main component and want to share it between components using component's instances. But i think it's a bad idea and there is exist a better solution.
bootstrap()
command where you setup your providers. Then in your components get the service via dependency injection from the constructor. Now you can use the service in every component you inject it. – rinukkusu