DEV Community

Ali S. Topuz
Ali S. Topuz

Posted on

2 2

Anyone needs push notifications for changes in RavenDB

Anyone needs push notifications for changes in RavenDB ?

Push notifications

  • 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. It is cross-platform, supported on Windows, Linux, and Mac OS. Also, as platform as a service, it is served as cloud service 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 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

A basic broadcast notification flow could be:

Basic broadcast message flow

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

  • Type: Type of action, PUT is update. Please see explanations here.

  • Id: Unique Id of document as Guid.

  • CollectionName: Specified collection while initialising changes api.

  • 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:

// Push Notifications for tracking changes on RavenDB entities Explained
// The Changes API is a Push Notifications service,
// that allows a RavenDB Client to receive messages from a RavenDB Server regarding events that occurred on the server.
// Being able to reach your visitors without any kind of effort
// server -> request -> push service -> message arrives
// capabilities of push notifications
// Web Push Notifications are used for simple tasks like alerting a user about upcoming sales or events.
// They display an icon and a few lines of text that the user can then click to open and
// go to a specific destination on your site
import { DocumentStore } from "./node_modules/ravendb/dist/Documents/DocumentStore.js"
import * as fs from "fs";
// load certificate and prepare authentication options
const authOptions = {
certificate: fs.readFileSync("x.pfx"),
type: "pfx",
password: "<pass>"
};
const store = new DocumentStore(['<url>'], 'northwind', authOptions);
store.initialize();
console.info('initialized.');
const changes = store.changes();
await changes.ensureConnectedNow();
const session = store.openSession();
const allDocsChanges = changes.forAllDocuments()
.on("data", async change => {
const product = await session.load(change.id, 'Products');
const response = {
latestVersion: product,
change
};
console.log(response);
})
.on("error", err => {
// handle error
});
try {
// application code here
} finally {
// dispose changes after use
}

Conclusion

You can implement changes api for the list of actions below. See more in documentation:

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.

Top comments (0)