Closed dills122 closed 3 weeks ago
import { IoAdapter } from '@nestjs/platform-socket.io';
import { createAdapter } from '@socket.io/redis-adapter';
import { Redis } from 'ioredis';
import { Server, ServerOptions } from 'socket.io';
import * as util from '../shared/util';
import { INestApplicationContext } from '@nestjs/common';
import { ConfigService } from '@nestjs/config';
const host = util.isLocal() ? 'localhost' : 'redis';
export class RedisIoAdapter extends IoAdapter {
protected redisAdapter;
constructor(private app: INestApplicationContext) {
super(app);
const configService = this.app.get(ConfigService);
const port: string = configService.get('REDIS_IO_PORT') || '';
if (!port || port.length <= 0 || !host) {
throw Error('Issue creating the Redis URL');
}
const pubClient = new Redis({
host,
port: Number(port)
});
//Create a new instance and initiate them individually
const subClient = new Redis({
host,
port:Number(port)
})
pubClient.connect(); // <------
subClient.connect(); // <------
this.redisAdapter = createAdapter(pubClient, subClient);
}
createIOServer(port: number, options?: ServerOptions) {
const server = super.createIOServer(port, options) as Server;
server.adapter(this.redisAdapter);
return server;
}
}
If want to duplicate the same instance
const subClient = new Redis({
host,
port:Number(port)
})
const pubClient=subClient.duplicate();
subClient.connect()
The pubClient
is automatically connect (before call subClient.connect())
I'm working on a chat app with Nest.js and initially had a
RedisIoAdapter.ts
setup after reading some docs, and that is/was working fine. However, when I added another Redis instance & client I started to get the error.So its leading me to believe I can't have connections open for each of my two servers?
Overview of what I'm trying to do:
Server 1 gets its connection setup in the
bootstrap
method for the NestJS server. Then Server 2 gets setup in theRedisModule
with its client.factory`.If I disable one of them (IOAdapter is the easiest to disable/comment out) then it runs without an error, but if I start the app with both still enabled, I get the error above whenever the second connection trys to start.
Server 2 Client Factory:
Server 1 IOAdapter: