问题
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