Anyone needs push notifications for changes in RavenDB
Tags: ravendb, softwaredevelopment, pushnotifications, javascript
Published: 2022-07-17 17:20:43


## **Anyone needs push notifications for changes in RavenDB ?**

![Push notifications](https://cdn-images-1.medium.com/max/4000/1*Ae58secYhmHNkgjuMMcGhQ.jpeg)

* Intro

* Why we may need push notifications? Real world scenarios with Push notifications

* What we receive from Changes API ?

* Implementation

## Intro

RavenDB is an open-source document-oriented database. It is fully [ACID](https://en.wikipedia.org/wiki/ACID). It is cross-platform, supported on Windows, Linux, and Mac OS. Also, as platform as a service, it is served as [cloud service](https://ravendb.net/) as well.

RavenDB stores data as JSON documents and can be deployed in distributed clusters with master-master replication.

RavenDB comes with it is own API. Its’ [Changes](https://ravendb.net/docs/article-page/5.3/csharp/client-api/changes/what-is-changes-api) API used as a Push Notifications service, that allows a RavenDB Client to receive messages from a RavenDB Server regarding events that occurred on the server.

With this API, A client can subscribe to entity events related to:

* Documents

* Indexes

* Operations

* Counters

* Time series

## **Why we may need push notifications?**

We push notifications to message our users when they might need a reminder about something. People find value in receiving push notifications that alert them of updates or changes to their upcoming travel plans, reservations, deliveries, and other time-sensitive topics.

![An example notification wireframe](https://cdn-images-1.medium.com/max/2096/1*fJXOqXUPOs8n-N9jf7x2sA.png)

A basic broadcast notification flow could be:

![Basic broadcast message flow](https://cdn-images-1.medium.com/max/3004/1*rbEcsHqjPVuVsLdLDqzRZQ.png)

Let’s assume that this categories data is stored in RavenDB and RavenDB provides push notification service to retrieve changes on specific document stores.

## **What we receive from Changes API as notification?**

Very basic information arrives as notification from push service. Please see below.

![Push notification data](https://cdn-images-1.medium.com/max/2000/1*krBU-o2GiUPa6vrlKz7ZVQ.png)

* Type: Type of action, PUT is update. Please see explanations [here](https://ravendb.net/docs/article-page/5.3/csharp/client-api/rest-api/document-commands).

* Id: Unique Id of document as Guid.

* CollectionName: Specified collection while initialising [changes api](https://gist.github.com/alisuleymantopuz/b1a5759bea058f0c47ef0db7f1aac07c).

* ChangeVector: Change vectors is the RavenDB implementation of the Vector clock concept. They give us partial order over modifications of documents in a RavenDB cluster.

**Implementation**

RavenDB API can be implemented in C#, Phyton, Node.JS and Java. Here a snippet to see how it connects in **Node.JS**:
{% gist https://gist.github.com/alisuleymantopuz/b1a5759bea058f0c47ef0db7f1aac07c.js %}
## **Conclusion**

You can implement changes api for the list of actions below. See more in [documentation](https://ravendb.net/docs/article-page/5.3/csharp/client-api/changes/what-is-changes-api):

For Document Changes:

* ForAllDocuments: Track changes for all document

* ForDocument: Track changes for a given document (by Doc ID)

* ForDocumentsInCollection: Track changes for all documents in a given collection

* ForDocumentsStartingWith: Track changes for documents whose ID contains a given prefix

For Index Changes:

* ForAllIndexes: Track changes for all indexes

* ForIndex: Track changes for a given index (by Index Name)

For Operation Changes:

* ForAllOperations: Track changes for all operation

* ForOperationId: Track changes for a given operation (by Operation ID)

For Counter Changes:

* ForAllCounters: Track changes for all counters

* ForCounter: Track changes for a given counter (by Counter Name)

* ForCounterOfDocument: Track changes for a specific counter of a chosen document (by Doc ID and Counter Name)

* ForCountersOfDocument: Track changes for all counters of a chosen document (by Doc ID)

For Time Series Changes:

* ForAllTimeSeries: Track changes for all time series

* ForTimeSeries: Track changes for all time series with a given name

* ForTimeSeriesOfDocument: Track changes for: a specific time series of a given document (by Doc ID and Time Series Name) and any time series of a given document (by Doc ID)

Cheers.