I have subscribed to a tree of topics and am using that to update values in an HTML table . Sometimes new child topics are added and old child topics are removed. I need to keep my UI in sync, new topics are detected when values arrive for an unknown topic, but I cannot discover when a subscribed topic has been removed.
How can I detect when a topic has been removed?
If you are subscribing to a topic tree using a topic selector such as ?myTopicTreeRoot//
, then you when you place this subscription call, you can also state what would happen in case of a unsubscription, as well as subscription, e.g:
session.subscribe("?myTopicTreeRoot//").on({
open: function(subscription) {
console.log('Opened subscription for: ' + subscription.selector);
},
update : function(update, topic) {
console.log('Update for ' + topic + ' : ' + update);
},
subscribe : function(details, topic) {
//This will notify you of every new subscription to a topic in your subtree
console.log('Subscribed to : ' + topic);
},
unsubscribe : function(reason, topic) {
//This will notify you of every unsubscription from a topic in your subtree
console.log('Unsubscribed from : ' + topic);
}
});
Property functions subscribe
and unsubscribe
are called for topic myTopicTreeRoot
and any descendant, e.g. myTopicTreeRoot/foo/bar/baz
.
Importantly: Any subscription is ended when the topic is removed, which in turn calls unsubscribe
.
Note that it is better to use the subscribe property function. Using the first call to update
to imply existence of a new topic is unwise if subscribing to stateless topics.