Skip to content

Commit

Permalink
Modified example in README
Browse files Browse the repository at this point in the history
Added example for unsubscribing when destroying a component.
  • Loading branch information
sclausen authored Apr 13, 2018
1 parent c7cddc8 commit ae6f4c2
Showing 1 changed file with 9 additions and 6 deletions.
15 changes: 9 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -86,22 +86,25 @@ export class AppModule { }
@Component({
template: `
<h1>{{mesage}}</h1>
<h1>{{(myOtherMessage$ | async)?.payload.toString()}}</h1>
`
})
export class ExampleComponent {
public myOtherMessage$: Observable<MqttMessage>;
export class ExampleComponent implements OnDestroy {
private subscription: Subscription;
public message: string;

constructor(private _mqttService: MqttService) {
this._mqttService.observe('my/topic').subscribe((message: MqttMessage) => {
this.myMessage = message.payload.toString();
this.subscription = this._mqttService.observe('my/topic').subscribe((message: MqttMessage) => {
this.message = message.payload.toString();
});
this.myOtherMessage$ = this._mqttService.observe('my/other/topic');
}

public unsafePublish(topic: string, message: string): void {
this._mqttService.unsafePublish(topic, message, {qos: 1, retain: true});
}

public ngOnDestroy() {
this.subscription.unsubscribe();
}
}
```

Expand Down

0 comments on commit ae6f4c2

Please sign in to comment.