Skip to content

Commit

Permalink
feat:add redisIoAdapter (#105)
Browse files Browse the repository at this point in the history
  • Loading branch information
romansharapov19 authored Aug 2, 2023
1 parent ce4b076 commit d19ccfb
Show file tree
Hide file tree
Showing 6 changed files with 190 additions and 1 deletion.
150 changes: 150 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions packages/api/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
"@nestjs/schedule": "^3.0.1",
"@nestjs/swagger": "^6.3.0",
"@nestjs/websockets": "^9.4.3",
"@socket.io/redis-adapter": "^8.2.1",
"bull": "^4.10.4",
"config": "^3.3.9",
"cookie-parser": "^1.4.6",
Expand Down
30 changes: 30 additions & 0 deletions packages/api/src/chats/adapters/redis-io.adapter.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import { ConfigService } from '@nestjs/config';
import { IoAdapter } from '@nestjs/platform-socket.io';
import { createAdapter } from '@socket.io/redis-adapter';
import { createClient } from 'redis';
import { ServerOptions } from 'socket.io';

export class RedisIoAdapter extends IoAdapter {
constructor(app, private readonly configService: ConfigService) {
super(app);
}
private adapterConstructor: ReturnType<typeof createAdapter>;

async connectToRedis(): Promise<void> {
const pubClient = createClient({
url: `redis://${this.configService.get('REDIS_CONNECTION_URL')}`,
password: this.configService.get('REDIS_HOST_PASSWORD'),
});
const subClient = pubClient.duplicate();

await Promise.all([pubClient.connect(), subClient.connect()]);

this.adapterConstructor = createAdapter(pubClient, subClient);
}

createIOServer(port: number, options?: ServerOptions): any {
const server = super.createIOServer(port, options);
server.adapter(this.adapterConstructor);
return server;
}
}
1 change: 1 addition & 0 deletions packages/api/src/chats/chat-socket.gateway.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import { Server, Socket } from 'socket.io';
@WebSocketGateway({
cors: {
origin: '*',
credentials: true,
},
})
export class ChatSocketGateway {
Expand Down
5 changes: 5 additions & 0 deletions packages/api/src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { DocumentBuilder, SwaggerModule } from '@nestjs/swagger';
import * as cookieParser from 'cookie-parser';

import { AppModule } from './app.module';
import { RedisIoAdapter } from './chats/adapters/redis-io.adapter';

async function bootstrap() {
const app = await NestFactory.create(AppModule);
Expand All @@ -31,6 +32,10 @@ async function bootstrap() {
const document = SwaggerModule.createDocument(app, config);
SwaggerModule.setup('docs', app, document);

const redisIoAdapter = new RedisIoAdapter(app, configService);
await redisIoAdapter.connectToRedis();
app.useWebSocketAdapter(redisIoAdapter);

await app.listen(configService.get('API_PORT'));
}

Expand Down
4 changes: 3 additions & 1 deletion packages/web-ui/app/state/socket.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@ import { atom } from 'recoil';
import io, { Socket } from 'socket.io-client';

// @ts-ignore
const socket = io(process.env.NEXT_PUBLIC_SOCKET_URL);
const socket = io(process.env.NEXT_PUBLIC_SOCKET_URL, {
withCredentials: true,
});

export const socketState = atom<Socket>({
key: 'Socket',
Expand Down

0 comments on commit d19ccfb

Please sign in to comment.