roslibjs icon indicating copy to clipboard operation
roslibjs copied to clipboard

Duplicate subscribe messages!

Open fabioaraujopt opened this issue 6 years ago • 7 comments

I'm using the following configuration:

`var ros = new ROSLIB.Ros({ url : 'ws://x.x.x.x:9090' });

listener=new ROSLIB.Topic({ ros : ros, name : '/vigil/sensor/gps/navsatfix', messageType : 'sensor_msgs/NavSatFix' });

listener.subscribe(function(message){ listener.unsubscribe(); console.log(message); }); ` I placed the subscribe function inside a javascript setInterval, in order to call it everytime i need data, instead of being always listening to broadcast. However every time a new subscribe is done. The number of response doubles. (I think it receives all messages from begin).

first_call -> 1 log first_call -> 3 logs first_call -> 4 logs first_call -> 5 logs first_call -> 6 logs

fabioaraujopt avatar Sep 23 '19 17:09 fabioaraujopt

Quote from the documentation

unsubscribe(callback)

Unregisters as a subscriber for the topic. Unsubscribing stop remove all subscribe callbacks. To remove a call back, you must explicitly pass the callback function in.

callback: the optional callback to unregister, if * provided and other listeners are registered the topic won't * unsubscribe, just stop emitting to the passed listener

Rayman avatar Sep 23 '19 19:09 Rayman

Can you show me example? Didn't understand

fabioaraujopt avatar Sep 24 '19 14:09 fabioaraujopt

Something like this (not tested)

function handler(message) {
	console.log(message);
	listener.unsubscribe(handler);
}

listener.subscribe(handler);

Rayman avatar Sep 24 '19 14:09 Rayman

Can you show me example? Didn't understand

Hey, did you figure out the solution for this?

asvinp avatar May 18 '20 23:05 asvinp

@asvinp @fabioaraujopt Any solution?

I'm unsuscribing by providing the callback to the unsubscribe function but I'm ending up with an error.

ManuelZ avatar Feb 18 '21 02:02 ManuelZ

I had this issue when using React Hooks.

I was clicking a button to get subscribed and then again the same button to get unsubscribed from a topic.

  • I had my topics declarations inside my component... solution, to move them out.
  • I needed to memoize the function that I was using to switch the subscription on/off and as well memoize the function callback passed to topic.unsubscribe()

ManuelZ avatar Feb 18 '21 03:02 ManuelZ

I had this issue when using React Hooks.

I was clicking a button to get subscribed and then again the same button to get unsubscribed from a topic.

  • I had my topics declarations inside my component... solution, to move them out.
  • I needed to memoize the function that I was using to switch the subscription on/off and as well memoize the function callback passed to topic.unsubscribe()

@ManuelZ If possible could you show an example?

asvinp avatar Apr 15 '21 05:04 asvinp