JSteunou / webstomp-client

Stomp client over websocket for browsers
Apache License 2.0
299 stars 59 forks source link

client.unsubscribe and resubscribe causes duplicate messages #79

Closed n-of-one closed 5 years ago

n-of-one commented 5 years ago

I encountered very strange (and buggy) behaviour when I ran:

client.subscribe('/topic/scan/123');
...
client.unsubscribe('/topic/scan/123');
...
client.subscribe('/topic/scan/123');

client.send(...)

This caused two messages to be sent. And every-time I unsubscribed and re-subscribed, an extra message gets sent when I call client.send() (so 2,3,4...)

The docs state:

It is preferable to unsubscribe from a subscription by calling unsubscribe() directly on the object returned by client.subscribe()

And this indeed solved the issue. However, it still feels like a bug to me. Why not just remove the client.unsubscribe() function?

JSteunou commented 5 years ago

Because this method was already present in the original library I forked.

Removing it would be a breaking change and I started this fork just for maintenance, not for major releases.

leandrogaspar commented 5 years ago

Hi @erikaadvisser, you are using the client.unsubscribe() method wrongly. The unsubscribe method accepts two args, the 'id' and the 'header':

unsubscribe(id, header={})

You are trying to unsubscribe using the destination.

So, both ways work:

const subscription = client.subscribe('/topic/scan/123');
...
client.unsubscribe(subscription.id);
const subscription = client.subscribe('/topic/scan/123');
...
subscription.unsubscribe();

The subscription 'unsubscribe' already bind the id arg for you.

n-of-one commented 5 years ago

Thanks for taking the time to clarify it, and include examples. It now makes sense to me again.