Possibility to unsubscribe subscriber or unregister publisher?

Hello,

I’m using ROS-TCP-Connector to establish a connection between ROS and Unity. The ROSConnection class provides functions like

  • Subscribe<T>(string topic, Action<T> callback)

  • SubscribeByMessageName(string topic, string rosMessageName, Action<Message> callback)

  • RegisterPublisher<T>(string rosTopicName, int? queue_size = null, bool? latch = null)

But I see no way to unsubscribe a subscriber, i.e. once you subscribe using a callback, that callback gets called for all eternity, even if you are no longer interested in the topic. In a similar fashion, a publisher will stay registered because there is no mechanism to unregister. Although, in case of the publisher I’m not sure what the implications are.

I haven’t used services, yet, so I cannot say whether the same functionality is missing there, too.

The reason I’m interested in this functionality is because I’m trying to abstract the singleton nature of the ROSConnection class away, by introducing separate ROSPublisher and ROSSubscriber classes:

The idea is that developers in my team instantiate the newly introduced classes ROSPublisher and ROSSubscriber to publish and subscribe to topics. Once they are done, they simply call Dispose() on them. But, unless ROSConnection offers some mechanism to unregister/unsubscribe, there is no real way to dispose the instances.

Hi! There is an Unsubscribe method in the ROSConnection. I was using it in my project to unsubscribe from a topic when I no longer wanted to receive messages from it.

As for the publishers, it is true that it seems there is no such method in the ROSConnection. However, I just found this SendPublisherUnregistration method in some InternalAPI, so maybe it was planned, and it would be possible to use it somehow. I did not take a closer look. When I was implementing this feature in my project, I simply set enabled = false because I was publishing my messages from the Update method. When enabled == fasle, the MonoBehaviour won’t run the Update method.

Oh, I totally forgot to talk about Unsubscribe(string topic). You’re correct, thats a thing. But as far as I can tell, it unsubscribes all callbacks from a given topic. So if I had two ROSSubscriber instances subscribed to the same topic and one of them calls Unsubscribe(string topic) during dispose, the other one would also suddenly stop receiving messages.

I’m looking for a mechanism like Unsubscribe(topic, callback) so that I can unsubscribe a specific callback.

I technically could create another class which acts as an adapter to ROSConnection. The ROSConnectionAdapter could then replicate the API of ROSConnection (while keeping track of individual callbacks when subscribing and providing an additional API to unsubscribe those callbacks again). Put that would be a pain in the butt…

Ah, that is true, it does unsubscribe everything from the topic. In my case, I was only switching between different subscribers, and I only had a single active subscription at a time, so I didn’t find this issue.

Maybe it would be worth raising an issue in the ROS-TCP-Connector repository (as a feature request), but I highly doubt anything would happen anytime soon. So… unfortunately, you might need to implement this yourself. :frowning:

Yeah, I think you are right about raising the issue part. There’s not been much activity in the ROS-TCP-Connector repository for quite some time now. Would be nice to know if someone in Unity is still maintaining it.

I’ll probably end up implementing it myself on top of the API provided by ROSConnection. Thanks for you reply. :slight_smile:

1 Like