API Reference

ws

Simple and fast WebSocket client and server implementation for Node.js with permessage-deflate and streaming APIs.

Official Documentation ↗
CompatibilityThis documentation covers version v8. Check the official docs for the latest API surface.

Installation

npmnpm install ws
pnpmpnpm add ws

Methods

Simple and fast WebSocket client and server implementation for Node.js with permessage-deflate and streaming APIs.

Overview

ws is a no-frills, high-throughput WebSocket implementation for Node.js. The server attaches to its own port or an existing HTTP server, exposes every live socket through wss.clients, and emits connection, message, close, and error events per socket.

The client side mirrors the browser WebSocket while adding Node needs: custom headers, timeouts, and permessage-deflate control. Heartbeats built on ping and pong, graceful close codes, and maxPayload guards are the operational scaffolding most real deployments add on top.

Core Methods

The most frequently used entry points, with signatures and runnable examples:

  • WebSocketServer — Creates a server bound to a port or an existing HTTP server; connections arrive on the connection event.
  • "message" event / send — Receives each frame as a buffer plus an isBinary flag, and send transmits text or binary payloads.
  • WebSocket (client) — Connects out to a WebSocket endpoint with custom headers or origin as a browser-style client.
  • ping / pong heartbeat — Heartbeat pattern: server pings each socket, and sockets missing pong replies are terminated to free ghost connections.

Usage Notes

  • Authenticate during the upgrade request, reading headers or query tokens from req, before accepting any frames.
  • Use heartbeat ping intervals slightly shorter than your load balancer idle timeout so proxies keep sockets alive.
  • Choose explicit WebSocket close codes so clients can distinguish shutdown, errors, and protocol violations.

Common Pitfalls

  • Calling send on a half-closed socket throws; gate every send on readyState or catch per-socket errors.
  • Default heartbeat absence lets dead connections accumulate until the socket table exhausts memory.
  • permessage-deflate trades CPU for bandwidth; disable it for high-fan-out servers of small messages.

Security notes

Quoted verbatim from the official documentation; this is not a complete threat model. 来源:https://cdn.jsdelivr.net/gh/websockets/ws@latest/README.md · 取证方式:HTTP GET 官方 README/文档页(HTML 去标签) · 抓取时间:2026-11-07T13:53:32.236Z.

FAQ

How do I broadcast to all connected clients?
Iterate wss.clients and call send on each open socket, or maintain your own room sets. Check ws.readyState === ws.OPEN before sending, since clients can close mid-iteration.
Why is my data arriving as Buffer instead of string?
v8 hands message payloads to you as Buffer, ArrayBuffer, or an array of Buffers with an isBinary flag, covering both text and binary frames uniformly. Convert with data.toString() for text frames you know are textual.
How do I bound message size?
Set maxPayload on the server; frames whose payload exceeds it close the connection with an appropriate close code. The default protects memory, but tune it deliberately for your protocol.