0

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 ?

  • If the things your function is doing are predominantly asynchronous (such as DB queries), then node.js will already have multiple things in flight at the same time. As soon as the first request goes to an async operation and has nothing else to do until that async result returns, then it will start serving the next message that has arrived. The only way to get more parallelism than that is to use multiple node.js processes (such as a the node.js cluster module or custom child processes that work is handed off to). The multiple processes let you engage multiple CPUs. – jfriend00 Sep 26 '17 at 03:47
  • If you show the actual code involved in `processMessage()`, we'd be more likely to be able to make specific coding suggestions. – jfriend00 Sep 26 '17 at 03:50
  • there is select query, inside the function, but its pretty fast, i mean 10ms. or less. –  Sep 26 '17 at 03:50
  • adding more code. –  Sep 26 '17 at 03:50
  • In direct answer to the question in the title, No - there's is no node.js equivalent of `boost::thread()`. A single node.js process only runs one thread of user Javascript - period. You'd have to explicitly launch additional node.js processes to get multiple interpreters truly working in parallel. – jfriend00 Sep 26 '17 at 03:53
  • Still waiting to see the additional code you said you were going to add. – jfriend00 Sep 26 '17 at 04:03
  • @jfriend00 added , please check. –  Sep 26 '17 at 04:05
  • @jfriend00 the main reason i wan to do this, becuase idk how the websocket reacts, if my function takes longer than the speed of incoming messages, does i miss messages, or the process queue gets longer ? –  Sep 26 '17 at 04:06
  • Incoming webSocket messages that are not immediately handled are queued. node.js is an event driven system. Incoming messages waiting to be processed would go in the node.js event queue waiting for node.js to finish what it was previously doing and service the next message in the event queue. – jfriend00 Sep 26 '17 at 04:12
  • The time consuming portion of your code (database query and `request()`) already is async. Unless `msgObject.rhand` is tens of thousands long or the result after `.filter()` is long, your function should consume very little node.js CPU as the time consuming stuff is pretty much all outside the main node.js thread. How long is the array before and after `.filter()`? – jfriend00 Sep 26 '17 at 04:15
  • sometimes, 2 items, sometimes 200 items, not more than 1k. –  Sep 26 '17 at 04:53
  • how long does the queue gets ? is there limit ?, how can i check how many items are in the queue, i mean lagging behind ? –  Sep 26 '17 at 04:55
  • Your code does not make a lot of sense to me. The first and the second function are effectively the same thing, most of the code is duplicated, down to the database query. Why would you want to hit the database *twice* with the same SQL in short succession? Also, please use parameterized queries. It's not okay anymore to build SQL by string concatenation in this day and age. As the others have said, all your code is async already. Do you have a tangible issue with it? – Tomalak Sep 26 '17 at 04:57
  • the object has 2 objects inside it, lhand and rhand, process for 2 object is somwhat simmilar until looking it up in db, and if found then follow the different flow. –  Sep 26 '17 at 05:05
  • 1
    See: [How many events can node.js queue?](https://stackoverflow.com/questions/34140101/how-many-events-can-node-js-queue) – jfriend00 Sep 26 '17 at 05:39
  • There is no Javascript access to the event queue to know anything about what's in it. If you really needed to know, you could use the main node.js process to immediately process each event into your own queue and do all subsequent processing that takes more time in one or more other node.js processes (so that processing doesn't interfere with you keeping your queue up-to-date). Then, you'd have your own current queue available to you in the main node.js process. – jfriend00 Sep 26 '17 at 05:41

0 Answers0