实时应用程序必备的 6 种高级 WebSocket 模式
作为一位畅销书作家,我诚邀您在亚马逊上浏览我的作品。也别忘了在Medium上关注我,给予我支持。谢谢!您的支持对我意义非凡!
在现代 Web 开发领域,实时功能已从锦上添花变为必不可少。用户期望即时更新、无缝交互和响应迅速的界面。WebSocket 为这些功能提供了技术基础,它提供持久连接,实现客户端和服务器之间的双向通信。多年来,我一直在各种项目中应用 WebSocket 解决方案,现在我想分享六种强大的模式,它们可以提升您的实时应用程序的性能。
WebSocket 连接池
连接池是需要多个 WebSocket 连接的应用程序的关键模式。它无需为每个组件或功能创建新连接,而是允许您管理一组数量有限的连接,这些连接可以在应用程序内共享。
我记得之前在一家交易平台工作,连接开销导致了性能问题。通过实施连接池,我们在保持相同功能的同时,将服务器负载降低了 40%。
class WebSocketConnectionPool {
constructor(serverUrl, poolSize = 3) {
this.serverUrl = serverUrl;
this.poolSize = poolSize;
this.connections = [];
this.connectionIndex = 0;
this.initialize();
}
initialize() {
for (let i = 0; i < this.poolSize; i++) {
this.createConnection();
}
}
createConnection() {
const ws = new WebSocket(this.serverUrl);
ws.onopen = () => {
console.log(`Connection ${this.connections.length} established`);
};
ws.onerror = (error) => {
console.error('WebSocket error:', error);
// Handle reconnection
this.handleReconnection(this.connections.indexOf(ws));
};
this.connections.push(ws);
return ws;
}
getConnection() {
// Simple round-robin selection
const connection = this.connections[this.connectionIndex];
this.connectionIndex = (this.connectionIndex + 1) % this.poolSize;
return connection;
}
handleReconnection(index) {
setTimeout(() => {
if (index >= 0 && index < this.connections.length) {
this.connections[index] = this.createConnection();
}
}, 1000);
}
sendMessage(message) {
const connection = this.getConnection();
if (connection.readyState === WebSocket.OPEN) {
connection.send(JSON.stringify(message));
return true;
}
return false;
}
}
利用这个连接池,你可以在多个连接上分发消息,同时控制资源使用量。我发现这在消息量大的应用程序中尤其有用。
心跳机制
网络连接可能会悄无声息地中断。心跳机制会定期发送“ping”消息来验证连接是否仍然有效且运行正常。
class HeartbeatWebSocket {
constructor(url, heartbeatInterval = 30000) {
this.url = url;
this.heartbeatInterval = heartbeatInterval;
this.connection = null;
this.heartbeatTimer = null;
this.connect();
}
connect() {
this.connection = new WebSocket(this.url);
this.connection.onopen = () => {
console.log('Connection established');
this.startHeartbeat();
};
this.connection.onclose = () => {
console.log('Connection closed');
this.stopHeartbeat();
// Reconnect logic would go here
};
this.connection.onmessage = (event) => {
const message = JSON.parse(event.data);
if (message.type === 'pong') {
// Reset heartbeat timer on pong
this.resetHeartbeat();
} else {
// Handle regular messages
this.handleMessage(message);
}
};
}
startHeartbeat() {
this.heartbeatTimer = setInterval(() => {
if (this.connection.readyState === WebSocket.OPEN) {
this.connection.send(JSON.stringify({ type: 'ping' }));
// Set a timeout to detect missed pongs
this.pongTimeoutTimer = setTimeout(() => {
console.log('Pong not received, connection may be dead');
this.connection.close();
}, 5000);
}
}, this.heartbeatInterval);
}
resetHeartbeat() {
if (this.pongTimeoutTimer) {
clearTimeout(this.pongTimeoutTimer);
this.pongTimeoutTimer = null;
}
}
stopHeartbeat() {
if (this.heartbeatTimer) {
clearInterval(this.heartbeatTimer);
this.heartbeatTimer = null;
}
this.resetHeartbeat();
}
handleMessage(message) {
// Process regular application messages
console.log('Received message:', message);
}
send(message) {
if (this.connection.readyState === WebSocket.OPEN) {
this.connection.send(JSON.stringify(message));
}
}
}
在我开发的一款聊天应用中,我们发现移动网络经常会维持一些看似活跃但实际上无法传输数据的“僵尸”连接。添加心跳机制后,消息传递失败率降低了 95%。
重新联系策略
网络中断不可避免。完善的重连策略可确保您的应用程序在连接断开时能够平稳恢复。
class ReconnectingWebSocket {
constructor(url, options = {}) {
this.url = url;
this.options = {
maxReconnectAttempts: 10,
reconnectInterval: 1000,
maxReconnectInterval: 30000,
reconnectDecay: 1.5,
...options
};
this.reconnectAttempts = 0;
this.socket = null;
this.isConnecting = false;
this.messageQueue = [];
this.connect();
}
connect() {
if (this.isConnecting) return;
this.isConnecting = true;
this.socket = new WebSocket(this.url);
this.socket.onopen = () => {
console.log('Connection established');
this.isConnecting = false;
this.reconnectAttempts = 0;
// Send any queued messages
while (this.messageQueue.length > 0) {
const message = this.messageQueue.shift();
this.send(message);
}
if (this.onopen) this.onopen();
};
this.socket.onclose = (event) => {
if (!event.wasClean) {
this.attemptReconnect();
}
if (this.onclose) this.onclose(event);
};
this.socket.onerror = (error) => {
console.error('WebSocket error:', error);
this.socket.close();
if (this.onerror) this.onerror(error);
};
this.socket.onmessage = (event) => {
if (this.onmessage) this.onmessage(event);
};
}
attemptReconnect() {
if (this.reconnectAttempts >= this.options.maxReconnectAttempts) {
console.log('Max reconnect attempts reached');
return;
}
const delay = Math.min(
this.options.reconnectInterval * Math.pow(this.options.reconnectDecay, this.reconnectAttempts),
this.options.maxReconnectInterval
);
this.reconnectAttempts++;
console.log(`Reconnecting in ${delay}ms... (Attempt ${this.reconnectAttempts})`);
setTimeout(() => {
this.isConnecting = false;
this.connect();
}, delay);
}
send(message) {
if (this.socket && this.socket.readyState === WebSocket.OPEN) {
this.socket.send(typeof message === 'string' ? message : JSON.stringify(message));
return true;
} else {
this.messageQueue.push(message);
return false;
}
}
close() {
if (this.socket) {
this.socket.close();
}
}
}
此实现方案采用指数退避机制,以避免在服务中断期间服务器过载。我发现,妥善处理重新连接对于用户体验的流畅性和糟糕的用户体验至关重要,尤其是在网络连接不稳定的地区。
消息队列
当连接中断时,你需要一种策略来处理外发消息。消息队列会在连接断开期间存储消息,并在连接恢复后发送它们。
class QueuedWebSocket {
constructor(url) {
this.url = url;
this.socket = null;
this.queue = [];
this.connected = false;
this.maxQueueSize = 100;
this.connect();
}
connect() {
this.socket = new WebSocket(this.url);
this.socket.onopen = () => {
console.log('Connection established');
this.connected = true;
this.flushQueue();
};
this.socket.onclose = () => {
console.log('Connection closed');
this.connected = false;
// Reconnection logic would go here
};
this.socket.onerror = (error) => {
console.error('WebSocket error:', error);
};
this.socket.onmessage = (event) => {
// Handle incoming messages
this.handleMessage(JSON.parse(event.data));
};
}
send(message) {
const messageObject = {
id: this.generateId(),
timestamp: Date.now(),
content: message,
attempts: 0
};
if (this.connected && this.socket.readyState === WebSocket.OPEN) {
this.sendMessage(messageObject);
} else {
this.enqueueMessage(messageObject);
}
}
sendMessage(messageObject) {
messageObject.attempts++;
try {
this.socket.send(JSON.stringify(messageObject.content));
return true;
} catch (error) {
console.error('Failed to send message:', error);
this.enqueueMessage(messageObject);
return false;
}
}
enqueueMessage(messageObject) {
// Keep queue size manageable
if (this.queue.length >= this.maxQueueSize) {
this.queue.shift(); // Remove oldest message
}
this.queue.push(messageObject);
}
flushQueue() {
if (!this.connected) return;
const queueCopy = [...this.queue];
this.queue = [];
queueCopy.forEach(messageObject => {
if (!this.sendMessage(messageObject)) {
// If sending fails, it will be re-enqueued
}
});
}
handleMessage(message) {
console.log('Received message:', message);
// Process incoming message
}
generateId() {
return Math.random().toString(36).substring(2, 15);
}
}
在我参与开发的一款协作文档编辑器中,我们实现了消息队列机制,以确保即使在短暂的网络中断期间,用户的编辑内容也不会丢失。这显著提高了应用程序在实际使用环境中的可靠性。
协议定义
清晰的消息协议确保客户端和服务器之间能够完美地相互理解。定义结构化的协议可以使您的 WebSocket 通信更易于维护,并且更不容易出错。
// Protocol Definition
const MessageTypes = {
AUTHENTICATION: 'auth',
EVENT: 'event',
COMMAND: 'command',
QUERY: 'query',
RESPONSE: 'response',
ERROR: 'error'
};
class WebSocketProtocol {
constructor(url) {
this.url = url;
this.socket = null;
this.messageHandlers = {};
this.pendingRequests = new Map();
this.requestTimeout = 10000; // 10 seconds
this.connect();
}
connect() {
this.socket = new WebSocket(this.url);
this.socket.onopen = () => {
console.log('Connection established');
};
this.socket.onmessage = (event) => {
try {
const message = JSON.parse(event.data);
this.handleMessage(message);
} catch (error) {
console.error('Failed to parse message:', error);
}
};
this.socket.onclose = () => {
console.log('Connection closed');
};
this.socket.onerror = (error) => {
console.error('WebSocket error:', error);
};
}
handleMessage(message) {
// Validate message format
if (!message.type || !message.id) {
console.error('Invalid message format:', message);
return;
}
// Handle responses to requests
if (message.type === MessageTypes.RESPONSE && this.pendingRequests.has(message.requestId)) {
const { resolve } = this.pendingRequests.get(message.requestId);
resolve(message.payload);
this.pendingRequests.delete(message.requestId);
return;
}
// Handle errors
if (message.type === MessageTypes.ERROR && this.pendingRequests.has(message.requestId)) {
const { reject } = this.pendingRequests.get(message.requestId);
reject(new Error(message.error));
this.pendingRequests.delete(message.requestId);
return;
}
// Handle other message types
if (this.messageHandlers[message.type]) {
this.messageHandlers[message.type](message.payload, message);
} else {
console.warn('No handler for message type:', message.type);
}
}
sendMessage(type, payload, requestId = null) {
if (this.socket.readyState !== WebSocket.OPEN) {
return Promise.reject(new Error('WebSocket is not connected'));
}
const id = this.generateId();
const message = {
id,
type,
timestamp: Date.now(),
payload
};
if (requestId) {
message.requestId = requestId;
}
this.socket.send(JSON.stringify(message));
// If this is a query that expects a response, return a promise
if (type === MessageTypes.QUERY) {
return new Promise((resolve, reject) => {
this.pendingRequests.set(id, { resolve, reject });
// Set timeout for the request
setTimeout(() => {
if (this.pendingRequests.has(id)) {
this.pendingRequests.delete(id);
reject(new Error('Request timed out'));
}
}, this.requestTimeout);
});
}
return Promise.resolve();
}
on(messageType, handler) {
this.messageHandlers[messageType] = handler;
}
authenticate(credentials) {
return this.sendMessage(MessageTypes.AUTHENTICATION, credentials);
}
query(resource, parameters = {}) {
return this.sendMessage(MessageTypes.QUERY, { resource, parameters });
}
command(action, parameters = {}) {
return this.sendMessage(MessageTypes.COMMAND, { action, parameters });
}
publishEvent(event, data = {}) {
return this.sendMessage(MessageTypes.EVENT, { event, data });
}
generateId() {
return Math.random().toString(36).substring(2, 15);
}
}
此处所示的协议为消息添加了结构,包括类型、ID、时间戳和有效负载。它还支持请求-响应模式和错误处理。在我开发的一个金融应用程序中,使用定义完善的协议使集成过程中的错误减少了 70% 以上。
频道订阅
对于具有不同类型实时更新的应用,通道订阅允许客户端仅接收他们需要的数据,从而减少带宽使用和处理开销。
class ChannelWebSocket {
constructor(url) {
this.url = url;
this.socket = null;
this.subscriptions = new Map();
this.reconnectAttempts = 0;
this.maxReconnectAttempts = 10;
this.connect();
}
connect() {
this.socket = new WebSocket(this.url);
this.socket.onopen = () => {
console.log('Connection established');
this.reconnectAttempts = 0;
// Resubscribe to all channels after reconnection
for (const [channel, callback] of this.subscriptions.entries()) {
this.sendSubscription(channel);
}
};
this.socket.onmessage = (event) => {
try {
const message = JSON.parse(event.data);
this.handleMessage(message);
} catch (error) {
console.error('Failed to parse message:', error);
}
};
this.socket.onclose = () => {
console.log('Connection closed');
if (this.reconnectAttempts < this.maxReconnectAttempts) {
this.reconnectAttempts++;
const delay = Math.min(1000 * Math.pow(1.5, this.reconnectAttempts), 30000);
setTimeout(() => this.connect(), delay);
}
};
this.socket.onerror = (error) => {
console.error('WebSocket error:', error);
};
}
handleMessage(message) {
if (!message.channel || !message.data) {
console.warn('Received malformed message:', message);
return;
}
// Forward message to subscribers
if (this.subscriptions.has(message.channel)) {
const callback = this.subscriptions.get(message.channel);
callback(message.data);
}
// Handle system messages
if (message.channel === 'system') {
this.handleSystemMessage(message.data);
}
}
handleSystemMessage(data) {
if (data.type === 'subscription_confirm') {
console.log(`Subscription to ${data.channel} confirmed`);
} else if (data.type === 'error') {
console.error('System error:', data.message);
}
}
subscribe(channel, callback) {
if (typeof callback !== 'function') {
throw new Error('Callback must be a function');
}
this.subscriptions.set(channel, callback);
if (this.socket.readyState === WebSocket.OPEN) {
this.sendSubscription(channel);
}
return {
unsubscribe: () => this.unsubscribe(channel)
};
}
unsubscribe(channel) {
if (!this.subscriptions.has(channel)) {
return false;
}
this.subscriptions.delete(channel);
if (this.socket.readyState === WebSocket.OPEN) {
this.socket.send(JSON.stringify({
action: 'unsubscribe',
channel
}));
}
return true;
}
sendSubscription(channel) {
this.socket.send(JSON.stringify({
action: 'subscribe',
channel
}));
}
publish(channel, data) {
if (this.socket.readyState !== WebSocket.OPEN) {
return false;
}
this.socket.send(JSON.stringify({
action: 'publish',
channel,
data
}));
return true;
}
}
这种模式对于具有多个数据源的仪表盘应用尤其有效。在我构建的一个监控系统中,通过实现通道订阅,用户可以只接收他们正在查看的组件的更新,从而将 WebSocket 流量减少了 80%。
WebSocket 的扩展性和性能考量
随着应用程序的增长,扩展 WebSocket 变得至关重要。以下是我成功应用的一些策略:
使用支持 WebSocket 的负载均衡器(例如 NGINX 或 HAProxy)进行水平扩展:
upstream websocket_servers {
hash $remote_addr consistent;
server backend1.example.com:8080;
server backend2.example.com:8080;
server backend3.example.com:8080;
}
server {
listen 80;
server_name ws.example.com;
location /ws/ {
proxy_pass http://websocket_servers;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_read_timeout 3600s;
proxy_send_timeout 3600s;
}
}
对于 Node.js 服务器端实现,我经常使用 ws 库和 Redis 来进行跨多个实例的消息广播:
const WebSocket = require('ws');
const Redis = require('ioredis');
const http = require('http');
// Create Redis clients
const subscriber = new Redis();
const publisher = new Redis();
// Create HTTP server
const server = http.createServer();
// Create WebSocket server
const wss = new WebSocket.Server({ server });
// Store connected clients and their subscriptions
const clients = new Map();
wss.on('connection', (ws) => {
const clientId = generateId();
const clientData = {
id: clientId,
subscriptions: new Set()
};
clients.set(ws, clientData);
console.log(`Client ${clientId} connected`);
ws.on('message', (message) => {
try {
const data = JSON.parse(message);
switch (data.action) {
case 'subscribe':
handleSubscribe(ws, clientData, data.channel);
break;
case 'unsubscribe':
handleUnsubscribe(ws, clientData, data.channel);
break;
case 'publish':
handlePublish(data.channel, data.data);
break;
default:
console.warn(`Unknown action: ${data.action}`);
}
} catch (error) {
console.error('Error processing message:', error);
}
});
ws.on('close', () => {
// Clean up subscriptions
clientData.subscriptions.forEach(channel => {
subscriber.unsubscribe(channel);
});
clients.delete(ws);
console.log(`Client ${clientId} disconnected`);
});
});
// Handle subscribe action
function handleSubscribe(ws, clientData, channel) {
// Subscribe to Redis channel
subscriber.subscribe(channel);
// Add to client's subscriptions
clientData.subscriptions.add(channel);
// Confirm subscription
ws.send(JSON.stringify({
channel: 'system',
data: {
type: 'subscription_confirm',
channel
}
}));
}
// Handle unsubscribe action
function handleUnsubscribe(ws, clientData, channel) {
// Remove from client's subscriptions
clientData.subscriptions.delete(channel);
// Check if we need to unsubscribe from Redis
let hasOtherSubscribers = false;
clients.forEach(client => {
if (client.subscriptions.has(channel)) {
hasOtherSubscribers = true;
}
});
if (!hasOtherSubscribers) {
subscriber.unsubscribe(channel);
}
}
// Handle publish action
function handlePublish(channel, data) {
// Publish to Redis
publisher.publish(channel, JSON.stringify(data));
}
// Process messages from Redis
subscriber.on('message', (channel, message) => {
// Broadcast to all clients subscribed to this channel
clients.forEach((clientData, ws) => {
if (clientData.subscriptions.has(channel) && ws.readyState === WebSocket.OPEN) {
ws.send(JSON.stringify({
channel,
data: JSON.parse(message)
}));
}
});
});
function generateId() {
return Math.random().toString(36).substring(2, 15);
}
// Start server
const PORT = process.env.PORT || 8080;
server.listen(PORT, () => {
console.log(`WebSocket server listening on port ${PORT}`);
});
这种实现方式允许多个服务器实例通过 Redis 共享 WebSocket 消息,从而实现横向扩展。当我在一个大型电商网站上实施这种模式时,我们能够在六个服务器实例上支持超过 5 万个并发 WebSocket 连接。
结论
这六种 WebSocket 模式在我的开发经验中被证明非常宝贵。连接池可以高效地管理资源,心跳机制可以确保连接健康,重连策略可以处理网络中断,消息队列可以防止数据丢失,协议定义可以规范通信,通道订阅可以优化网络使用。
提供的代码示例均经过生产环境的实战检验,可根据您的具体需求进行调整。请记住,最佳实现方案取决于您的具体需求——请考虑预期用户数量、消息发送频率以及实时更新的重要性等因素。
通过应用这些模式,您可以创建功能丰富、健壮、可扩展且易于维护的 WebSocket 实现。最终,您将获得即使在网络环境恶劣的情况下也能提供卓越用户体验的实时 Web 应用程序。
101本书
101 Books是一家人工智能驱动的出版公司,由作家Aarav Joshi联合创办。通过利用先进的人工智能技术,我们将出版成本控制在极低的水平——有些书的价格低至4 美元——让每个人都能轻松获取优质知识。
欢迎查看我们在亚马逊上发售的《Golang Clean Code》一书。
敬请关注后续更新和精彩资讯。选购书籍时,搜索Aarav Joshi即可找到更多我们的作品。点击提供的链接即可享受专属折扣!
我们的创作
请务必查看我们的作品:
投资者中心|投资者中心西班牙语版|投资者中心德语版|智慧生活|时代与回响|谜题之谜|印度教|精英开发| JS学校
我们在 Medium 上。
Tech Koala Insights | Epochs & Echoes World | Investor Central Medium | Puzzling Mysteries Medium | Science & Epochs Medium | Modern Hindutva
文章来源:https://dev.to/nithinbharathwaj/6-essential-websocket-patterns-for-real-time-applications-39gf