-
Notifications
You must be signed in to change notification settings - Fork 32
How to use multiple subscriptions and unsubscribes? #2
Comments
Yes, you can subscribe any number of queues. Just make multiple calls to subscribe. Please check the samples as well. If you are new to dependency injection, please add the StompService to |
And each subscription must be assigned to a different observable? |
When you make a call to subscribe, a new Observable is returned, which will provide messages for that queue. |
Forgive me I am not web dev expert.
or
|
The first one: this.messages = this._stompService.subscribe('/topic/ng-demo-sub'); |
and to unsubscribe
is this enough for both subscriptions? |
No, the code will look something like following (I haven't executed the code, just typed here): // use messages from /topic/ng-demo-sub by subscribing to the Observable
this.messages = this._stompService
.subscribe('/topic/ng-demo-sub')
.subscribe((msg: Message) => {
// use it
});
// use messages from /topic/ng-demo-sub2 by subscribing to the Observable
this.messages2 = this._stompService
.subscribe('/topic/ng-demo-sub2')
.subscribe((msg: Message) => {
// use it
});
// Unsubscribe from /topic/ng-demo-sub
this.messages.unsubscribe();
this.messages = null;
// Unsubscribe from /topic/ng-demo-sub2
this.messages2.unsubscribe();
this.messages2 = null; The above has been updated. |
In summary both the subscriptions are independent of each other and do not affect each other. |
ok thanks a lot |
I am using StompRService and when I call |
@marcelormourao, please create a demo project demonstrating the issue. You can fork one of the demo projects (links in the README). Thanks! Meanwhile I am also taking a look. |
I has a similiar problem =( |
I have tested the following sequence (I have added these and few more cases in queSubscription1 = stompService.subscribe(queueName1)
.map((message) => message.body)
.subscribe(spyHandler1);
queSubscription2 = stompService.subscribe(queueName2)
.map((message) => message.body)
.subscribe(spyHandler2);
stompService.publish(queueName1, 'Message 01-01');
stompService.publish(queueName2, 'Message 02-01');
queSubscription1.unsubscribe();
stompService.publish(queueName1, 'Message 01-02');
stompService.publish(queueName2, 'Message 02-02'); Please see the log output corresponding to the above call. Lines starting with **** are my annotations.
|
Please note that I have updated my schematic code at #2 (comment) |
@kum-deepak I am having the similar issue. I reported it here #73. Basically it does not work if I specify a subscription callback via an inline function then I am having that issue. I did not try anonymous function like you had in your example - that might work but I still would like to be able to set inline function as a callback to the topic Observable subscribe. Thanks |
If you pass stomp headers to |
@s1vert, seems bit peculiar behavior. Please fork any of the samples and modify it to show the issue. I will like to have a look at the issue. |
@s1vert I think I undertsand the issue now. It will need to be fixed in underlying library as well as in this one. It will be done in next major release. |
would this be part of 7.0.0? |
@sahyun1 this thread has two different issues mentioned. The original question - multiple subscriptions - that has always worked. There was one more issue that some functions were altering the header argument. It has been fixed in underlying library stomp-js/stompjs#11. It will be part of v7. If you mean any other issue, please create a new one 😄 |
Cant we do it dynamically ? |
Could not understand your query. Please post some code sample. |
get(demo) { } this.get(ng-demo-sub); // Unsubscribe from /topic/ng-demo-sub cant we unsubscribe from one observable |
Probably you want the following (I haven't executed the code, just typed here): get(topic) {
return this._stompService.subscribe(`/topic/${topic}`).subscribe((message) => {
// Do something
});
}
// Subscribe
this.messages = this.get('demo1');
this.messages2 = this.get('demo2');
// Unsubscribe from /topic/ng-demo-sub
this.messages.unsubscribe();
this.messages = null;
// Unsubscribe from /topic/ng-demo-sub2
this.messages2.unsubscribe();
this.messages2 = null; |
If the number of new variables gets out of hand, you may use an array to collect all the subscriptions and use a loop to unsubscribe in the end. This also guarantees that you don't forget about any subscriptions. This looks like a good place to use a pattern like subsink. You just throw a bunch of subscriptions at it, then call unsubscribe once. The code behind this library is dead simple, if it doesn't suit you, it's pretty easy to make your own implementation. |
Hi,
Using one instance of a StompService can I subscribe to multiple queues?
The text was updated successfully, but these errors were encountered: