0

I have a nodejs process with 4 different websockets, each pushing thousand events each second. For each event I'm calling an async function which elaborate it.

Does nodejs execute this function in parallel every time an event arrive, or nodejs will wait the execution to process the next event?

async function callHttp(data){
    await http.get...
}

socket.on('event1', async (data) => {
    await callHttp(data)
});

socket.on('event2', async (data) => {
    await callHttp(data)
});

socket.on('event3', async (data) => {
    await callHttp(data)
});

socket.on('event4', async (data) => {
    await callHttp(data)
});

If I change the await in a promise with .then, does it change something?

Francesco Clementi
  • 1,874
  • 4
  • 13
  • 28

1 Answers1

1

Does nodejs execute this function in parallel every time an event arrive, or nodejs will wait the execution to process the next event?

If this code receives rapid fire incoming events, it will not wait the execution for the first one to finish before the next one starts. As soon as the first one hits the await or returns and there are events in the queue waiting to be processed, then next event will fire.

await does not block the interpreter from going back to the event loop and processing more events. So, as soon as your code hits an await, the interpreter will suspend execution of that function, return a promise from it (that's what all async functions do) and as soon as the calling code returns, it will go back to the event loop to see if there are more incoming events to process. If so, it grabs the next one from the event queue and starts processing it.

This will result in all your callHttp(data) functions running in parallel, all in flight at the same time.

This is an example of I/O and await in Javascript are both non-blocking. The interpreter is free to process other incoming events during an asynchronous I/O call or during an await.

If I change the await in a promise with .then, does it change something?

No. It changes nothing here.


If you needed to serialize the processing of these events such that you don't process event2 until event1 is done, you'd probably have to implement your own queue so that when event2 arrives, you check to see if you're already processing another event and, if so, you just insert event2 into your queue. Then, when any event finishes it's process, it notifies the queue so the queue can run the next event in line. But, at that point, I'd suggest a more top-down review of the overal design of what you're' trying to accomplish and how it is currently implemented because something is probably less-than-ideal in the design if you're forcing your server to serialize all these actions.

jfriend00
  • 683,504
  • 96
  • 985
  • 979
  • I like your answer, and a deeper one is: https://stackoverflow.com/questions/14795145/how-the-single-threaded-non-blocking-io-model-works-in-node-js. How ever I tried to implement a queue with bull, but it's too much slower than pure js. – Francesco Clementi Nov 26 '21 at 08:23