node-mongodb Protection against websocket data loss

301 Views Asked by At

I am on the client side of a websocket API which uses SocketCluster for its pub/sub.

After authenticating I receive every second json data via

SCsocket.on('authenticate', function(){
   var channel = SCsocket.subscribe('channel1');
   channel.watch(function(data){
      console.log(data); 
   });
});

of the form

[
  {
      "product": "Product1",
      "price":   "10.0" 
  },
  {
      "product": "Product2",
      "price":   "15.0"  
  }
]

Instead of printing the data, I will save it to a mongo db.

In case some of the data cannot be uploaded, I need some sort of a safety net. Something that allows me to upload the data from the websocket to mongo db in retrospect.

What are best practices for doing that? I hope this question is not too broad, I am new to node and mongo db.

Thanks!

1

There are 1 best solutions below

4
On

This is an example of how you can handle the data and save it into Mongo. I didn't have a mongo server running, so you'll have to check that the saving actually works. But the principle is there.

Not sure what you mean by safety net but I added in a check to see if the data is defined, you should more checks in there for your own specific cases.

Let me know if you need any help with something specific.

SCsocket.on('authenticate', function () {

    // subscribe to channel 1
    var channel = SCsocket.subscribe('channel1');
    channel.watch(function (data) {
        // if there is any data
        // you should do more specific checks here
        if (Object.keys(data).length > 0) {

            // connect to mongo
            MongoClient.connect(url, function(err, db) {

                assert.equal(null, err);

                // insert data
                insertDocument(data, db, function() {
                    db.close(); // close db once insert
                });
            });
        } else {
            // handle invalid data
            console.log('data sent is invalid');
            console.log(data);
        }
    });
});


// insert into mongo
var insertDocument = function (data, db, callback) {

    // save into product
    db.collection('product').insertOne(
        // insert data sent from socket cluster
        data, function (err, result) {
        assert.equal(err, null);
        console.log("Inserted successfully into");
        console.log(result); // output result from mongo
        callback();
    });
};