Skip to content

Add support for removing a single subscription handler #592

@iche033

Description

@iche033

Desired behavior

Currently there is one bool Node::Unsusbcribe(const std::string &_topic) API which removes all subscription handlers for that topic in the Node. The feature request is to have a variant of the Unsubscribe API that supports removing just one specific subscription handler from the node.

As an example, the current usage of Subscribe / Unsubscribe and their behavior is as shown below:

// create one subscription
bool success1 = node.Subscribe(myTopic, callback1, opts);
// create another subscription
bool success2 = node.Subscribe(myTopic, callback2, opts);
// unsubscribes both callback1 and callback2
node.Unsubscribe(topic);

Ideally we can have something like:

// create one subscription
Subscriber sub1 = node.Subscribe(myTopic, callback1, opts);
// create another subscription
Subscriber sub2 = node.Subscribe(myTopic, callback2, opts);
// unsubscribes just sub1 
node.Unsubscribe(sub1);
// alternative API - unsubscribes just sub2 
sub2.Unsubscribe();

The new APIs above is just for illustrative purposes. It is however a breaking change since it changes the return type from bool to a custom struct / class.

To minimize breaking changes, another option would be to add overloaded Subscribe(myTopic, callback1, opts, sub1) API in which you can pass a reference for it to be set by the function.

Alternatives considered

See above for alternative API suggestions

Implementation suggestion

For Unsubscribing a single subscription from node, we'll need a function similar to Node::Unsubscribe. The difference being:

  • Support removing handlers by a unique id
    if (!this->dataPtr->RemoveHandlersFromPubQueue(topic))
  • Only notify publishers that the node is no longer interested in receiving published messages if no more handlers exists:
    // Notify to the publishers that I am no longer interested in the topic.

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Type

    No type

    Projects

    Status

    Inbox

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions