Socket Freeze Issue During High Data Volume in Node.js Application

17 Views Asked by At

I'm having a problem with a Node.js application. This application is a client that reads data from a TCP connection using net.socket. When there's a sudden surge in data volume, the socket appears to freeze, preventing further data arrival. After a period of being stuck, the socket unlocks, and the previously withheld data starts arriving, as if it had been temporarily held back and then released.

const connect = require('node:net').connect
const PassThrough = require('node:stream').PassThrough

const TELNET_HOST = process.env.TELNET_HOST
const TELNET_PORT = process.env.TELNET_PORT

const broadcaster = new PassThrough()
const socket = connect(
  {
    host: TELNET_HOST,
    port: TELNET_PORT,
    onread: {
      buffer: Buffer.alloc(32 * 1024),
      callback: (nread, buffer) => socket.emit('data', buffer.subarray(0, nread)),
    },
  },
  () => console.info('Connected')
)

socket.on('data', (data) => broadcaster.write(data))
0

There are 0 best solutions below