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.

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", callback) method Adds a listener for 'connectionStatus' event
on("error", callback) 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