DEV Community

 Prathamesh Madane
Prathamesh Madane

Posted on

From Raw TCP to Real-Time Chat & File Sharing — No Socket.io, Just Sockets

Introduction
I’ve recently started exploring low-level networking—specifically how the TCP protocol works under the hood. Instead of relying on high-level libraries like socket.io, I decided to build something real using raw TCP sockets in Node.js to understand networking fundamentals at a deeper level.

I began with UDP, a connectionless protocol that simply sends raw packets without caring about their delivery, order, or reliability. This gave me a contrast point before diving into TCP, which is connection-oriented, reliable, and stream-based. TCP uses a 3-way handshake (SYN → SYN-ACK → ACK) to establish a connection, and every client has a dedicated socket for full-duplex communication—both ends can read and write simultaneously.

That got me thinking:

Can I use TCP’s duplex stream to build a simple system for real-time messaging and file transfer between multiple clients?

So I built it—from scratch.

How It Works
Server-Side Logic
Client Identification:
When a new client connects, the server prompts for a username. It keeps track of each client as an object { username, socket } in an in-memory array.

Message Broadcasting:
Whenever a client sends a text message, the server relays that message to all other connected clients by writing to their socket streams.

File Transfers:
If the client sends a special header like FILE:|, the server detects this as a file transfer.
It:

Temporarily removes the data listener for regular messages.

Listens for file chunks.

Forwards file chunks to other clients.

Restores the regular message listener once the file is fully transferred.

This mechanism allows a seamless switch between text and binary data streaming on the same socket.

Client-Side Logic
Connection Setup:
Each client opens a raw TCP connection to the server.

Sending Data:

For messages: It simply sends the typed message string.

For files: It sends a custom header FILE:| and then streams the file in binary.

Receiving Data:
On receiving data, the client checks:

If it’s a message → display it.

If it’s a file header → start writing the received chunks to a local file.

The client dynamically switches between message and file reception using internal flags and temporary data listeners.

Features
Supports multiple clients

Real-time message broadcasting

File sharing over raw TCP sockets

Duplex streaming with zero third-party libraries

Pure Node.js and net module-based implementation

What I Learned
How TCP socket streams operate at a low level in Node.js

The nature of full-duplex communication using net.Socket

Handling multiple client connections manually

Dynamic event listener switching based on protocol-like headers

Source Code
The full code is available on GitHub:
🔗 (https://github.com/Pratham2811/backend/tree/main/Nodejsnetworking/HTTP/creatingchatcli)

This project helped me understand TCP not just as a protocol, but as a practical, programmable tool. It’s not production-ready, but it’s a useful learning experiment that helped me go deeper into how real-time systems work from the wire up.

Top comments (0)