socket.io 集群版——旧版归档笔记

本文涉及的产品
云数据库 Redis 版,社区版 2GB
推荐场景:
搭建游戏排行榜
简介: socket.io 集群版——旧版归档笔记

写在前面

本笔记所记录的方式来源于 socket.io 官方文档,但是实际测量下来经过这样后还会有问题,最后迫不得已,将 websocket 功能抽出成单独的服务。


中文文档

官方文档


注意

截止到目前(2021年11月17日),中文文档和官方文档中都使用了socket.io-redis这个包,但是这个包已经在 6 个月前就声明 deprecated 了,迁移至新的@socket.io/redis-adapter地址了。


实现原理与方式

写不过来了,有空补。


使用redis包

注意

目前 redis 包升级到了 4.x ,官方文档上的写法已经不可以了,官方文档还没有给出新的写法,如果使用这个方案,暂时先使用 redis 3.x 吧(记录于2022年1月11日)。


安装必要包

$ pnpm add @nestjs/websockets @nestjs/platform-socket.io socket.io @socket.io/redis-adapter redis
$ pnpm add -D @types/redis点击复制复制失败已复制


编写适配器

建立适配器文件 redis-io.adapter.ts ,写入如下内容:

import { ConfigType } from '@nestjs/config';
import { NestExpressApplication } from '@nestjs/platform-express';
import { IoAdapter } from '@nestjs/platform-socket.io';
import { createAdapter } from '@socket.io/redis-adapter';
import { createClient, RedisClient } from 'redis';
import { ServerOptions } from 'socket.io';
import { RedisConfigRegister } from '../../config/registers/redis.register';
/** socket.io redis 适配器 */
export class RedisIoAdapter extends IoAdapter {
  constructor(app: NestExpressApplication, redisConfig: ConfigType<typeof RedisConfigRegister>) {
    super(app);
    this.pubClient = createClient({ host: redisConfig.common.host, port: redisConfig.common.port });
    this.subClient = this.pubClient.duplicate();
  }
  private pubClient: RedisClient;
  private subClient: RedisClient;
  createIOServer(port: number, options?: ServerOptions): any {
    const server = super.createIOServer(port, options);
    server.adapter(createAdapter(this.pubClient, this.subClient));
    return server;
  }
}点击复制复制失败已复制


提示

主要参考 socket.io 的官方文档中redis适配器部分。


使用

main.ts 文件中配置,具体参考如下:

……
const app = await NestFactory.create<NestExpressApplication>(AppModule); 
const redisConfig = app.select(AppModule).get<ConfigType<typeof RedisConfigRegister>>(RedisConfigRegister. KEY); 
// 使用Redis socket.io适配器
app.useWebSocketAdapter(new RedisIoAdapter(app, redisConfig)); 
……点击复制复制失败已复制


使用ioredis包

安装必要包

$ pnpm add @nestjs/websockets @nestjs/platform-socket.io socket.io @socket.io/redis-adapter ioredis点击复制复制失败已复制


编写适配器

建立适配器文件 redis-io.adapter.ts ,写入如下内容:

import { ConfigType } from '@nestjs/config';
import { NestExpressApplication } from '@nestjs/platform-express';
import { IoAdapter } from '@nestjs/platform-socket.io';
import { createAdapter } from '@socket.io/redis-adapter';
import { Cluster } from 'ioredis';
import { ServerOptions } from 'socket.io';
import { RedisConfigRegister } from '../../config/registers/redis.register';
/** socket.io redis 适配器 */
export class RedisIoAdapter extends IoAdapter {
  constructor(app: NestExpressApplication, redisConfig: ConfigType<typeof RedisConfigRegister>) {
    super(app);
    this.pubClient = new Cluster([{ host: redisConfig.common.host, port: redisConfig.common.port }]);
    this.subClient = this.pubClient.duplicate();
  }
  private pubClient: Cluster;
  private subClient: Cluster;
  createIOServer(port: number, options?: ServerOptions): any {
    const server = super.createIOServer(port, options);
    server.adapter(createAdapter(this.pubClient, this.subClient));
    return server;
  }
}
点击复制复制失败已复制


提示

主要参考 socket.io 的官方文档中redis适配器部分。


使用

main.ts 文件中配置,具体参考如下:

……
const app = await NestFactory.create<NestExpressApplication>(AppModule); 
const redisConfig = app.select(AppModule).get<ConfigType<typeof RedisConfigRegister>>(RedisConfigRegister. KEY); 
// 使用Redis socket.io适配器
app.useWebSocketAdapter(new RedisIoAdapter(app, redisConfig)); 
……
相关实践学习
基于Redis实现在线游戏积分排行榜
本场景将介绍如何基于Redis数据库实现在线游戏中的游戏玩家积分排行榜功能。
云数据库 Redis 版使用教程
云数据库Redis版是兼容Redis协议标准的、提供持久化的内存数据库服务,基于高可靠双机热备架构及可无缝扩展的集群架构,满足高读写性能场景及容量需弹性变配的业务需求。 产品详情:https://www.aliyun.com/product/kvstore &nbsp; &nbsp; ------------------------------------------------------------------------- 阿里云数据库体验:数据库上云实战 开发者云会免费提供一台带自建MySQL的源数据库&nbsp;ECS 实例和一台目标数据库&nbsp;RDS实例。跟着指引,您可以一步步实现将ECS自建数据库迁移到目标数据库RDS。 点击下方链接,领取免费ECS&amp;RDS资源,30分钟完成数据库上云实战!https://developer.aliyun.com/adc/scenario/51eefbd1894e42f6bb9acacadd3f9121?spm=a2c6h.13788135.J_3257954370.9.4ba85f24utseFl
目录
相关文章
|
3月前
|
Unix C语言
操作系统基础:IO管理概述【上】
操作系统基础:IO管理概述【上】
操作系统基础:IO管理概述【上】
|
SQL 运维 监控
【巡检问题分析与最佳实践】MongoDB 磁盘IO高问题
阿里云数据库MongoDB的IOPS使用率是一个非常重要的监控指标,IOPS使用率达到或接近100%后容易引起业务响应缓慢,甚至导致业务不可用的情形。一般云数据库厂商为了避免宿主机出现IO争抢,会使用Cgroup等技术进行实例间的IO隔离和IOPS限制,即不同规格的实例配置对应不同的IOPS使用上限。
【巡检问题分析与最佳实践】MongoDB 磁盘IO高问题
|
18天前
|
存储 消息中间件 缓存
jeecgboot运行磁盘不足问题( java.io.IOException)和redis闪退问题
jeecgboot运行磁盘不足问题( java.io.IOException)和redis闪退问题
22 0
|
1月前
|
安全 Java
掌握 Java IO 流:常见问题与解决方案
【4月更文挑战第4天】Java IO 流问题详解:文件读写失败(检查路径与权限)、字符编码错误(指定正确编码)、缓冲区使用不当(优化性能)、异常处理缺失(捕获并处理异常)、资源未释放(及时关闭流或用try-with-resources)、并发访问冲突(使用同步机制)和文件锁定(处理锁文件异常)。解决这些问题可提升程序稳定性与性能。
|
3月前
操作系统基础:IO管理概述【下】
操作系统基础:IO管理概述【下】
|
9月前
|
前端开发 API 开发工具
阿里云oss开发实践:大文件分片、断点续传、实时进度 React+Node+Socket.IO
阿里云oss开发实践:大文件分片、断点续传、实时进度 React+Node+Socket.IO
908 1
|
11月前
|
Linux 测试技术
服务器IO读写/下载测速脚本SuperBench
服务器IO读写/下载测速脚本SuperBench
179 0
|
网络协议 Linux API
系统编程之文件IO(一)——概述
系统编程之文件IO(一)——概述
系统编程之文件IO(一)——概述
|
消息中间件 Java 中间件
订阅 OceanBase CLog 日志(Java程序)
简介: 随着 OceanBase 数据库的开源,越来越多的企业开始使用 OceanBase,也有很多个人、机构开始学习 OceanBase,我也是其中之一。后续计划将自己的学习经验陆续总结出来,欢迎大家一起讨论。考虑到数据库是一个博大精深的领域,如有写的不对的地方欢迎指正。 本文主要通过 OceanBase-Mini 版本、LogProxy、已经 Java 程序,实现实时订阅 OB CLog 日志,仅限学习场景使用,不适合生产。
656 0
|
缓存 负载均衡 NoSQL
Elasticsearch实战-磁盘IO被打满
Elasticsearch实战-磁盘IO被打满