title: The best way to share WebSocket data between multiple components in Angular 2?
body: <p>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:</p>
  
  <p><a href="https://i.stack.imgur.com/IRVtW.jpg" rel="noreferrer"><img src="https://i.stack.imgur.com/IRVtW.jpg" alt="enter image description here"></a></p>
  
  <p>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.</p>
  
  <p>Now i'm using the next approach:</p>
  
  <p><div class="snippet" data-lang="js" data-hide="false" data-console="false" data-babel="false">
  <div class="snippet-code">
  <pre class="snippet-code-js lang-js prettyprint-override"><code>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);
      };
  }</code></pre>
  </div>
  </div>
  </p>
  
  <p>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.</p>