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/LICENSE3. Used by agents' projects
Dependency in 204 public GitHub projects set up for AI coding agents that we scanned (3.3%).
4. Agent access
Agent skills for this library in the skills catalog; community skills are written by others, read them before you install.
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
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
Often used with
ws in practice
Strengths
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.
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>.
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.
<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
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>.
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".
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
Head to head
- ws vs Socket.IO Client used together in 10 projects
- ws vs Socket.IO used together in 9 projects
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
Pick Socket.IO when you want rooms, automatic reconnection and fallback transports built in, and can use its own client library.
Pick websockets when your server is written in Python instead of Node.js.
Pick aiohttp when you want one Python async framework for both web pages and WebSocket connections.
Real-time bidirectional communication library for JavaScript with fallbacks.
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.”
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'); });In the browser, connect with the built-in WebSocket object, not with ws.
Rules for your agent
## 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
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.
Since 8.0.0 text messages arrive as Buffers and the handler gets an isBinary flag.
ws is for Node.js only; its browser entry (browser.js) just throws an error telling you to use the native WebSocket.
The docs discourage verifyClient and show the upgrade-event pattern instead.
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.