Just a second...

Topic Selection Scopes

Topic selection scopes allow an application with multiple components to isolate subscription and unsubscription requests.

In some cases an application may have multiple components which subscribe and unsubscribe to topics using the same session.

An application component can use a topic selection scope to manage a set of selectors that is unaffected by unsubscriptions performed by other application components. The session will be subscribed to all topics with paths matching a selector in any scope. The unsubscribe operation removes a selector from specific scopes.

A scope may be specified to a subscribe or unsubscribe method, indicating that the selection only applies to that scope. The server manages scopes to ensure that unsubscriptions applied to one scope do not affect another.

Scope names are case sensitive. A scope name may not begin with the character $ as this is reserved for internal use.

Unsubscription using a wildcard selector that indicates all topics (such as ?.*//) effectively removes the scope.

An application can request unsubscription from all scopes using unsubscribeAllScopes.

The DEFAULT_SELECTION_SCOPE ("") is used for all methods that do not explicitly specify a scope. This maintains backwards compatibility but it is not advised to combine the use of named scopes with the default scope.

For example, if our application has a single session which is used by multiple components:

final Session session = Diffusion.sessions().open(serverUrl);
final Topics topics = session.feature(Topics.class);

In component A we subscribe to a topic with a specified scope:

topics.subscribe("my/topic/path", "scopeA");

Elsewhere in the application component B subscribes to the same topic with its own scope:

topics.subscribe("my/topic/path", "scopeB");

Component A no longer needs the subscription and unsubscribes:

topics.unsubscribe("my/topic/path", "scopeA");

In this scenario component B is still subscribed. When component B unsubscribes (or unsubscribeAllScopes is called) the session is unsubscribed.

topics.unsubscribe("my/topic/path", "scopeB");

It should be noted that value streams are managed independently of subscriptions. Unsubscribing with a scope will not prevent a component from receiving value updates from a registered stream. As long as any scope in the application has a subscription to a topic, the session will receive updates for that topic and streams that select that topic will receive those updates. To stop updates for a selection the stream should be removed. For more information see Using streams for subscription.

Subscription Control

The SubscriptionControl feature can be used to manage subscriptions on behalf of clients and as such can manage the selection scopes that an application may be using. It also allows control clients to get a representation of the current active scopes. For example:

Subscribe a client to a topic with a scope, subscribe clients to a topic with a scope using a session filter, and get all active scopes for a session:

JavaScript
// Subscribe a client to a topic with a scope
await session.clients.subscribe(otherSessionId, 'my/topic/path', 'scopeA');

// Subscribe clients to a topic with a scope using a session filter
await session.clients.subscribe("$Principal is 'client'", 'my/topic/path', 'scopeA');

// Get all active scopes for a session
await session.clients.getTopicSelections(otherSessionId);
.NET
// Subscribe a client to a topic with a scope
await subscriptionControl.SubscribeAsync(otherSessionId, "my/topic/path", "scopeA");

// Subscribe clients to a topic with a scope using a session filter
await subscriptionControl.SubscribeByFilterAsync("$Principal is 'client'", "my/topic/path", "scopeA");

// Get all active scopes for a session
await subscriptionControl.GetTopicSelectionsAsync(otherSessionId);
Java and Android
// Subscribe a client to a topic with a scope
subscriptionControl.subscribe(otherSessionId, "my/topic/path", "scopeA");

// Subscribe clients to a topic with a scope using a session filter
subscriptionControl.subscribeByFilter("$Principal is 'client'", "my/topic/path", "scopeA");

// Get all active scopes for a session
subscriptionControl.getTopicSelections(otherSessionId);
C
void subscription_control_scopes(
    SESSION_T *session,
    SESSION_ID_T *other_session_id)
{
    char *topic_path = "my/topic/path";

    // Subscribe a client to a topic with a scope
    SUBSCRIPTION_CONTROL_PARAMS_T params_subscribe = {
        .session_id = *other_session_id,
        .topic_selector = topic_path
    };
    diffusion_subscribe_client_with_scope(session, "scopeA", params_subscribe, NULL);

    // Subscribe clients to a topic with a scope using a session filter
    DIFFUSION_SUBSCRIBE_BY_FILTER_PARAMS_T params_subscribe_by_filter = {
        .filter = "$Principal is 'client'",
        .topic_selector = topic_path
    };
    diffusion_subscribe_client_by_filter_with_scope(session, "scopeA", params_subscribe_by_filter, NULL);

    // Get all active scopes for a session
    DIFFUSION_GET_TOPIC_SELECTIONS_PARAMS_T params_get_topic_selections = {
        .session_id = other_session_id,
        .on_complete = on_get_topic_selections
    };
    diffusion_get_topic_selections(session, params_get_topic_selections, NULL);
}
Apple
let subscriptionControl = session.subscriptionControl

// Subscribe a client to a topic with a scope
_ = subscriptionControl.subscribe(otherSessionId, to: "my/topic/path", withSelectionScope: "scopeA") { (error) in }

// Subscribe clients to a topic with a scope using a session filter
_ = subscriptionControl.subscribe(withFilter: "$Principal is 'client'", to: "my/topic/path", withSelectionScope: "scopeA") { (count, error) in }

// Get all active scopes for a session
_ = subscriptionControl.getTopicSelections(otherSessionId) { (result, error) in }

When managing selection scopes in this way there is a need for collaborative design of the application to ensure consistent use of scopes across clients and control clients.

Example

The following example demonstrates topic selection scopes across multiple application components, including using unsubscribeAllScopes to unsubscribe a session from a topic across all scopes.

JavaScript
const topicPath = 'my/topic/path';
const myOtherPath = 'my/other/path';

// Component A subscribes to a topic with its own scope
await session.selectWithScope(topicPath, 'scopeA');

// Component B subscribes to both topics with a different scope
await session.selectWithScope(topicPath, 'scopeB');
await session.selectWithScope(myOtherPath, 'scopeB');

// Component A no longer needs the subscription; scopeB is unaffected
await session.unsubscribeWithScope(topicPath, 'scopeA');

// A control client unsubscribes this session from all remaining scopes for
// myOtherPath, which is now unsubscribed for the session
await session.clients.unsubscribeAllScopes(session.sessionId, myOtherPath);
.NET
var topicPath = "my/topic/path";
var myOtherPath = "my/other/path";

// Component A subscribes to a topic with its own scope
await topics.SubscribeAsync(topicPath, "scopeA");

// Component B subscribes to both topics with a different scope
await topics.SubscribeAsync(topicPath, "scopeB");
await topics.SubscribeAsync(myOtherPath, "scopeB");

// Component A no longer needs the subscription; scopeB is unaffected
await topics.UnsubscribeAsync(topicPath, "scopeA");

// A control client unsubscribes this session from all remaining scopes for
// myOtherPath, which is now unsubscribed for the session
await subscriptionControl.UnsubscribeAllScopesAsync(session.SessionId, myOtherPath);
Java and Android
final Session session = Diffusion.sessions().open("ws://localhost:8080");
final Topics topics = session.feature(Topics.class);
final SubscriptionControl subscriptionControl = session.feature(SubscriptionControl.class);

final String topicPath = "my/topic/path";
final String myOtherPath = "my/other/path";

// Component A subscribes to a topic with its own scope
topics.subscribe(topicPath, "scopeA").join();

// Component B subscribes to both topics with a different scope
topics.subscribe(topicPath, "scopeB").join();
topics.subscribe(myOtherPath, "scopeB").join();

// Component A no longer needs the subscription; scopeB is unaffected
topics.unsubscribe(topicPath, "scopeA").join();

// A control client unsubscribes this session from all remaining scopes for
// myOtherPath, which is now unsubscribed for the session
subscriptionControl.unsubscribeAllScopes(session.getSessionId(), myOtherPath).join();
C
static int on_subscribe(
    SESSION_T *session,
    void *context_data)
{
    // subscription request received and accepted
    return HANDLER_SUCCESS;
}

static int on_unsubscribe(
    SESSION_T *session,
    void *context_data)
{
    // unsubscription request received and accepted
    return HANDLER_SUCCESS;
}

static int on_error(
    SESSION_T *session,
    const DIFFUSION_ERROR_T *error)
{
    printf("On error: %s\n", error->message);
    return HANDLER_SUCCESS;
}

void subscribe_using_selection_scopes(
    SESSION_T *session)
{
    char *topic_path = "my/topic/path";
    char *my_other_path = "my/other/path";

    // Component A subscribes to a topic with its own scope
    SUBSCRIPTION_PARAMS_T params_subscribe_a = {
        .topic_selector = topic_path,
        .on_subscribe = on_subscribe
    };
    diffusion_subscribe_with_scope(session, "scopeA", params_subscribe_a, NULL);

    // Component B subscribes to both topics with a different scope
    SUBSCRIPTION_PARAMS_T params_subscribe_b1 = {
        .topic_selector = topic_path,
        .on_subscribe = on_subscribe
    };
    diffusion_subscribe_with_scope(session, "scopeB", params_subscribe_b1, NULL);

    SUBSCRIPTION_PARAMS_T params_subscribe_b2 = {
        .topic_selector = my_other_path,
        .on_subscribe = on_subscribe
    };
    diffusion_subscribe_with_scope(session, "scopeB", params_subscribe_b2, NULL);

    // Component A no longer needs the subscription; scopeB is unaffected
    UNSUBSCRIPTION_PARAMS_T params_unsubscribe = {
        .topic_selector = topic_path,
        .on_unsubscribe = on_unsubscribe,
        .on_error = on_error
    };
    diffusion_unsubscribe_with_scope(session, "scopeA", params_unsubscribe, NULL);

    // The session unsubscribes from my_other_path across all remaining scopes,
    // which is now unsubscribed for the session
    UNSUBSCRIPTION_ALL_SCOPES_PARAMS_T params_unsubscribe_all_scopes = {
        .topic_selector = my_other_path,
        .on_unsubscribe = on_unsubscribe,
        .on_error = on_error
    };
    diffusion_unsubscribe_all_scopes(session, params_unsubscribe_all_scopes, NULL);
}
Apple
let topics = session.topics

let topicPath = "my/topic/path"
let myOtherPath = "my/other/path"

// Component A subscribes to a topic with its own scope
topics.subscribe(topicPath, withSelectionScope: "scopeA") { (error) in }

// Component B subscribes to both topics with a different scope
topics.subscribe(topicPath, withSelectionScope: "scopeB") { (error) in }
topics.subscribe(myOtherPath, withSelectionScope: "scopeB") { (error) in }

// Component A no longer needs the subscription; scopeB is unaffected
topics.unsubscribe(topicPath, withSelectionScope: "scopeA") { (error) in }

// The session unsubscribes from myOtherPath across all remaining scopes,
// which is now unsubscribed for the session
topics.unsubscribeAll(myOtherPath) { (error) in }