Black Lives Matter. Please consider donating to Black Girls Code today.

Real time streaming node.js example from mongodB

Any tutorial on how to connect from mongodB from AWS
I have MongodB driver and this is initial read in from AWS

MongoClient = require('mongodb').MongoClient, assert = require('assert');
var url = 'mongodb://';

MongoClient.connect(url, function (err, db) {
    assert.equal(null, err);
    console.log("Connected successfully to energy server!");
        findDocuments(db, function () {

var findDocuments = function (db, callback) {
    var collection = db.collection('myenergydata');
    console.log('This is collection : ', collection);

    var myCount = db.collection('myenergydata').count();
    console.log('Counted number of documents : ', myCount);

    // Find some documents
    collection.find({}).toArray(function (err, chartData) {
        assert.equal(err, null);
        console.log("Found the following records");
        console.log('Type of doc is : ', typeof(chartData)); //Type of doc is :  object
        console.log('Object 0 energy is :', chartData[0].energy); //16
        console.log('Object 0 time is :', chartData[0].time); // 06/08/2015 10:00
        console.log('Object 0 time is :', chartData[1].time); //06/08/2015 10:30

which is 30 mins data of the format

  "_id" : ObjectId("57d9209236b7c31958782bd2"),
  "time" : "01/08/2015 01:30",
  "energy" : 29,
  "created_date" : ISODate("2016-09-14T00:00:00Z")
  "_id" : ObjectId("57d9209236b7c31958782bd3"),
  "time" : "01/08/2015 02:00",
  "energy" : 28,
  "created_date" : ISODate("2016-09-14T00:00:00Z")

and plot.js to update real time as new data comes in?

1 Like

I think the recommendation is to get data from MongoDB continuously, then just repeatedly redraw the plot.

You could also use Plotly’s hosted streaming service with Node.

Since I can’t post more than two links in a post as a presumably inexperienced new user, here’s a separate post, linking to Node streaming examples:

See also using extendTraces.

Thanks @dandv if streaming data is coming from MONGO_URL i.e MONGO driver to AWS ec2 and not HTTP URL to Plotly server?

var MONGO_URL = ‘mongodb://’;

Or I can connect to anywhere ? So the streaming examples are from Plotly servers only?