Why JavaScript in Network Programming?
Once upon a time, JavaScript was just a language for animating buttons and validating forms. Today, it is a powerful tool for creating network applications. Network programming in JavaScript opens the door to the world of real-time: chats, online games, data streaming, monitoring systems, and even your own web servers.
In this article, we won't boringly rewrite the documentation. We will journey from a simple HTTP request to a full WebSocket connection. You will see how Node.js turns a browser language into a powerful network engine. Ready? Let's go!
Basics: HTTP and Node.js Modules
Any network programming in JavaScript begins with understanding protocols. The most popular one is HTTP. In the browser, you use fetch() or XMLHttpRequest, but the server side requires a different approach.
Creating a Simple HTTP Server
Node.js provides the built-in http module. Here is a minimal example that responds with "Hello, world!" to any request:
const http = require('http');
const server = http.createServer((req, res) => { res.statusCode = 200; res.setHeader('Content-Type', 'text/plain'); res.end('Hello, world! Network programming in JavaScript works!');});
server.listen(3000, '127.0.0.1', () => { console.log('Server running at http://127.0.0.1:3000/');});Run this code with node server.js and open your browser. You will see our message. Simple, right? But this is just the beginning. The real magic starts when we work with sockets.
Working with TCP Sockets in JavaScript
HTTP is an application layer protocol built on top of TCP. If you need full control over data transmission, use JavaScript sockets. In Node.js, the net module handles this.
TCP Server: Receiving Data
Imagine you are writing a simple echo server. A client connects, sends a message, and the server sends it back. Here is what it looks like:
const net = require('net');
const server = net.createServer((socket) => { console.log('Client connected');
socket.on('data', (data) => { console.log('Received:', data.toString()); socket.write('Echo: ' + data.toString()); });
socket.on('end', () => { console.log('Client disconnected'); });});
server.listen(8080, () => { console.log('TCP server listening on port 8080');});Now let's write a client for this server. Client-server in JavaScript is a two-way movement:
const net = require('net');
const client = net.createConnection({ port: 8080 }, () => { console.log('Connected to server'); client.write('Hello, server!');});
client.on('data', (data) => { console.log('Server response:', data.toString()); client.end();});
client.on('end', () => { console.log('Disconnected from server');});Run the server first, then the client. You will see data traveling back and forth. This is the foundation of all JavaScript network protocols.
WebSocket: Real-Time Without Delays
HTTP and TCP are good, but they have a problem: to get new data, the client must constantly make requests (polling). This is inefficient. WebSocket in JavaScript solves this problem — after establishing a connection, the server can send data at any time.
WebSocket on Node.js (ws library)
Install the library: npm install ws
Server:
const WebSocket = require('ws');
const wss = new WebSocket.Server({ port: 8081 });
wss.on('connection', (ws) => { console.log('WebSocket client connected');
ws.on('message', (message) => { console.log('Received:',