DEV Community

1xApi
1xApi

Posted on • Originally published at 1xapi.com

5 Ways Node.js 22's Built-in WebSocket Client Changes API Development

5 Ways Node.js 22's Built-in WebSocket Client Changes API Development

As of March 2026, Node.js 22 has been the LTS version for over a year, and its built-in WebSocket client is finally getting the attention it deserves. No more ws or socket.io dependencies just for client-side WebSocket connections.

1. No More External Dependencies

Previously, you needed packages like ws or node-fetch for WebSocket connections. Node.js 22 ships with a stable, browser-compatible WebSocket client enabled by default.

// Before: npm install ws
// const WebSocket = require('ws');

// After: Just use it
const ws = new WebSocket('wss://api.example.com/realtime');

ws.onopen = () => {
  console.log('Connected!');
  ws.send(JSON.stringify({ type: 'subscribe', channel: 'updates' }));
};

ws.onmessage = (event) => {
  const data = JSON.parse(event.data);
  console.log('Received:', data);
};
Enter fullscreen mode Exit fullscreen mode

2. Unified Full-Stack Code

The WebSocket API in Node.js 22 matches the browser API exactly. This means you can share WebSocket code between frontend and backend.

// Works in browser AND Node.js 22+
const socket = new WebSocket('wss://your-api.com/ws');

socket.addEventListener('open', () => {
  socket.send('Hello!');
});
Enter fullscreen mode Exit fullscreen mode

3. Better Performance with AbortSignal

Node.js 22 optimized AbortSignal creation, which directly benefits WebSocket connections. You can now timeout connections cleanly:

const controller = new AbortController();
const timeout = setTimeout(() => controller.abort(), 5000);

const ws = new WebSocket('wss://api.example.com', {
  signal: controller.signal
});

ws.onopen = () => clearTimeout(timeout);
ws.onerror = () => clearTimeout(timeout);
Enter fullscreen mode Exit fullscreen mode

4. Native Integration with Fetch API

Since Fetch is also stable in Node.js 22, you can combine them elegantly:

async function connectWithAuth(url) {
  const token = await getToken(); // Your auth logic

  const ws = new WebSocket(url, {
    headers: { 'Authorization': `Bearer ${token}` }
  });

  return new Promise((resolve, reject) => {
    ws.onopen = () => resolve(ws);
    ws.onerror = reject;
  });
}
Enter fullscreen mode Exit fullscreen mode

5. Simpler Real-Time API Implementations

Building real-time APIs just got easier. Here's a complete server + client example:

// Server (using Bun or Node.js 22)
const { Server } = require('ws');
const wss = new Server({ port: 8080 });

wss.on('connection', (ws) => {
  ws.on('message', (data) => {
    // Broadcast to all clients
    wss.clients.forEach(client => {
      if (client.readyState === WebSocket.OPEN) {
        client.send(data);
      }
    });
  });
});

// Client (Node.js 22 - no dependencies!)
const client = new WebSocket('ws://localhost:8080');
client.on('message', (e) => console.log(e.data));
Enter fullscreen mode Exit fullscreen mode

Why This Matters in 2026

The WebSocket client in Node.js 22 isn't just "nice to have" — it's part of a larger shift toward reducing dependency bloat. Fewer npm packages means:

  • Smaller bundle sizes
  • Fewer security vulnerabilities
  • Faster cold starts in serverless
  • Consistent API across your stack

If you're building real-time features in 2026, give Node.js 22's native WebSocket client a try. It's production-ready and simplifies your dependency tree significantly.


What do you think? Are you using the built-in WebSocket client, or still sticking with ws? Let me know in the comments!

Top comments (0)