Using WebSocket I receive updates which i want to store in a mongodb database. Each time there is an update an event with data is emitted. The data is an array of updates, which need to be processed one by one before putting them in the database. Sometimes, a new update is received before the old update is processed. When this happens, only a part of the old update is processed and saved and the function starts processing the new data.
Is there a way to run multiple instances of this function, so that starting a new function with new data doesn't stop the processing of the old data?
eventBTRX.on('marketUpdate', function(data) {
data.Sells.forEach(function(askChange) {
console.log(askChange);
switch (askChange.Type) {
case 0:
delete askChange.Type
var askNew = {
Quantity: askChange.Quantity,
Rate: askChange.Rate,
Type: 'ask',
Exchange: 'BTRX'
}
dbo.collection(colName).insertOne(askNew, function(err, result) {
if (err) console.log(err);
});
break;
case 1:
delete askChange.Type
askChange.Type = 'ask';
askChange.Exchange = 'BTRX';
var deleteQuery = {Exchange: bidChange.Exchange, Rate: askChange.Rate, Type: askChange.Type};
dbo.collection(colName).deleteOne(deleteQuery, function(err, result) {
if (err) console.log(err);
});
break;
case 2:
delete askChange.Type
askChange.Type = 'ask';
askChange.Exchange = 'BTRX';
var updateQuery = {Exchange: askChange.Exchange, Rate: askChange.Rate, Type: askChange.Type};
var newValue = { $set: {Quantity: askChange.Quantity} };
dbo.collection(colName).updateOne(updateQuery, newValue, function(err, result) {
if (err) console.log(err);
});
break;
default:
console.log('Error in update type.')
}
});