Skip to content
Agents tracked: 506 Releases (7d): 634 Agent status: 4 with issuesSince last limit reset: Claude 15d · Codex 0d For AI agents: Markdown Updated Nov 26, 2026

ws

Live two-way connections for Node.js servers.

HTTP clients and networkingNPM

GitHubWebsite

1. License

“Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction”

MIT allows commercial use; keep the copyright and license notice with the code.

github.com/websockets/ws/blob/master/LICENSE

From the package registry and GitHub, checked daily; card checked Nov 10, 2026. Tap a verdict to see its source.How we checkedReport an error

What ws does

ws is a free library for Node.js that keeps a live, two-way connection open between your server and each visitor. The server can push new chat messages, prices or progress updates the moment they happen, without the page asking again. It runs only on the server side; the browser uses its own built-in WebSocket.

What people build with it

Live chat or comments

A message one visitor sends shows up for everyone else on the page at once, without reloading.

Push updates from your server

Your Node.js server sends new orders, scores or job progress to the open page the moment they change.

Listen to a live data feed

A backend script connects to another service's live feed, such as market prices or events, and reacts to each message.

Let only signed-in users connect

Check the visitor's login before the live connection opens, and turn strangers away with "401 Unauthorized".

Close dead connections

Find visitors whose phone lost signal or whose laptop went to sleep, and free the server's memory for them.

Show an AI agent's progress live Idea

While a long task runs on your server, send each step to the page so the person sees it moving instead of a spinner.

ws popularity

82 / 100
Gid Score, #64 overall
427M
downloads a week, npm
22.8K
GitHub stars
204
agent projects use it
ws: downloads per weeknpm, ws 427,109,592
ws: GitHub starswebsockets/ws 22,838

Gid Score measures public popularity and activity, not quality. How the Gid Score is calculated

How ws is used

  • ws is a dependency in 204 of the 6,258 public GitHub projects set up for AI coding agents that we scanned (4.2% of the 4,867 JavaScript/TypeScript ones), #4 of 126 in http clients and networking.
  • Counting the projects our scan samples by dependency (Solana 3,067, Ethereum and EVM 1,774, 3D and games 3,130, agent frameworks and SDKs 13,750), it appears in 902; those are left out of every share on this page.
  • The most used alternative in our scan is Socket.IO Client (69 projects).
  • Projects that use it most often also use tsx (57.4%), dotenv (52.5%), Zod (64.7%).
  • It is more common in projects set up for OpenClaw (16.5%) than for Cursor (2.7%).

By coding agent

144 projects, 4.4%
89 projects, 2.9%
66 projects, 2.7%
55 projects, 16.5%
33 projects, 7.0%
17 projects, 3.6%
15 projects, 5.4%
14 projects, 4.1%

Often used with

In 57.4% of its projects
In 52.5% of its projects
In 64.7% of its projects
In 41.2% of its projects
In 58.3% of its projects
In 85.8% of its projects
In 32.8% of its projects
In 31.9% of its projects

ws in practice

Strengths

Autobahn reports for server and client

The README says ws passes the "quite extensive" Autobahn test suite. It links one published report for the server and a separate one for the client.

Several endpoints on one HTTP server

With <code>noServer: true</code>, the HTTP server's <code>upgrade</code> event picks which WebSocket server takes each connection. In the README this is done by pathname, and anything unmatched gets <code>socket.destroy()</code>.

Reject before the handshake

The README's client-authentication example checks the request inside the <code>upgrade</code> handler. On failure it writes a 401 and destroys the socket. Only authenticated requests reach <code>handleUpgrade</code>, and the authenticated client is passed along to the <code>connection</code> event.

Opt-in native addon

<code>bufferutil</code> is an optional module that can be installed alongside ws. The README says it improves the performance of certain operations, such as masking and unmasking frame payloads. It also says prebuilt binaries mean you don't necessarily need a C++ compiler.

Watch out for

Node.js only

Browser clients must use the native <code>WebSocket</code>. For code that runs in both places, the README points to wrappers on npm such as <code>isomorphic-ws</code>.

Different compression defaults per side

permessage-deflate is off by default on the server and on by default on the client. The README says it adds "significant overhead", and the maintainers write that they suggest enabling it "only if it is really needed".

zlib under concurrency

The README warns that Node.js compression can lead to "catastrophic memory fragmentation" and slow performance with more concurrency, especially on Linux. It advises a workload-representative test before using compression in production.

Simple server

import { WebSocketServer } from 'ws';

const wss = new WebSocketServer({ port: 8080 });

wss.on('connection', function connection(ws) {
  ws.on('error', console.error);

  ws.on('message', function message(data) {
    console.log('received: %s', data);
  });

  ws.send('something');
});

More about ws

Funding
  • GitHub Sponsors: https://github.com/lpinca
  • 7 GitHub sponsors

See all

Head to head
Packages around it
  • tsx: TypeScript runner
  • express: Web framework
  • typescript: Typed JavaScript
  • zod: Schema validation
  • dotenv: Env file loader
  • vitest: Test runner
More in HTTP clients and networking

Alternatives

ws This page

MITGid 82427M/wk

Socket.IO

Pick Socket.IO when you want rooms, automatic reconnection and fallback transports built in, and can use its own client library.

MITGid 7926M/wk

websockets

Pick websockets when your server is written in Python instead of Node.js.

aiohttp

Pick aiohttp when you want one Python async framework for both web pages and WebSocket connections.

Socket.IO Client

Real-time bidirectional communication library for JavaScript with fallbacks.

MITGid 8022.3M/wk

Compare ws with another tool

Get started

Tell your AI assistant

“Use the npm package ws (pin ws@8.22.2, Node.js) for the WebSocket server; import { WebSocketServer } from 'ws', check the login in the HTTP server's 'upgrade' event, set maxPayload, and use the browser's native WebSocket on the client.”

  1. Add ws to your Node.js project.

    npm install ws

    From github.com/websockets/ws/blob/master/README.md

  2. Start a server that greets each new connection and logs what it receives.

    import { WebSocketServer } from 'ws';
    const wss = new WebSocketServer({ port: 8080 });
    wss.on('connection', function connection(ws) {
      ws.on('error', console.error);
      ws.on('message', function message(data) {
        console.log('received: %s', data);
      });
      ws.send('something');
    });
    

    From github.com/websockets/ws/blob/master/README.md

  3. In the browser, connect with the built-in WebSocket object, not with ws.

    From github.com/websockets/ws/blob/master/README.md

Rules for your agent

Paste into AGENTS.md or CLAUDE.md
## ws
- Use ws only in Node.js code. Browser code must use the native `WebSocket` object.
- Register an `error` listener on each connection, as the README examples do (`ws.on('error', console.error)`).
- To serve several WebSocket endpoints or to authenticate on one HTTP server, create servers with `noServer: true`. Route or authenticate in the HTTP server's `upgrade` handler, then call `handleUpgrade` and emit `connection`.
- When broadcasting, send only to clients whose `readyState === WebSocket.OPEN`, and pass `{ binary: isBinary }` through.
- Keep `perMessageDeflate` at its server default (disabled) and enable it only if it is really needed. If you enable it, test memory and performance under a representative workload first.
- For broken-connection detection, follow the README heartbeat: mark clients alive on `pong`, `terminate()` ones that did not answer, and clear the interval on server `close`.
- Behind a proxy like NGINX, the README gives the `X-Forwarded-For` header as the de-facto standard for the client IP.

Mistakes to avoid

import WebSocket from 'ws'; new WebSocket.Server({ port: 8080 }) (from old CommonJS tutorials)
import { WebSocketServer } from 'ws'; new WebSocketServer({ port: 8080 })

WebSocket.Server is attached only in the CommonJS entry index.js; the ES module wrapper exports WebSocketServer as a named export and does not load index.js.

JSON.parse(data) or data === 'ping' in the 'message' handler, as in ws 7 code
const text = isBinary ? data : data.toString(); then JSON.parse(text)

Since 8.0.0 text messages arrive as Buffers and the handler gets an isBinary flag.

import WebSocket from 'ws' in front-end code (React, Vue, plain pages)
Use the browser's global WebSocket; keep ws on the server

ws is for Node.js only; its browser entry (browser.js) just throws an error telling you to use the native WebSocket.

Checking tokens with the verifyClient option
Authenticate in server.on('upgrade') and call wss.handleUpgrade only for valid users (noServer mode)

The docs discourage verifyClient and show the upgrade-event pattern instead.

Leaving maxPayload at its default and pinning an old 8.x release
Set maxPayload to what your messages need (for example 64 * 1024) and use 8.21.0 or newer

The default allows 100 MiB per message, and 8.20.1 and 8.21.0 fixed a memory disclosure bug and a memory exhaustion attack.

Questions

Can I use ws in a React or plain browser page?

No. In the browser use the built-in WebSocket object; ws is for Node.js servers and backend clients.

How do I keep strangers out?

Check the login in the HTTP server's 'upgrade' event and only then hand the connection to ws; the verifyClient option is discouraged.

How do I notice that a visitor is gone?

Send pings on a timer and terminate connections that did not answer with a pong since the last round.

Can it share a port with my Express or HTTP server?

Yes. Pass your server as the server option, or use noServer mode and route the 'upgrade' event yourself.

Can I use it in a paid product?

Yes, it is under the MIT license.