Is socket.io.emit () method synchronous or asynchronous?

I have a server side code that runs in a loop like this:

        var start = Date.now();
        var lotsOfData = calculateData();//SYNCHRONOS, returns 500kb of data
        var end = Date.now();

        console.log('got data in ', end-start, ' ms'); // ~40ms

        var substart = Date.now();
        req.io.emit('lotsOfData', lotsOfData);// IS IT SYNC OR ASYNC ?
        var subend = Date.now();

        console.log('emitted data in ', subend-substart, ' ms'); // ~40ms

      

At first I do some pretty expensive calculations to get some data. There, synchronous work and the code are blocked. After that, I launch and send this data.

My test shows that receiving and sending takes ~ 40ms. However, if the sending part was asynchronous, it would be possible to receive the next piece of data when the current one was sent, so the server would send pieces of data every 40 milliseconds, rather than every 80 milliseconds as it is now.

I hope you understand what I mean. Can you speed it up?

+3


source to share





All Articles