Developer Community

Emit messages on changes in your MongoDB

OCTOBER 14, 2022 • 4 MIN READ
Written by
Esther Agbaje

In today's world, streaming and processing events in real-time is critical. For example, you may want to listen to event changes in your Mongo database and immediately emit messages based on these changes.

However, getting real-time data changes from your MongoDB and emitting messages on these changes comes with complexities. But not anymore!

In this article, you will learn how to watch for changes in MongoDB and easily use Buildable's Node SDK to emit messages based on these changes.

Prerequisites

Before we get started, you'll need the following to complete this tutorial:

  • A Buildable account
  • A Mongo Database

Initializing a Node.js project

A Node.js project helps us set up the connection between MongoDB and Buildable. If you already have a Node.js project set up, feel free to skip this step.

Run the following command in your terminal to set up a Node.js project from scratch.


Now that we have a Node.js project running, the first thing you want to do is install the required packages. Since we're using MongoDB as our database and Buildable's SDK, let's go ahead to install them.

In your terminal window, run `npm install mongodb` and` npm install @buildable/messages` to add these packages to your NodeJS server.


Creating a Buildable Secret Key

Let’s head over to Buildable to create a secret key. If you don’t have an account, you can get one for free here.

Right in your dashboard, click on your avatar and select Settings.

On the left panel, click Secret Keys. You'll want to create a new secret key for our Node.js server. Let's call this secret key "mongodb".

Copy this secret key, we’ll be using it shortly.

Adding environmental variables

Back in our Node.js project, let’s create an .env file that contains the following environmental variables for Buildable and MongoDB:

  • BUILDABLE_SECRET_KEY
  • MONGO_URI
  • MONGO_DATABASE

Now, go ahead to paste the “mongodb” secret key earlier created.

After this, go into your MongoDB account. Select Database from the left panel, then Click Connect > Connect your application

Here, we’ll need to  grab the connection URI.

💡Remember to replace <username>:<password> with your database access username and password

Lastly, you also need to specify the name of the database your existing or new database in the `.env` file.

Your `.env` file should look like:


Connecting MongoDB to your Node.js project

In your Node.js project, create an `index.js` file and import the following at the top of your file:


Using the MongoDB NodeJS Driver, let’s connect to the MongoDB instance


Watching for changes in MongoDB

Next, we need to watch for any changes that happen in our database. To do this, let’s open a change stream to listen for any changes in our MongoDB.


💡 Change streams are a real-time stream of the changes that occur in the database or collections.

Emitting messages to Buildable

When changes happen in your MongoDB, we can emit message to Buildable using the SDK. With these messages, you can orchestrate processes that are useful for application (e.g. reporting, logging, analytics, etc).

Inside the stream listener, let’s use the Buildable SDK to emit a message.


Your final code should look like this:


Now, go ahead and run your `index.js` file.

You’d notice whenever there are changes in your Mongo database, messages are emitted in your Buildable Stream.

Simplify your MongoDB streaming process with Buildable

In this tutorial, you learned how to emit messages in real-time based on changes in your mongoDB using Buildable's SDK. With this, you can stay up to date with your data changes and begin emitting custom messages depending on your use case.

I hope this post will get you started emitting messages with Buildable.

If you have any questions, please reach out via Twitter or join our Discord community. Till next time, keep building with Buildable!