@azure/web-pubsub-socket.io

中文文档
1.2.1发布于 2026/04/22MITTypeScriptESM + CJS近 30 天下载 3.3万

Enable Socket.IO server support Azure Web PubSub Service

原始使用文档
文档目录10 节
  1. Get Started
  2. Create a Web PubSub for Socket.IO resource
  3. Initialize a Node project and install required packages
  4. Write server code
  5. Write client code
  6. Run the app
  7. Debug
  8. Building library from source
  9. Unit Test
  10. Contributing

Introduction

This package is the extension library to the Socket.IO Server SDK. Using this library together with the Web PubSub For Socket.IO Service enables the Azure service to manage clients at scale and keep Socket.IO's programming experience.

Web PubSub For Socket.IO works as a broker between clients and the Socket.IO server. It handles connection management and broadcasting messages at scale and provide scalability and reliability experience. With this library, you don't need to introduce and manage an extra Adapter to support multi-server environment.

Get Started

The following steps show you how to create a Web PubSub for Socket.IO resource and use this library to enable your Socket.IO server to work together with the service. For more details step of how to get started with Web PubSub for Socket.IO, please refer to Get started with Web PubSub for Socket.IO.

Create a Web PubSub for Socket.IO resource

Use following button to create a Web PubSub for Socket.IO resource in Azure.

Deploy to Azure

Initialize a Node project and install required packages

mkdir quickstart
cd quickstart
npm init
npm install @azure/web-pubsub-socket.io socket.io-client

Write server code

Create a server.js file and add following code to create a Socket.IO server and integrate with Web PubSub for Socket.IO.

/*server.js*/
const { Server } = require("socket.io");
const { useAzureSocketIO } = require("@azure/web-pubsub-socket.io");

let io = new Server(3000);

// Use the following line to integrate with Web PubSub for Socket.IO
useAzureSocketIO(io, {
    hub: "Hub", // The hub name can be any valid string.
    connectionString: process.argv[2]
});

io.on("connection", (socket) => {
    // Sends a message to the client
    socket.emit("hello", "world");

    // Receives a message from the client
    socket.on("howdy", (arg) => {
        console.log(arg);   // Prints "stranger"
    })
});

Write client code

Create a client.js file and add following code to connect the client with Web PubSub for Socket.IO.

/*client.js*/
const io = require("socket.io-client");

const socket = io("<web-pubsub-socketio-endpoint>", {
    path: "/clients/socketio/hubs/Hub",
});

// Receives a message from the server
socket.on("hello", (arg) => {
    console.log(arg);
});

// Sends a message to the server
socket.emit("howdy", "stranger")

When you use Web PubSub for Socket.IO, <web-pubsub-socketio-endpoint> and path are required for the client to connect to the service. The <web-pubsub-socketio-endpoint> and path can be found in Azure portal.

  1. Go to the key blade of Web PubSub for Socket.IO

  2. Type in your hub name and copy the Client Endpoint and Client Path

    Get client path

Run the app

  1. Run the server app:

    node server.js "<connection-string>"
    

    The <connection-string> is the connection string that contains the endpoint and keys to access your Web PubSub for Socket.IO resource. You can also find the connection string in Azure portal

    Get connection string

  2. Run the client app in another terminal:

    node client.js
    

Debug

  1. Determine which packages' logs you want to see.

  2. Set environmental variable DEBUG in your shell according to your wanted packages. Here is a PowerShell examples:

# Show log from this package
$Env:DEBUG='wps-sio-ext*'

# Show log from this package + Engine.IO
$Env:DEBUG='wps-sio-ext*,engine*'

# Show log from this package + Engine.IO + Socket.IO
$Env:DEBUG='wps-sio-ext*,engine*,socket.io:*'

# Show logs from all packages
$Env:DEBUG='*'

Building library from source

The @azure/web-pubsub-socket.io library depends on server-proxies library, so you need to build server-proxies first.

Build server-proxies:

cd ../server-proxies
yarn install
yarn build

Build @azure/web-pubsub-socket.io

# navigate into webpubsub-socketio-extension folder
yarn install
yarn run build

Unit Test

  1. Rename .env.test.example to .env.test. And update the WebPubSubConnectionString inside:
WebPubSubConnectionString="<web-pubsub-connection-string>"
WebPubSubHub="eio_hub"
SocketIoPort=3000
  1. Run command
yarn test:unit

Contributing

This project welcomes contributions and suggestions. Most contributions require you to agree to a Contributor License Agreement (CLA) declaring that you have the right to, and actually do, grant us the rights to use your contribution. For details, visit https://cla.microsoft.com.

When you submit a pull request, a CLA-bot will automatically determine whether you need to provide a CLA and decorate the PR appropriately (e.g., label, comment). Simply follow the instructions provided by the bot. You will only need to do this once across all repos using our CLA.

This project has adopted the Microsoft Open Source Code of Conduct. For more information see the Code of Conduct FAQ or contact [email protected] with any additional questions or comments.

版本与发布记录

@azure/web-pubsub-socket.io 共发布 10 个版本,首次发布于 2023/08/01;过去 12 个月发布了 1 个版本。

版本发布日期距今
1.2.1latest2026/04/226 个月前
1.2.02024/07/092 年前
1.1.02024/03/192 年前
1.0.12023/11/172 年前
1.0.02023/11/162 年前
1.0.0-beta.62023/09/213 年前

其他发布通道:next → 1.0.0-alpha.20230803.1。

关于 @azure/web-pubsub-socket.io 的常见问题

@azure/web-pubsub-socket.io 是什么?

Enable Socket.IO server support Azure Web PubSub Service

如何安装 @azure/web-pubsub-socket.io?

在项目目录运行 npm install @azure/web-pubsub-socket.io,或使用 pnpm add / yarn add / bun add。

@azure/web-pubsub-socket.io 支持 TypeScript 吗?

支持。包内自带 TypeScript 类型声明(package.json 声明了 types 入口),安装后即可获得类型提示,无需再安装 @types 包。

@azure/web-pubsub-socket.io 支持 ESM 还是 CommonJS?

同时提供 ES Module 与 CommonJS 入口,import 和 require() 都可以使用,打包工具会自动选择合适的版本。

@azure/web-pubsub-socket.io 需要什么版本的 Node.js?

package.json 的 engines 字段要求 Node.js >=10.0.0。版本不符时 npm 默认只给出警告,开启 engine-strict 后会拒绝安装。

@azure/web-pubsub-socket.io 可以商用吗?

@azure/web-pubsub-socket.io 使用 MIT 许可。宽松许可:允许商业使用、修改和再分发,通常只需在分发时保留原版权与许可声明。以上为概括,不构成法律意见,请以许可原文为准。

@azure/web-pubsub-socket.io 的下载量有多少?

根据 npm 官方下载统计,@azure/web-pubsub-socket.io 在 2026/10/08 – 2026/11/06 这 30 天内被下载 33,345 次,日均约 1,112 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@azure/web-pubsub-socket.io 还在维护吗?

最新版本 1.2.1 发布于 2026/04/22(6 个月前);至今共发布 10 个版本,其中过去 12 个月发布了 1 个;首次发布于 2023/08/01。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。