Subscribing to topics
Subscribe to topics with topic selectors. When topics exist that match the selections made by the client, data from those topics is sent to the client from Diffusion™ Cloud .
The client must register a stream to access topic data that has been sent from Diffusion Cloud . For more information, see Subscribing to topics.
Required permissions:
and permissions for the specified topicsSubscribing to topics
A client can subscribe to a topic to receive updates that are published to the topic. If the topic has state, when the client subscribes to that topic Diffusion Cloud sends the topic state as a full value. Subsequent updates to the data on the topic are sent as deltas or as full values depending on the type of the topic and the structure of its data.
session.select('topic_selector');
// Subscribe to 'random/string' topic await topics.SubscribeAsync(topic);
final Session session = Diffusion.sessions().open("ws://localhost:8080"); final Topics topics = session.feature(Topics.class); topics.subscribe("my/topic/path");
static int on_subscribe( SESSION_T *session, void *context_data) { // subscription request received and accepted return HANDLER_SUCCESS; } static int on_subscribe_error( SESSION_T *session, const DIFFUSION_ERROR_T *error) { // an error has occurred during subscription return HANDLER_SUCCESS; } void subscribe_to_topic_path( SESSION_T *session, char *topic_path) { SUBSCRIPTION_PARAMS_T params = {.topic_selector = topic_path, .on_subscribe = on_subscribe, .on_error = on_subscribe_error}; subscribe(session, params); }
await session.topics.subscribe(topic_selector)
let topic_selector_expression = ">a/b//" session.topics.subscribe(withTopicSelectorExpression: topic_selector_expression) { (error) in if (error == nil) { print("Subscribe request succeeded") } else { print("An error occurred while attempting to subscribe: %@", error!.localizedDescription) } }
A client can subscribe to multiple topics in a single request by using topic selectors. Topic selectors enable you to select whole branches of the topic tree or use regular expressions to select topics based on the names in the topic path.
For more information, see Topic selectors.
Unsubscribing from topics
To stop receiving updates from a topic or set of topics, unsubscribe from the topic or topics:
session.unsubscribe('topic_selector');
// Unsubscribe from 'random/string' topic await topics.UnsubscribeAsync(topic);
final Session session = Diffusion.sessions().open("ws://localhost:8080"); final Topics topics = session.feature(Topics.class); topics.unsubscribe("my/topic/path");
static int on_unsubscribe( SESSION_T *session, void *context_data) { // unsubscription request received and accepted return HANDLER_SUCCESS; } static int on_unsubscribe_error( SESSION_T *session, const DIFFUSION_ERROR_T *error) { // an error has occurred during unsubscription return HANDLER_SUCCESS; } void unsubscribe_from_topic_path( SESSION_T *session, char *topic_path) { UNSUBSCRIPTION_PARAMS_T params = {.topic_selector = topic_path, .on_unsubscribe = on_unsubscribe, .on_error = on_unsubscribe_error}; unsubscribe(session, params); }
await session.topics.unsubscribe(topic_selector)
let topic_selector_expression = ">*//" session.topics.unsubscribe(fromTopicSelectorExpression: topic_selector_expression) { (error) in if (error == nil) { print("Unsubscribe request succeeded") } else { print("An error occurred while attempting to unsubscribe: %@", error!.localizedDescription) } }