Search code examples
node.jssocketstcppacketboundary

nodejs: does socket data need reassemble and prepend length for packet boundary?


I'm building a tcp server. I have some questions:

In nodejs, when socket receive data event:

  1. Do need to take care whether the packet order is correct, in other words, how the tcp packets reassemble is handled? Is it handled already by the underlying OS or Nodejs? Or I need to do reassemble myself?

  2. Will nodejs take care the packet boundary automatically? Meaning, is the possible:

    • receive multiple packets in one data event
    • one packet spread in multiple data events

Solution

  • Do need to take care whether the packet order is correct, in other words, how the tcp packets reassemble is handled? Is it handled already by the underlying OS or Nodejs? Or I need to do reassemble myself?

    It is handled by TCP.

    Will nodejs take care the packet boundary automatically? Meaning, is the possible:

    • receive multiple packets in one data event
    • one packet spread in multiple data events

    TCP/IP is a byte stream protocol. There are no packets or packet boundaries visible to applications.