Taking Advantage of MongoDB’s ‘Change Streams’ for real time IoT applications using NodeJs

Basically, after creating a folder for all our database information, we need to run the mongod service (what we’ve just installed) and specify the port and path for your replica set.

Yes, a replica set.

This was what caused me so many troubles when attempting this for the first time.

In order to be able to watch changes in our database in real time (defined as change streams in mongodb’s documentation) you need to run not one instance of our db, but a cluster.

And this is basically what you are doing with the code above.

The first line creates the replica set, specifying your recently created data folder, so that the cluster stores its information there.

You also tell mongod the port in which our cluster will be running, and a few other options, with the parameter replSet being the name of our cluster.

After that you need to enter mongo shell and initialize our recently created cluster, which is what the second command does.

A few notes:The replica set needs to be initialized only once, there’s no need to run that command every time.

If you’re running it as a script, consider adding ‘&’ at the end of the mongod command to avoid having to use multiple terminals.

Once you’ve done all this we are ready to run our server.

js, so:cd ~/projectnpm initnpm install mongodb –savenode server.

js(I’m assuming you’re familiar with node and npm)Now you could run another script in a different terminal to insert a new item in the recently created database and see how server.

js prints every new insert in the cluster.

In the new terminal run:mongoThis will open mongo’s console so now we can interact with our database:use demo-dbdb.

data.

insert({sensor: 'Sensor A'})And just like that, in our server.

js terminal we should see our db insert appear as a console log.

ConclusionsIoT is not only changing the way we think hardware, but also how we design our software.

Requirements usually include being able to process data in real time and storing it for machine learning and big data analysis.

All this requirements impact on our server application, so choosing the right tools is essential.

Mongodb has proven to be a very flexible tool for our IoT applications regarding data storage.

If all worked out ok you should be able to integrate this simple example into your project.

.

. More details

Leave a Reply