The ws package implements WebSocket client and server roles for Node.js. It is not for browser runtimes; front ends should use the native WebSocket API or an isomorphic wrapper.
Overview
ws provides WebSocket client and server implementations aimed at Node.js backends. It covers HyBi draft versions and defaults to the current RFC 6455-era protocol level, with optional settings for older drafts.
The library passes the Autobahn conformance suite for both server and client roles. Documentation describes classes and helpers in a Node-style reference, with examples for text and binary messaging, standalone servers, attachment to external HTTP or HTTPS servers, shared HTTP servers, authentication, and broadcast patterns.
Optional permessage-deflate compression can be negotiated between endpoints. On the server the extension is off by default; on the client it is on by default, with guidance that compression adds memory and CPU cost. Older Node releases can load optional native add-ons for masking and UTF-8 validation; newer Node builds use built-in alternatives when available.
Examples also cover round-trip timing, the Node streams API, detecting stale connections with ping and pong, reading client addresses from sockets or proxy headers, and connecting through HTTP or SOCKS proxies via custom agents. Browser clients are out of scope for this module itself.
Key features
- WebSocket client and server for Node.js
- Autobahn test suite coverage
- Permessage-deflate compression support
- Attach to existing HTTP or HTTPS servers
- Broadcast and authentication patterns
- Ping and pong for liveness checks
- Streams API integration
- Proxy connection via custom agents
Use cases
- Realtime backends on Node.js
- Shared HTTP server with WebSocket upgrade
- Binary and text message channels
- Server-wide event broadcast
- Low-level WebSocket protocol control