Changes API: How to subscribe to document changes?

Following methods allow you to subscribe to document changes:

ForDocument

Single document changes can be observed using ForDocument method.

Syntax

IObservableWithTask<DocumentChangeNotification> ForDocument(string docId);
Parameters
docId string Id of a document for which notifications will be processed.
Return Value
IObservableWithTask<DocumentChangeNotification> Observable that allows to add subscriptions to notifications for given document.

Example

IDisposable subscription = store
	.Changes()
	.ForDocument("employees/1")
	.Subscribe(
		change =>
		{
			switch (change.Type)
			{
				case DocumentChangeTypes.Put:
					// do something
					break;
				case DocumentChangeTypes.Delete:
					// do something
					break;
			}
		});

ForDocumentsInCollection

To observe all document changes in particular collection use ForDocumentInCollection method. This method filters documents by Raven-Entity-Name metadata property value.

Syntax

IObservableWithTask<DocumentChangeNotification>
	ForDocumentsInCollection(string collectionName);

IObservableWithTask<DocumentChangeNotification>
	ForDocumentsInCollection<TEntity>();
Parameters
collectionName string Name of document collection for which notifications will be processed.
Return Value
IObservableWithTask<DocumentChangeNotification> Observable that allows to add subscriptions to notifications for given document collection name.

Information

Overload with TEntity type uses Conventions.GetTypeTagName to get collection name.

Example

IDisposable subscription = store
	.Changes()
	.ForDocumentsInCollection<Employee>()
	.Subscribe(change => Console.WriteLine("{0} on document {1}", change.Type, change.Id));

or

string collectionName = store.Conventions.GetTypeTagName(typeof(Employee));
IDisposable subscription = store
	.Changes()
	.ForDocumentsInCollection(collectionName)
	.Subscribe(change => Console.WriteLine("{0} on document {1}", change.Type, change.Id));

ForDocumentsOfType

To observe all document changes for given type use ForDocumentsOfType method. This method filters documents by Raven-Clr-Type metadata property value.

Syntax

IObservableWithTask<DocumentChangeNotification>
	ForDocumentsOfType(string typeName);

IObservableWithTask<DocumentChangeNotification>
	ForDocumentsOfType(Type type);

IObservableWithTask<DocumentChangeNotification>
	ForDocumentsOfType<TEntity>();
Parameters
typeName or type string or Type Name of type or type for which notifications will be processed. If default conventions are used, the full type name without version information should be passed. See Raven.Client.Document.ReflectionUtil.GetFullNameWithoutVersionInformation.
Return Value
IObservableWithTask<DocumentChangeNotification> Observable that allows to add subscriptions to notifications for given document type name.

Information

Overloads with TEntity type or Type uses Conventions.FindClrTypeName to get type name.

Example

IDisposable subscription = store
	.Changes()
	.ForDocumentsOfType<Employee>()
	.Subscribe(change => Console.WriteLine("{0} on document {1}", change.Type, change.Id));

or

string typeName = store.Conventions.FindClrTypeName(typeof(Employee));
IDisposable subscription = store
	.Changes()
	.ForDocumentsOfType(typeName)
	.Subscribe(change => Console.WriteLine("{0} on document {1}", change.Type, change.Id));

ForDocumentsStartingWith

To observe all document changes for documents with Id that contains given prefix use ForDocumentsStartingWith method.

Syntax

IObservableWithTask<DocumentChangeNotification>
	ForDocumentsStartingWith(string docIdPrefix);
Parameters
docIdPrefix string Document Id prefix for which notifications will be processed.
Return Value
IObservableWithTask<DocumentChangeNotification> Observable that allows to add subscriptions to notifications for given document Id prefix.

Example

IDisposable subscription = store
	.Changes()
	.ForDocumentsStartingWith("employees/1") // employees/1, employees/10, employees/11, etc.
	.Subscribe(change => Console.WriteLine("{0} on document {1}", change.Type, change.Id));

ForAllDocuments

To observe all document changes use ForAllDocuments method.

Syntax

IObservableWithTask<DocumentChangeNotification>
	ForAllDocuments();
Return Value
IObservableWithTask<DocumentChangeNotification> Observable that allows to add subscriptions to notifications for all documents.

Example

IDisposable subscription = store
	.Changes()
	.ForAllDocuments() // employees/1, orders/1, customers/1, etc.
	.Subscribe(change => Console.WriteLine("{0} on document {1}", change.Type, change.Id));

Remarks

Information

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