Subscribing to topics
Consider the following information when deciding how clients subscribe to topics.
- The client must subscribe using a selector that matches the topic.
- The topic must exist on the Diffusion™ server .
- The client must register at least one stream that matches the topic.
When all these conditions are met, the stream receives a subscription notification and an initial value for the topic. The client receives subsequent updates to the topic through the stream.
The order in which these conditions are met does not affect the receipt of the subscription notification and the initial value of the topic.
Permissions
To subscribe to a topic, a client must have the Permissions.
permission and the permission for that topic. For more information, seeThe rest of this section assumes that the client has the required permissions to complete the described actions.
Subscription flow
- The prerequisite conditions for subscribing to a topic are met:
- The client selects a set of topics to subscribe to, or that
selection is made on the client's behalf by another client.
A client can select multiple topics using a topic selector. This subscription can be to topics that match a particular regular expression or to topics in a particular branch of the topic tree. For more information, see Topic selectors.
The selection made by the subscribe request is persistent and is stored on the Diffusion server . Because selections are stored, the client can subscribe, pre-emptively, to topics that do not currently exist.
- The topic exists on
the
Diffusion
server
.
The topic can be created before or after any subscribe request that selects it. In both cases, the client that makes the request is subscribed to the topic when both the selection and the topic exist.
- The client selects a set of topics to subscribe to, or that
selection is made on the client's behalf by another client.
- Both prerequisite conditions for a subscription are met and the client is
subscribed to the topic.
The intersection of the topic paths that the client has selected for subscription and the topics that exist on the Diffusion server defines the list of subscriptions that client has made.
For each client that connects to the Diffusion server , the Diffusion server stores a separate list of subscriptions.
- When the subscription is made, the value of the subscribed topic is sent to
the client.
Subsequent updates are sent as values or as deltas, depending on the topic specification and the nature of the update.
Client handling of data from subscribed topics
- When the client makes a subscription to a topic, the value of the topic is
sent to the client.
Subsequent updates are sent as values or as deltas.
- Values for each subscribed topic are stored in the subscribed topic cache on
the client.
This is not the case for stateless or record topics, see Considerations when subscribing.
- The client registers streams against a topic selector. A list of streams is maintained in the stream registry.
- If one or more streams exists that matches a topic, the value for that topic is received through the matching streams.
Considerations when subscribing
The subscriptions a client has, which are defined by the intersection of the topics that exist on the Diffusion server with the selections made by the client, determine what data is sent to the client from the Diffusion server . To reduce the amount of data sent between the Diffusion server and the client, only subscribe to those topics that the client uses.
The streams a client registers determine what topic values are available for the client to work with. Adding and removing streams as they are needed by the client enables your application to access topic values stored in the subscribed topic cache in real time.
When a client subscribes to a stateless topic, the values received are not stored in the subscribed topic cache. If a stream is created after the topic is subscribed, the stream does not receive a value until the next time the topic is updated.