nestJS “socket.io-redis”: “6.0.1”

元气小坏坏 提交于 2021-01-29 15:22:29

问题


according to the documentation TypeScript // npm i -D @types/redis

import { Server } from 'socket.io';
import { createAdapter } from 'socket.io-redis';
import { RedisClient } from 'redis';

const io = new Server(8080);
const pubClient = new RedisClient({ host: 'localhost', port: 6379 });
const subClient = pubClient.duplicate();

io.adapter(createAdapter({ pubClient, subClient }));

I create redis adapter

import { IoAdapter } from '@nestjs/platform-socket.io';
import { createAdapter } from 'socket.io-redis';
import { RedisClient } from 'redis';

export class RedisIoAdapter extends IoAdapter {
  createIOServer(port: number, options?: any): any {
    const server = super.createIOServer(port, options);
    const pubClient = new RedisClient({ host: 'localhost', port: 6379 });
    const subClient = pubClient.duplicate();
    server.adapter(createAdapter({ pubClient, subClient }));
    return server;
  }
}

after i use dispatch this.appGateway.server.emit('test', 'hello');

TypeError: callback is not a function
    at Encoder.encode (/home/test/node_modules/socket.io-parser/index.js:135:5)
    at RedisAdapter.broadcast (/home/test/node_modules/socket.io-redis/node_modules/socket.io-adapter/dist/index.js:102:45)
    at RedisAdapter.broadcast (/home/test/node_modules/socket.io-redis/dist/index.js:267:15)
    at Namespace.emit (/home/test/node_modules/socket.io/lib/namespace.js:234:16)
    at Server.<computed> [as emit] (/home/test/node_modules/socket.io/lib/index.js:505:29)

my gateWay

import { OnGatewayConnection,  WebSocketGateway, WebSocketServer } from '@nestjs/websockets';
import { Logger } from '@nestjs/common';

@WebSocketGateway()
export class AppGateway implements OnGatewayConnection {
  private readonly logger = new Logger(AppGateway.name);

  @WebSocketServer()
  server;
  
  handleConnection(client): any {
    this.logger.log('new user connected...');
    client.emit('connection', 'Succesfuly connected');
  }

}

help please solve the problem


回答1:


socket.io-redis ^6 uses socket.io version 3 which is not yet supported by Nest. There's an open issue about it here and it's set to be ready for Nest v8



来源:https://stackoverflow.com/questions/65134378/nestjs-socket-io-redis-6-0-1

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!