I am newbie to node.js and have one query regarding mysql - batch insert query with node.js and socket.io.
var socket = require( 'socket.io' );
var express = require( 'express' );
var http = require( 'http' );
var mysql = require('mysql');
// batch processing variables
var TRIGGER_BATCH_INSERT_ON = 10;
var batch_insert_counter = 1;
var batch_insert_arr = [];
// create db connection
var con = mysql.createConnection({
host: HOST,
port: PORT,
user: MYSQL_USER,
password: MYSQL_PASS,
database: DATABASE,
});
var app = express();
var server = http.createServer( app );
var io = socket.listen(server);
io.sockets.on( 'connection', function( client ) {
// process client request using insert query
client.on( 'message', function( data ) {
users[0] = client.id;
console.log( 'Message received ' + data.name + ":" + data.message );
// check batch is ready for insert
if(TRIGGER_BATCH_INSERT_ON == batch_insert_counter){
// batch insert
var query = con.query('INSERT INTO message (author, message) VALUES ?', [batch_insert_arr], function(err, results) {
if (err) throw err;
});
// reset array and counter
batch_insert_arr = [];
batch_insert_counter = 0;
}
else{
// push records for batch processing
batch_insert_arr.push([data.name,data.message]);
batch_insert_counter = batch_insert_counter + 1;
}
});
});
server.listen(8000);
I inserted into mysql table after batch of 10 records is been pushed into an batch_insert_arr array. But I have doubt that it will work based on individual socket (client.on( 'message', function( data ) {}). It means an individual socket needs to send 10 messages then only it will work. I need any socket connected must be able to push data into batch_insert_arr array and then after batch of 10 records it gets inserted into db.
One more query: After insertion into db table, I have emptied the batch_insert_arr array. It might happen that while insert query is being processed, some other sockets also added data into that array and emptiying it might lost few records.
Please suggest how to move ahead with this or suggest me better approach.
Thanks in advance.