websockets
Build real-time features with WebSockets: connection lifecycle, protocols, backpressure, scaling, and reconnection. Use for live updates and chat.
WebSockets
Building real-time, bidirectional communication with WebSockets.
Quick Start
npm i ws
node server.js # ws://localhost:8080When to Use
- Live updates (chat, notifications, dashboards)
- Collaborative editing and presence
- Streaming data and progress
- Bidirectional client-server interactions
Best Practices
Protocol Design
- Define message envelopes with a type + payload
- Version the message protocol
- Use JSON or a binary format consistently
- Include correlation/request IDs
Connection Lifecycle
- Handle open, message, close, and error events
- Implement ping/pong keepalive
- Detect half-open connections
- Clean up resources on close
Backpressure & Flow
- Respect slow consumers; don't buffer unbounded
- Use queues with limits and drop policies
- Batch high-frequency updates
- Pause/close on saturation
Scaling
- Share state via pub/sub (Redis) across nodes
- Use sticky sessions or a gateway
- Reconnect with exponential backoff client-side
- Handle authentication at handshake
Dependencies
npm i ws
# Python: pip install websockets
# Node: socket.io for fallback/roomsExamples
// Minimal WebSocket server (Node)
import { WebSocketServer } from "ws";
const wss = new WebSocketServer({ port: 8080 });
wss.on("connection", (ws) => {
ws.send(JSON.stringify({ type: "hello", data: "welcome" }));
ws.on("message", (raw) => {
const msg = JSON.parse(raw.toString());
broadcast({ type: "chat", data: msg });
});
});
function broadcast(obj) {
const text = JSON.stringify(obj);
wss.clients.forEach((c) => c.readyState === 1 && c.send(text));
}# Python server with websockets
import asyncio, websockets
async def handler(ws):
await ws.send("hello")
async for raw in ws:
await ws.send("echo: " + raw)
async def main():
async with websockets.serve(handler, "localhost", 8080):
await asyncio.Future()
asyncio.run(main())// Client with reconnect + backoff
function connect(url) {
const ws = new WebSocket(url);
let delay = 500;
ws.onclose = () => setTimeout(() => connect(url), delay);
ws.onopen = () => { delay = 500; };
return ws;
}// Keepalive ping/pong
const interval = setInterval(() => {
if (ws.readyState === 1) {
ws.ping();
if (!ws.isAlive) return ws.terminate();
ws.isAlive = false;
}
}, 30000);Step-by-Step
1. Define the message envelope and event types.
2. Set up the server with connection handling.
3. Add keepalive (ping/pong) and reconnection.
4. Implement broadcast and room/scope routing.
5. Add backpressure handling for slow clients.
6. Auth at the handshake (token/cookie).
7. Scale with a pub/sub backbone for multi-node.
8. Monitor connections, messages, and errors.
Validation
1. Messages round-trip bidirectionally
2. Reconnection resumes without data loss
3. Keepalive detects dead connections
4. Slow consumers don't stall the server
5. Multi-node broadcast is consistent
Troubleshooting
- Connections drop: add keepalive and check proxy timeouts.
- Order issues: use sequence numbers or a single writer per topic.
- Scale limits: move state to pub/sub and add a gateway.
WebSockets (ВебСокеты)
Двунаправленная связь в реальном времени через WebSockets.
Быстрый старт
npm i ws
node server.js # ws://localhost:8080Когда использовать
- Live-обновления (чат, уведомления, дашборды)
- Совместное редактирование и presence
- Стриминг данных и прогресс
- Двунаправленные взаимодействия клиент-сервер
Лучшие практики
Дизайн протокола
- Конверты сообщений: type + payload
- Версионируйте протокол сообщений
- Консистентный JSON или бинарный формат
- Correlation/request ID
Жизненный цикл соединения
- Обрабатывайте open, message, close, error
- Keepalive через ping/pong
- Детектируйте half-open соединения
- Чистите ресурсы при закрытии
Backpressure и поток
- Уважайте медленных потребителей; без бесконечных буферов
- Очереди с лимитами и политикой дропа
- Батчите частые обновления
- Пауза/закрытие при насыщении
Масштабирование
- Общее состояние через pub/sub (Redis) между нодами
- Sticky sessions или gateway
- Реconnect с экспоненциальным backoff на клиенте
- Auth на этапе handshake
Зависимости
npm i ws
# Python: pip install websockets
# Node: socket.io для fallback/roomsПримеры
// Минимальный WebSocket сервер (Node)
import { WebSocketServer } from "ws";
const wss = new WebSocketServer({ port: 8080 });
wss.on("connection", (ws) => {
ws.send(JSON.stringify({ type: "hello", data: "welcome" }));
ws.on("message", (raw) => {
const msg = JSON.parse(raw.toString());
broadcast({ type: "chat", data: msg });
});
});
function broadcast(obj) {
const text = JSON.stringify(obj);
wss.clients.forEach((c) => c.readyState === 1 && c.send(text));
}# Python сервер через websockets
import asyncio, websockets
async def handler(ws):
await ws.send("hello")
async for raw in ws:
await ws.send("echo: " + raw)
async def main():
async with websockets.serve(handler, "localhost", 8080):
await asyncio.Future()
asyncio.run(main())// Клиент с реконнектом и backoff
function connect(url) {
const ws = new WebSocket(url);
let delay = 500;
ws.onclose = () => setTimeout(() => connect(url), delay);
ws.onopen = () => { delay = 500; };
return ws;
}// Keepalive ping/pong
const interval = setInterval(() => {
if (ws.readyState === 1) {
ws.ping();
if (!ws.isAlive) return ws.terminate();
ws.isAlive = false;
}
}, 30000);Пошаговое руководство
1. Определите конверт сообщений и типы событий.
2. Настройте сервер с обработкой соединений.
3. Добавьте keepalive (ping/pong) и реконнект.
4. Реализуйте broadcast и роутинг по комнатам/скоупам.
5. Добавьте обработку backpressure для медленных клиентов.
6. Auth на handshake (token/cookie).
7. Масштабируйте через pub/sub backbone для multi-node.
8. Мониторьте соединения, сообщения и ошибки.
Валидация
1. Сообщения проходят в обе стороны
2. Реконнект возобновляется без потерь
3. Keepalive детектирует мёртвые соединения
4. Медленные потребители не стопорят сервер
5. Multi-node broadcast консистентен
Устранение неполадок
- Соединения падают: добавьте keepalive и проверьте таймауты прокси.
- Проблемы порядка: sequence numbers или один писатель на топик.
- Пределы масштаба: состояние в pub/sub и gateway.