<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community:  Prathamesh Madane</title>
    <description>The latest articles on DEV Community by  Prathamesh Madane (@pratham2811).</description>
    <link>https://dev.to/pratham2811</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F2783088%2Fc1bf9fba-afd2-4e85-8d9b-f2a1f1bacfb7.jpeg</url>
      <title>DEV Community:  Prathamesh Madane</title>
      <link>https://dev.to/pratham2811</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/pratham2811"/>
    <language>en</language>
    <item>
      <title>From Raw TCP to Real-Time Chat &amp; File Sharing — No Socket.io, Just Sockets</title>
      <dc:creator> Prathamesh Madane</dc:creator>
      <pubDate>Thu, 31 Jul 2025 18:31:26 +0000</pubDate>
      <link>https://dev.to/pratham2811/from-raw-tcp-to-real-time-chat-file-sharing-no-socketio-just-sockets-3cal</link>
      <guid>https://dev.to/pratham2811/from-raw-tcp-to-real-time-chat-file-sharing-no-socketio-just-sockets-3cal</guid>
      <description>&lt;p&gt;Introduction&lt;br&gt;
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.&lt;/p&gt;

&lt;p&gt;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.&lt;/p&gt;

&lt;p&gt;That got me thinking:&lt;/p&gt;

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

&lt;p&gt;So I built it—from scratch.&lt;/p&gt;

&lt;p&gt;How It Works&lt;br&gt;
Server-Side Logic&lt;br&gt;
Client Identification:&lt;br&gt;
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.&lt;/p&gt;

&lt;p&gt;Message Broadcasting:&lt;br&gt;
Whenever a client sends a text message, the server relays that message to all other connected clients by writing to their socket streams.&lt;/p&gt;

&lt;p&gt;File Transfers:&lt;br&gt;
If the client sends a special header like FILE:|, the server detects this as a file transfer.&lt;br&gt;
It:&lt;/p&gt;

&lt;p&gt;Temporarily removes the data listener for regular messages.&lt;/p&gt;

&lt;p&gt;Listens for file chunks.&lt;/p&gt;

&lt;p&gt;Forwards file chunks to other clients.&lt;/p&gt;

&lt;p&gt;Restores the regular message listener once the file is fully transferred.&lt;/p&gt;

&lt;p&gt;This mechanism allows a seamless switch between text and binary data streaming on the same socket.&lt;/p&gt;

&lt;p&gt;Client-Side Logic&lt;br&gt;
Connection Setup:&lt;br&gt;
Each client opens a raw TCP connection to the server.&lt;/p&gt;

&lt;p&gt;Sending Data:&lt;/p&gt;

&lt;p&gt;For messages: It simply sends the typed message string.&lt;/p&gt;

&lt;p&gt;For files: It sends a custom header FILE:| and then streams the file in binary.&lt;/p&gt;

&lt;p&gt;Receiving Data:&lt;br&gt;
On receiving data, the client checks:&lt;/p&gt;

&lt;p&gt;If it’s a message → display it.&lt;/p&gt;

&lt;p&gt;If it’s a file header → start writing the received chunks to a local file.&lt;/p&gt;

&lt;p&gt;The client dynamically switches between message and file reception using internal flags and temporary data listeners.&lt;/p&gt;

&lt;p&gt;Features&lt;br&gt;
Supports multiple clients&lt;/p&gt;

&lt;p&gt;Real-time message broadcasting&lt;/p&gt;

&lt;p&gt;File sharing over raw TCP sockets&lt;/p&gt;

&lt;p&gt;Duplex streaming with zero third-party libraries&lt;/p&gt;

&lt;p&gt;Pure Node.js and net module-based implementation&lt;/p&gt;

&lt;p&gt;What I Learned&lt;br&gt;
How TCP socket streams operate at a low level in Node.js&lt;/p&gt;

&lt;p&gt;The nature of full-duplex communication using net.Socket&lt;/p&gt;

&lt;p&gt;Handling multiple client connections manually&lt;/p&gt;

&lt;p&gt;Dynamic event listener switching based on protocol-like headers&lt;/p&gt;

&lt;p&gt;Source Code&lt;br&gt;
The full code is available on GitHub:&lt;br&gt;
🔗 (&lt;a href="https://github.com/Pratham2811/backend/tree/main/Nodejsnetworking/HTTP/creatingchatcli" rel="noopener noreferrer"&gt;https://github.com/Pratham2811/backend/tree/main/Nodejsnetworking/HTTP/creatingchatcli&lt;/a&gt;)&lt;/p&gt;

&lt;p&gt;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.&lt;/p&gt;

</description>
      <category>webdev</category>
      <category>learning</category>
      <category>node</category>
      <category>development</category>
    </item>
  </channel>
</rss>
