Using SocketIO to publish Mongo database change streams to clients

Member-only story

Mongo Change Streams and SocketIO Web Sockets: Abaganon

Create a NodeJS app that streams Mongo data changes over web sockets.

Eric Fossas
4 min readJul 18, 2019

--

This is a full tutorial on creating a NodeJS app that listens for changes in a Mongo database and streams those changes to clients who connect to the app through a SocketIO web socket. The tutorial will also include the setup on the client side and the Mongo configuraiton.

Using this setup, the Mongo database will be the “Source of Truth” and users can get live updates on any changes.

For the full working example, reference this repo: MongoChangeStreamSockets

What This Tutorial Covers

  1. Installing & Configuring Mongo For Change Streams
  2. Creating A NodeJS App With SocketIO & Mongo Driver
  3. Configuring A Back End For Basic Mongo Updates
  4. Configuring Client Side SocketIO

What You Need For This Tutorial

Docker

Mongo Set Up

Mongo requires either a replica set or a sharded cluster in order to enable change streams. We’ll be using a replica set. The following file is the Docker Compose configuration for a mongo replica set along with the builds and deployments for the back and front ends.

Btw, on Kubernetes you would deploy a Mongo replica set using a headless service and a stateful set. Unfortunately, Docker doesn’t have those concepts, so we’ll have to define each Mongo replica as a separate Docker service.

Some important things to note:

  • It’s important to use Mongo 4.0 as that’s when database change streams was added. In version 3.6 you can watch change streams on collections, but not entire databases.

--

--

Responses (1)

Write a response