i am getting data using websockets
approx 30 messages per second. and i want to pass the received message (object) to a custom function
as a parameter
.
the function
is somewhat slow (time consuming, DB related queries), and i want function to run async
or parallel
i.e. not wait for function to complete i.e. pass new received message from websocket
to function
ASAP, i am not looking to get data returned from function
. so it can be run freely and then done with it.
in cpp there is
boost::thread(functionName, parameter);
how can i do something like this in nodejs
here is my code in nodejs.
var WebSocketClient = require('websocket').client
var WSclient = new WebSocketClient();
WSclient.connect('127.0.0.1/ws');
WSclient.on('connect', function(conn){
console.log('connected to websocket');
conn.on('message', function (packet){
var msg = JSON.parse(packet)
processMessage(msg);
//dont wait
processMessageOtherway(msg);
});
});
function processMessage ( msgObject ){
//time consuming code
msgObject.rhand
.filter(elem => elem.isUser === false && typeof elem.name === "string")
.map(function(elem) {
//check if message is in our database
mysql_con.query("SELECT value FROM key_value WHERE key = '" + (elem.name) + "'" , function (err, result) {
if (err) throw err;
if ( result.length > 0 ){
if(result) {
data = result[0].value
//request remote page
request({
url: "http://example.com/pushValue",
method: "POST",
body: data
}, function(error, response, received_body){
console.log(received_body);
});
}
}
});
});
}
function processMessageOtherway ( msgObject ){
//time consuming code
msgObject.lhand
.filter(elem => elem.isUser === false && typeof elem.name === "string")
.map(function(elem) {
//check if message is in our database
mysql_con.query("SELECT value FROM key_value WHERE key = '" + (elem.name) + "'" , function (err, result) {
if (err) throw err;
if ( result.length > 0 ){
if(result) {
data = result[0].value
msgObject.rhand
.filter(elem => elem.isUser === false && typeof elem.name === "string")
.map(function(elem) {
console.log(elem.FullName) ;
});
}
}
});
});
}
How can i do something like this in async ?