Exploring the New WebSocket Client in Node.js 23: What You Need to Know

Node.js 23 introduces a subtle but powerful addition — a native WebSocket client. While WebSocket support has existed through third-party libraries like ws
, this is the first time Node.js ships with a built-in client API that's aligned with the browser standard.
"When you eliminate the unnecessary, the necessary speaks." – Hans Hofmann
In this article, we’ll explore how the new WebSocket client works, how it compares to existing solutions, and how you can start using it today — no extra packages required.
🌐 The Bigger Picture: Why It Matters
If you've ever built bots, real-time dashboards, or CLI tools that consume WebSocket APIs, you know the usual dance:
npm install ws
Now, with native support, you can skip the install and get straight to work with a familiar browser-style API.
"Simplicity is the ultimate sophistication." – Leonardo da Vinci
🚀 A Native Client in Action
Here’s how you can use the native client to connect to a WebSocket server:
// client.js
const ws = new WebSocket("ws://localhost:8080");
ws.addEventListener("open", () => {
console.log("Connected to server");
ws.send("Hello from native client!");
});
ws.addEventListener("message", (event) => {
console.log("Received from server:", event.data);
});
Run it with:
node client.js
No flags. No polyfills. Just Node.js 23.
🛠 Still Need a Server? Use ws
Node.js 23 doesn't (yet) include a native WebSocket server, so for now we’ll still rely on ws
to handle incoming connections:
// server.js
import { WebSocketServer } from "ws";
const wss = new WebSocketServer({ port: 8080 });
wss.on("connection", (ws) => {
console.log("Client connected");
ws.on("message", (message) => {
console.log("Received:", message.toString());
ws.send(`Echo: ${message}`);
});
ws.send("Welcome to the server");
});
✅ Pros of Native WebSocket Client
Zero dependencies
Familiar browser-like API
Faster startup and reduced bundle size for tools
⚠️ Limitations
No native server yet — you still need
ws
or similarAPI is client-only for now, so this is only half the story
"Half a loaf is better than none. Especially if it's native." – Probably Not Shakespeare
💾 Sample Code on GitHub
Want to try it yourself? You can find the full example code used in this article in the GitHub repo:
👉 codanyks/native-websocket-nodejs
Feel free to clone, experiment, and modify to fit your use case.
🧭 What’s Next?
You’ve got a native client and a working server. But what about securing the connection?
Next up: Securing WebSockets: Authentication in WebSocket Connections — where we’ll dive into authentication for real-time systems.
Stay tuned!
Subscribe to my newsletter
Read articles from codanyks directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by
