In this lesson, you will learn how to bridge the gap between human-readable strings and the raw binary data that actually travels across a TCP network.
The Problem with Strings
In JavaScript, strings are encoded as UTF-16, which is great for displaying text but terrible for networking. TCP sockets communicate using raw bytes. If you try to send a string directly over a TCP stream, Node.js has to perform an "encoding conversion" behind the scenes to translate those characters into a format (like UTF-8) that the receiver can understand.
Why Buffers Matter
A Buffer is a raw memory allocation outside the V8 heap. When we deal with networking, we prefer Buffers because they represent the exact bytes being pushed onto the wire. If you receive data from a socket, it arrives as a Buffer because the OS doesn't know if that data is text, an image, or a custom binary protocol.
Encoding Conversions in Practice
To communicate reliably, you must explicitly convert your data. When sending, you turn a string into a Buffer; when receiving, you turn the Buffer back into a string using the correct character encoding.
const socket = getTcpSocket(); // Imagine a connected TCP socket
// Sending: Convert string to a binary Buffer using UTF-8
const message = "Hello, Network!";
const bufferToSend = Buffer.from(message, 'utf8');
socket.write(bufferToSend);
// Receiving: Convert raw binary data back into a readable string
socket.on('data', (chunk) => {
// 'chunk' is a Buffer object
const decodedMessage = chunk.toString('utf8');
console.log(`Received: ${decodedMessage}`);
});
When to Avoid Strings
If you are streaming binary files, images, or compressed data, never convert them to strings. Doing so risks "data corruption" because the byte sequences might not form valid characters in any encoding. Always treat binary payloads as raw Buffer streams to maintain data integrity across the socket.
Key Takeaway: Always use Buffers for raw network transmission and explicitly encode or decode them only when you need to interface with human-readable text.