Events and State subscription

Introduction

Events and state subscriptions are based on a WebSocket API where applications can subscribe to specific events and state.

What it is used for

An events and state session allows an application to receive events from an XProtect system on a persistent connection. The application can granularly specify which events are of interest and use stateful events to keep track state.

The same session can also be used to retrieve state on-demand.

If the connection is interrupted, events will be cached on the server and can be retrieved if the client reconnects within a timeout (default: 30 seconds).

Classes

Class Description
VideoOS.Platform.EventsAndState.EventsAndStateSession Static class containing a Create method for creating an IEventsAndStateSession with a specified IEventReceiver.
VideoOS.Platform.EventsAndState.IEventsAndStateSession Interface used for subscribing to events and getting state.
VideoOS.Platform.EventsAndState.IEventReceiver Interface implemented by user code for receiving events and connection state.
VideoOS.Platform.EventsAndState.SubscriptionRule Subscription rules specifying which events should be received.

Functionality

Event receiver

Implement the IEventReceiver interface to be able to receive events and state.

This interfaces will deliver events on the OnEventsReceivedAsync method.

The interface also delivers the state of the underlying connection to the XProtect system on the OnConnectionStateChangedAsync method.

Creating a session with an even receiver

The static class EventsAndStateSession has a factory method to create a session for receiving events and state. Calling the create method and passing the login settings (see Component environments for login) and an event receiver, will create a session defined by the interface IEventsAndStateSession.

The IEventsAndStateSession interface is where an application can add/remove subscriptions and get the current state.

Defining subscription rules for a session

Subscription rules specify which events should be received in a session.

When defining a SubscriptionRule, it is possible to specify either specific types/ids that should be matched or indicate that all types/ids should be matched.

Available event types, states and event source(s) can be looked up using the RESTful Configuration API

Subscription rules can be added to an IEventsAndStateSession using the AddSubscriptionAsync method, which returns a subscription id. The subscription id can be used to remove the subscription again.

Subscription properties

Property Description
Modifier Determines if the specified events are excluded or included. Available values are Include and Exclude.
ResourceTypes Specifies which resource type(s) that are covered by this rule. E.g., “recordingServers”, “hardware”, “cameras”. It is also possible to use one of the GUIDs defined in the static class Kind.
SourceIds Specifies specific source(s) that are covered by this rule.
EventTypes Specifies specific event type(s) that are covered by this rule. All event types currently defined in an XProtect system can be retrieved using the RESTful Configuration API using the resource path /eventTypes. Alternatively, pre-defined event types can be found in the static class KnownStatusEvents.

NOTE:
An event must match all specifiers to be covered by a rule.

Relation between a session, subscription rules and modifiers

Remove a subscription

Remove a previously created subscription specified by the subscription id.

Getting events

Events are received in the OnEventsReceivedAsync method of the IEventReceiver implementation.

Getting state

An application can get the current state satisfying the subscription by calling GetCurrentStateAsync. Alternatively, the application can request all stateful events by calling SendAllStatefulEventsAsync. Stateful events will then be received in the OnEventsReceivedAsync method of the IEventReceiver implementation.

This can be useful to retrieve the initial state after one or more subscriptions have been made.

Connection state

Changes in the state of the underlying connection are received in the OnConnectionStateChangedAsync method of the IEventReceiver implementation. See VideoOS.Platform.EventsAndState.ConnectionState for the possible connection states and their meanings.

If disconnected, the connection will automatically be reestablished and the session resumed.

If the session is successfully resumed, then all missed events will be retrieved. If the session cannot be resumed (e.g. if the 30 second timeout has expired), a new session will be created with the previously defined subscriptions. If SendStatefulEventsOnReconnect is set on the IEventsAndStateSession (default is true), then the current state will automatically be be received as stateful events in OnEventsReceivedAsync.

Sample code

async Task SampleAsync()
{
    var loginSettings = LoginSettingsCache.GetLoginSettings(EnvironmentManager.Instance.MasterSite);

    // Create event receiver and session
    var eventReceiver = new EventReceiver();
    var session = EventsAndStateSession.Create(loginSettings, eventReceiver);

    // Add subscription
    var subscription = new[]
        {
            new SubscriptionRule(
                Modifier.Include,
                new ResourceTypes(new[]{ "cameras" }),
                SourceIds.Any,
                new EventTypes(new[]{ KnownStatusEvents.CommunicationStarted, KnownStatusEvents.CommunicationStopped }))
        };
    await session.AddSubscriptionAsync(subscription, default);

    // Send initial state as events, if needed
    await _session.SendAllStatefulEventsAsync(default);

    // Run application / Wait for shutdown
    // Call _session.Dispose() to stop receiving events and close the underlying connection
}

class EventReceiver : IEventReceiver
{
    public async Task OnConnectionStateChangedAsync(ConnectionState newState)
    {
        // Handle connection state change
    }

    public async Task OnEventsReceivedAsync(IEnumerable<Event> events)
    {
        // Handle events
    }
}

Sample

See also