Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

what determines the size of socket data event in nodejs

I want to know what determines the size of data when a tcp socket emitted an 'data' event as following, thx!

    var client = new Socket(options);
    client.on('data', chunk => {}); // what determines the size of chunk?
like image 962
user5487705 Avatar asked Dec 09 '25 14:12

user5487705


1 Answers

When a IP packet with aTCP segment arrives, the data contained will be pushed into the internal buffer of net.Socket (which is a duplex stream ) with stream.push(chunk).

Provided that net.Socket is in flowing mode, the data added with stream.push(chunk) will be delivered with a 'data' event. As a result, the size of the chunk corresponds to the data of the TCP segment which arrived.

However, if the stream is paused and then resumed, the chunk released afterwards will not correspond to the data contained to a single TCP segment.

The stream internal buffer can hold data up to highWaterMark threshold.

See details on buffering and highWaterMark here: https://nodejs.org/api/stream.html#stream_buffering

like image 169
Charidimos Avatar answered Dec 11 '25 03:12

Charidimos