The ArangoDB team gave me an opportunity to write a tutorial about arangochair. Arangochair is the first attempt to listen for changes in the database and execute actions like pushing a document to the client or execute an AQL query. Currently it is limited to single nodes.

This tutorial is loosely based on the example at baslr/arangochair-serversendevents-demo

arangochair is a Node.js module hosted on npm which make it fairly easy to install. Just run
npm install arangochair and its installed.

New to multi-model and graphs? Check out our free ArangoDB Graph Course.

Now we can write our first lines of code

We set up arangochair to listen for changes on the collection tweets and construct a server send event (SSE) message and sent it to all connected sockets. The SSE consists of two lines per message. The first line is the event and the second line is a stringified linke of JSON.

On the client side we use the EventSource interface to listen for events that we send on the server.

First we construct a new EventSource and add two EventListeners to listen for insert/update and delete. Separate events for insert and update are currently not possible but will be part of a future update.

Handle socket connections on the server with express:

In this example we use express as our framework to handle api calls. We write a middleware that handles the socket of a client to receive SSEs. If the client connection ends we remove the socket from the array of stored sockets.

Why not WebSockets?

Since we want only push data to the client we do not need a duplex connection. Also SSE uses a traditional HTTP connection without a special protocol and reconnects itself on connection loss.