🌐
HTTP20M+/wkMIT

ws

ws is the most widely used WebSocket implementation for Node.js, providing a simple, fast, and thoroughly tested client and server. Unlike Socket.IO which adds

Installation

npm
npm install ws
yarn
yarn add ws
pnpm
pnpm add ws

Import

ESM
import { WebSocketServer } from 'ws';

Quick Example

usage
import { WebSocketServer } from 'ws';

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

wss.on('connection', (ws) => {
  ws.on('message', (data) => {
    ws.send(`Echo: ${data}`);
  });
});

About ws

ws is the most widely used WebSocket implementation for Node.js, providing a simple, fast, and thoroughly tested client and server. Unlike Socket.IO which adds its own protocol layer on top of WebSockets, ws implements the raw WebSocket protocol (RFC 6455) directly, making it compatible with any WebSocket client including browser-native WebSocket objects. The library provides both a WebSocket server that can run standalone or be attached to an existing HTTP/HTTPS server, and a WebSocket client for connecting to WebSocket endpoints. ws supports binary messages, ping/pong for connection health checking, per-message deflate compression, and connection upgrade handling. The server tracks connected clients and supports custom upgrade authentication logic for verifying connections before accepting them. ws is intentionally minimal — it does not include reconnection logic, room management, or message serialization, leaving those concerns to application code or higher-level libraries. This simplicity and protocol compliance make ws the foundation for many Node.js WebSocket libraries and frameworks. It is the recommended choice when you need raw WebSocket performance without additional protocol overhead.

Quick Facts

Packagews
CategoryHTTP
Weekly Downloads20M+
LicenseMIT
Installnpm install ws

Related Packages

Browse npm Packages by Category

Explore our reference of 200 popular npm packages with install commands, examples, and quick-start guides.