一个透明代理,能够将redis channel的消息通过socket.io 透明传输到网页。使用socket.io v4.8.1 和ioredis v5.7.0,使网页客户端可以使用sub命令订阅后端redis服务器上的消息通道。
- Node.js: 18.0.0 或更高版本
- Redis 服务器: 本地或远程
在Windows环境下,可以通过以下方式安装Redis:
-
使用Windows安装包:
- 访问 Redis for Windows 下载最新版本
- 运行安装程序,按照向导完成安装
- 安装完成后,Redis服务会自动启动
-
使用Docker:
- 安装 Docker Desktop for Windows
- 运行以下命令启动Redis容器:
docker run --name redis -p 6379:6379 -d redis:alpine
-
使用WSL2(Windows Subsystem for Linux):
- 安装WSL2
- 在WSL2中安装Redis:
sudo apt update sudo apt install redis-server sudo service redis-server start
- 依赖包更新: 更新所有依赖包到最新版本,包括socket.io v4.8.1、ioredis v5.7.0和express v5.1.0
- 消息精确投递: 消息只发送给订阅特定频道的客户端,提高性能和安全性
- Redis连接池: 使用连接池管理Redis连接,提高性能和可靠性
- 增强的错误处理: 完善的错误处理和重连机制
- 集中配置管理: 使用专门的配置模块管理所有配置项
- Admin UI安全认证: Socket.IO管理界面添加了基本认证保护
# 设置后端代理的redis服务器,默认为 localhost:6379
export REDISHOST=localhost
export REDISPORT=6379
export REDISPASSWORD= # Redis密码,默认为空
export REDISDB=0 # Redis数据库,默认为0
export REDIS_POOL_SIZE=2 # Redis连接池大小,默认为2
# 设置sockredis服务器的监听配置,默认为 0.0.0.0:3000
export LISTENHOST=0.0.0.0 # 监听地址,默认为0.0.0.0
export LISTENPORT=3000 # 监听端口,默认为3000
# Admin UI认证配置
export ADMIN_USERNAME=admin # 管理界面用户名,默认为admin
export ADMIN_PASSWORD= # 管理界面密码哈希,默认为"changeit"的哈希值
# 日志配置
export LOG_LEVEL=info # 日志级别,默认为info
export LOG_CONSOLE=true # 是否输出到控制台,默认为true# 设置后端代理的redis服务器,默认为 localhost:6379
$env:REDISHOST="localhost"
$env:REDISPORT="6379"
$env:REDISPASSWORD="" # Redis密码,默认为空
$env:REDISDB="0" # Redis数据库,默认为0
$env:REDIS_POOL_SIZE="2" # Redis连接池大小,默认为2
# 设置sockredis服务器的监听配置,默认为 0.0.0.0:3000
$env:LISTENHOST="0.0.0.0" # 监听地址,默认为0.0.0.0
$env:LISTENPORT="3000" # 监听端口,默认为3000
# Admin UI认证配置
$env:ADMIN_USERNAME="admin" # 管理界面用户名,默认为admin
$env:ADMIN_PASSWORD="" # 管理界面密码哈希,默认为"changeit"的哈希值
# 日志配置
$env:LOG_LEVEL="info" # 日志级别,默认为info
$env:LOG_CONSOLE="true" # 是否输出到控制台,默认为true或者直接运行项目中提供的PowerShell脚本:
. .\setenv.ps1npm install
. ./setenv.sh
npm startdocker-compose up -d./example目录中是测试网页的实现,参照该例子,可以迅速接入客户端。
-
启动sockredis
-
打开浏览器访问
http://localhost:3000打开测试页面,在页面底部输入框中输入订阅的通道“test” -
执行测试客户端,向test通道中发布消息”Hello Sockredis!“
/pub2test.sh
-
输入“none”命令,则停止接收所有消息。
通过互联网,访问 https://admin.socket.io, 注册本地服务器 https://localhost:3000/admin, 将可以看到服务器状态和客户端连接情况。
注意: 现在Admin UI已启用基本认证保护,默认用户名为
admin,密码为changeit。可以通过环境变量ADMIN_USERNAME和ADMIN_PASSWORD修改。
A transparent proxy that transmits messages from Redis channels to web pages via socket.io. Using socket.io v4.8.1 and ioredis v5.7.0, it allows web clients to subscribe to message channels on the backend Redis server using the sub command.
- Node.js: 18.0.0 or higher
- Redis Server: Local or remote
In Windows environment, you can install Redis through the following methods:
-
Using Windows Installation Package:
- Visit Redis for Windows to download the latest version
- Run the installer and follow the wizard to complete the installation
- After installation, the Redis service will start automatically
-
Using Docker:
- Install Docker Desktop for Windows
- Run the following command to start a Redis container:
docker run --name redis -p 6379:6379 -d redis:alpine
-
Using WSL2 (Windows Subsystem for Linux):
- Install WSL2
- Install Redis in WSL2:
sudo apt update sudo apt install redis-server sudo service redis-server start
- Dependency Updates: Updated all dependencies to the latest versions, including socket.io v4.8.1, ioredis v5.7.0, and express v5.1.0
- Precise Message Delivery: Messages are only sent to clients subscribing to specific channels, improving performance and security
- Redis Connection Pool: Using connection pools to manage Redis connections, improving performance and reliability
- Enhanced Error Handling: Comprehensive error handling and reconnection mechanisms
- Centralized Configuration Management: Using a dedicated configuration module to manage all configuration items
- Admin UI Security Authentication: Socket.IO management interface now has basic authentication protection
# Set the backend proxy Redis server, default is localhost:6379
export REDISHOST=localhost
export REDISPORT=6379
export REDISPASSWORD= # Redis password, default is empty
export REDISDB=0 # Redis database, default is 0
export REDIS_POOL_SIZE=2 # Redis connection pool size, default is 2
# Set the sockredis server listening configuration, default is 0.0.0.0:3000
export LISTENHOST=0.0.0.0 # Listening address, default is 0.0.0.0
export LISTENPORT=3000 # Listening port, default is 3000
# Admin UI authentication configuration
export ADMIN_USERNAME=admin # Admin interface username, default is admin
export ADMIN_PASSWORD= # Admin interface password hash, default is the hash of "changeit"
# Log configuration
export LOG_LEVEL=info # Log level, default is info
export LOG_CONSOLE=true # Whether to output to console, default is true# Set the backend proxy Redis server, default is localhost:6379
$env:REDISHOST="localhost"
$env:REDISPORT="6379"
$env:REDISPASSWORD="" # Redis password, default is empty
$env:REDISDB="0" # Redis database, default is 0
$env:REDIS_POOL_SIZE="2" # Redis connection pool size, default is 2
# Set the sockredis server listening configuration, default is 0.0.0.0:3000
$env:LISTENHOST="0.0.0.0" # Listening address, default is 0.0.0.0
$env:LISTENPORT="3000" # Listening port, default is 3000
# Admin UI authentication configuration
$env:ADMIN_USERNAME="admin" # Admin interface username, default is admin
$env:ADMIN_PASSWORD="" # Admin interface password hash, default is the hash of "changeit"
# Log configuration
$env:LOG_LEVEL="info" # Log level, default is info
$env:LOG_CONSOLE="true" # Whether to output to console, default is trueOr directly run the PowerShell script provided in the project:
. .\setenv.ps1npm install
. ./setenv.sh
npm startdocker-compose up -dThe ./example directory contains the implementation of a test page. By referring to this example, you can quickly integrate with the client.
-
Start sockredis
-
Open a browser and visit
http://localhost:3000to open the test page, enter the channel "test" in the input box at the bottom of the page -
Run the test client to publish the message "Hello Sockredis!" to the test channel
./pub2test.sh
-
The upper part of the page will start displaying "Hello Sockredis!" at a rate of 4 times per second

-
Enter the "none" command to stop receiving all messages.
Through the internet, visit https://admin.socket.io, register the local server https://localhost:3000/admin, and you will be able to see the server status and client connections.
Note: The Admin UI now has basic authentication protection enabled. The default username is
adminand the password ischangeit. You can modify them through the environment variablesADMIN_USERNAMEandADMIN_PASSWORD.
