jsrosetta

I/O

TCP Server

How Node.js's net.createServer compares to net.Listen (Go), TcpListener (Rust), Network.framework (Swift), and ServerSocket (Java) for a TCP echo server.

Minimum versions
Node.js ≥ 12.20Go ≥ 1.0Rust ≥ 1.65Swift ≥ 5.7Java ≥ 25
Verified on
Node.js 24.12.0Go 1.27.1Rust 1.98.1Swift 6.2.4Java 25.0.4.1

Node.js code is an ES module: save it as .mjs or set "type": "module" in package.json.

A TCP server just needs to: listen on a port, accept connections, then read/write data on each one. Node.js handles each socket with events; Go and Rust handle each connection in its own thread/goroutine (Rust has no goroutines — thread::spawn creates a real OS thread, which is heavier; matching a goroutine's lightness needs an async runtime like tokio). Swift uses Network.framework (Apple-only; Foundation has no socket API of its own). Java 25 handles each connection with a virtual thread (Thread.ofVirtual(), finalized in Java 21) — nearly as cheap as a goroutine, no thread pool needed.

Echo server

import net from 'node:net'
 
function handler(socket) {
  socket.write('Received: ')
  socket.pipe(socket)
}
 
const server = net.createServer(handler)
server.listen(3000)
$ echo 'hello' | nc localhost 3000
Received: hello

Reference: github.com/miguelmota/golang-for-nodejs-developers#tcp-server