Language
C# Java
Article For
4.0 5.1 5.3

Changes API

The RavenDB client offers a push notification feature that allows you to receive messages from a server about events that occurred there. You are able to subscribe to events for all documents, indexes, and operations as well as to indicate a particular one that you are interested in. This mechanism lets you notify users if something has changed without the need to do any expensive polling.

Changes API on Secured Server

Changes API uses WebSockets under the covers. Due to the lack of support for client certificates in WebSockets implementation in .NET Core 2.0 the Changes API won't work for secured servers accessible over HTTPS.

This issue is fixed in the final version of .NET Core 2.1 available here. In order to workaround this you can switch your application to use .NET Core 2.1.

The issue affects only the RavenDB client.

Accessing Changes API

The changes subscription is accessible by a document store through its IDatabaseChanges interface.

store.changes([database]);
Parameters
database string Name of database to open changes API for. If null, the database configured in DocumentStore will be used.
Return value
IDatabaseChanges object Instance implementing IDatabaseChanges interface.

Connection interface

Changes object interface extends IConnectableChanges interface that represents the connection. It exposes the following properties, methods and events.

Properties and methods
connected boolean Indicates whether it's connected or not
on("connectionStatus") method Adds a listener for 'connectionStatus' event
on("error") method Adds a listener for 'error' event
ensureConnectedNow() method Returns a Promise resolved once connection to the server is established.

Subscriptions

In order to retrieve notifications you have to subscribe to server-side events by using one of the following methods:

Unsubscribing

In order to end subscription (stop listening for particular notifications) you must dispose() it.

const changes = store.changes();

await changes.ensureConnectedNow();

const allDocsChanges = changes.forAllDocuments()
    .on("data", change => {
        console.log(change.type + " on document " + change.id);
    })
    .on("error", err => {
        // handle error
    });

// ...

try {
    // application code here
} finally {
    // dispose changes after use
    if (changes != null) {
        changes.dispose();
    }
}

Remarks

Note

One or more open Changes API connections will prevent a database from becoming idle and unloaded, regardless of configuration value for database idle timeout

Information

To get more method overloads, especially the ones supporting delegates, please add Reactive Extensions Core package to your project.