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.
IDatabaseChanges Changes(string database = null);
Parameters | ||
---|---|---|
database | string | Name of database to open changes API for. If null , the Database configured in DocumentStore will be used. |
Return value | |
---|---|
IDatabaseChanges | Instance implementing IDatabaseChanges interface. |
Connection interface
IDatabaseChanges
inherits from IConnectableChanges<TChanges>
interface that represent the connection.
public interface IConnectableChanges<TChanges> : IDisposable
where TChanges : IDatabaseChanges
{
// returns state of the connection
bool Connected { get; }
// A task that ensures that the connection to the server was established.
Task<TChanges> EnsureConnectedNow();
//An event handler to detect changed to the connection status
event EventHandler ConnectionStatusChanged;
//An action to take if an error occured in the connection to the server
event Action<Exception> OnError;
}
Subscriptions
In order to retrieve notifications you have to subscribe to server-side events by using one of the following methods:
- ForAllDocuments
- ForAllCounters
- ForAllIndexes
- ForAllOperations
- ForDocument
- ForDocumentsInCollection
- ForDocumentsStartingWith
- ForCounter
- ForCounterOfDocument
- ForCountersOfDocument
- ForIndex
- ForOperationId
Unsubscribing
In order to end subscription (stop listening for particular notifications) you must Dispose
it.
IDatabaseChanges subscription = store.Changes();
await subscription.EnsureConnectedNow();
subscription.ForAllDocuments().Subscribe(change => Console.WriteLine("{0} on document {1}", change.Type, change.Id));
try
{
// application code here
}
finally
{
if (subscription != null)
subscription.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.