Skip to content

Commit fef182d

Browse files
committed
Stopping the watch
1 parent 41d3da1 commit fef182d

File tree

1 file changed

+9
-3
lines changed

1 file changed

+9
-3
lines changed

src/app/messages/messages.component.ts

+9-3
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,29 @@
1-
import { Component, OnInit } from '@angular/core';
1+
import { Component, OnDestroy, OnInit } from '@angular/core';
22
import { RxStompService} from '@stomp/ng2-stompjs';
33
import { Message } from '@stomp/stompjs';
4+
import { Subscription } from 'rxjs';
45

56
@Component({
67
selector: 'app-messages',
78
templateUrl: './messages.component.html',
89
styleUrls: ['./messages.component.css']
910
})
10-
export class MessagesComponent implements OnInit {
11+
export class MessagesComponent implements OnInit, OnDestroy {
1112
public receivedMessages: string[] = [];
13+
private topicSubscription: Subscription;
1214

1315
constructor(private rxStompService: RxStompService) { }
1416

1517
ngOnInit() {
16-
this.rxStompService.watch('/topic/demo').subscribe((message: Message) => {
18+
this.topicSubscription = this.rxStompService.watch('/topic/demo').subscribe((message: Message) => {
1719
this.receivedMessages.push(message.body);
1820
});
1921
}
2022

23+
ngOnDestroy() {
24+
this.topicSubscription.unsubscribe();
25+
}
26+
2127
onSendMessage() {
2228
const message = `Message generated at ${new Date}`;
2329
this.rxStompService.publish({destination: '/topic/demo', body: message});

0 commit comments

Comments
 (0)