The best way to share WebSocket data between multiple components in Angular 2?

Edward

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:

enter image description here

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.

Thierry Templier

Sure you can share your service by set its provider when bootstrapping the application:

bootstrap(AppComponent, [ WebSocketService ]);

This way you will share the same instance of your service in the whole application. I mean when you inject the WebSocketService service, the corresponding instance will be the same whatever the component / service.

To be able to share the received data, I would leverage an hot observable. By default observables are cold so you need to use the share operator.

initializeWebSocket(url) {
  this.wsObservable = Observable.create((observer) => {
    this.ws = new WebSocket(url);

    this.ws.onopen = (e) => {
      (...)
    };

    this.ws.onclose = (e) => {
      if (e.wasClean) {
        observer.complete();
      } else {
        observer.error(e);
      }
    };

    this.ws.onerror = (e) => {
      observer.error(e);
    }

    this.ws.onmessage = (e) => {
      observer.next(JSON.parse(e.data));
    }

    return () => {
      this.ws.close();
    };
  }).share();
}

Components that would like to receive data from the web socket could use this observable this way:

@Component({
  (...)
})
export class SomeComponent {
  constructor(private service:WebSocketService) {
    this.service.wsObservable.subscribe((data) => {
      // use data
    });
  }
}

See this article for more details in the "event-based approach" section:

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Angular: Best way to pass data between components?

angular 2 share data between components

Angular2 Share data between components

what's the best way to share user data between components?

Best way to share data between two child components in Blazor

Share data between components In Angular

Share data between components using Service - IONIC 2, Angular 2

Angular 2 share websocket service across components

how to decide the best way to communicate between components in Angular 2?

Angular 2: Using service to share data between components

How do I share data between components in Angular 2?

Angular2 - Share data between components using services

Share data between components using a service in Angular2

Any way to share data between Polymer components?

best way to share data between pages in flutter

How to share data between angular components?

How to share data between components in angular

How to share components between modules in Angular 2?

How to share an array between components in Angular 2?

How to use two way data binding between components in angular 2?

Angular 2, best practice to load data from a server one time and share results to components

Angular - Share Data Between Sibling Components Without Sharing Between Instances

Rust - best way to share a hashset in a structure between multiple workers

What's the best way to share a dbcontext between multiple services?

Angular 2 share/change data got from HTTP service between components

React - most efficient way to share data between components

How to share api response between multiple Angular Components?

Reactjs: how to share a websocket between components

vuejs - share websocket connection between components