Skip to main content
Version: 4.x

教程步骤 #9 - 水平缩放



英:Now that our application is resilient to temporary network interruptions, let's see how we can horizontally scale it in order to be able to support thousands of concurrent clients.

  • 水平扩展(也称为 "横向扩展")意味着向你的基础架构添加新服务器以应对新需求
  • 垂直扩展(也称为 "扩大")意味着向现有基础设施添加更多资源(处理能力、内存、存储……)

第一步: 让我们使用主机的所有可用核心。 默认情况下,Node.js 在单线程中运行 Javascript 代码,这意味着即使使用 32 核 CPU,也只会使用一个核心。 幸运的是,Node.js cluster 模块 提供了一种为每个核心创建一个工作线程的便捷方法。

英:First step: let's use all the available cores of the host. By default, Node.js runs your Javascript code in a single thread, which means that even with a 32-core CPU, only one core will be used. Fortunately, the Node.js cluster module provides a convenient way to create one worker thread per core.

我们还需要一种在 Socket.IO 服务器之间转发事件的方法。 我们将此组件称为 "适配器"。

英:We will also need a way to forward events between the Socket.IO servers. We call this component an "Adapter".

The 'hello' event is forwarded to the other serversThe 'hello' event is forwarded to the other servers


英:So let's install the cluster adapter:

npm install


英:Now we plug it in:

const express = require('express');
const { createServer } = require('node:http');
const { join } = require('node:path');
const { Server } = require('');
const sqlite3 = require('sqlite3');
const { open } = require('sqlite');
const { availableParallelism } = require('node:os');
const cluster = require('node:cluster');
const { createAdapter, setupPrimary } = require('');

if (cluster.isPrimary) {
const numCPUs = availableParallelism();
// create one worker per available core
for (let i = 0; i < numCPUs; i++) {
PORT: 3000 + i

// set up the adapter on the primary thread
return setupPrimary();

async function main() {
const app = express();
const server = createServer(app);
const io = new Server(server, {
connectionStateRecovery: {},
// set up the adapter on each worker thread
adapter: createAdapter()

// [...]

// each worker will listen on a distinct port
const port = process.env.PORT;

server.listen(port, () => {
console.log(`server running at http://localhost:${port}`);


就是这样! 这将为你机器上可用的每个 CPU 生成一个工作线程。 让我们看看它的实际效果:

英:That's it! This will spawn one worker thread per CPU available on your machine. Let's see it in action:


正如你在地址栏中看到的,每个浏览器选项卡都连接到不同的 Socket.IO 服务器,并且适配器只是在它们之间转发 chat message 事件。

英:As you can see in the address bar, each browser tab is connected to a different Socket.IO server, and the adapter is simply forwarding the chat message events between them.


目前有 5 个官方适配器实现:

英:There are currently 5 official adapter implementations:

因此,你可以选择最适合你需求的一种。 但请注意,某些实现不支持连接状态恢复功能,你可以找到兼容性矩阵 此处

英:So you can choose the one that best suits your needs. However, please note that some implementations do not support the Connection state recovery feature, you can find the compatibility matrix here.


在大多数情况下,你还需要确保 Socket.IO 会话的所有 HTTP 请求都到达同一服务器(也称为 "粘性会话")。 不过,这里不需要这样做,因为每个 Socket.IO 服务器都有自己的端口。

英:In most cases, you would also need to ensure that all the HTTP requests of a Socket.IO session reach the same server (also known as "sticky session"). This is not needed here though, as each Socket.IO server has its own port.

更多信息在 此处

英:More information here.

我们的聊天应用终于完成了! 在本教程中,我们了解了如何:

英:And that finally completes our chat application! In this tutorial, we have seen how to:

  • 在客户端和服务器之间发送事件
  • 将事件广播给所有或部分连接的客户端
  • 处理临时断线
  • 放大

你现在应该对 Socket.IO 提供的功能有了更好的了解。 现在是你构建自己的实时应用的时候了!

英:You should now have a better overview of the features provided by Socket.IO. Now it's your time to build your own realtime application!



英:You can run this example directly in your browser on: