Skip to content

Commit

Permalink
fix: use ioredis on socket.io connections
Browse files Browse the repository at this point in the history
  • Loading branch information
rubiin committed Aug 30, 2024
1 parent d4efa25 commit d86fb06
Show file tree
Hide file tree
Showing 6 changed files with 11 additions and 86 deletions.
1 change: 0 additions & 1 deletion env/.env.sample
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,6 @@ REDIS_HOST=
REDIS_PORT=
REDIS_USERNAME=
REDIS_PASSWORD=
REDIS_URI=redis://${REDIS_USERNAME}:${REDIS_PASSWORD}@${REDIS_HOST}:${REDIS_PORT}


# rabbitmq
Expand Down
1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,6 @@
"preview-email": "^3.1.0",
"prom-client": "^14.2.0",
"qrcode": "^1.5.4",
"redis": "^4.7.0",
"reflect-metadata": "0.2.2",
"rxjs": "^7.8.1",
"sharp": "^0.33.5",
Expand Down
76 changes: 0 additions & 76 deletions pnpm-lock.yaml

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

3 changes: 0 additions & 3 deletions src/lib/config/configs/redis.config.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
import process from "node:process";
import { registerAs } from "@nestjs/config";
import Joi from "joi";
import { REDIS_URI_REGEX } from "@common/constant";

export const redisConfigValidationSchema = {
REDIS_URI: Joi.string().pattern(REDIS_URI_REGEX).required(),
REDIS_TTL: Joi.number().integer().min(1).required(),
REDIS_HOST: Joi.string().required(),
REDIS_PORT: Joi.number().port().required(),
Expand All @@ -14,7 +12,6 @@ export const redisConfigValidationSchema = {
};

export const redis = registerAs("redis", () => ({
url: process.env.REDIS_URI,
host: process.env.REDIS_HOST,
username: process.env.REDIS_USERNAME,
password: process.env.REDIS_PASSWORD,
Expand Down
3 changes: 2 additions & 1 deletion src/modules/user/user.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ export class UserService {
* @returns The function `referUser` returns an Observable of type `Referral`.
*/
referUser(dto: ReferUserDto, user: User): Observable<Referral> {
const userExists$ = from(this.userRepository.count({ mobileNumber: dto.mobileNumber, isActive: true }));
const userExists$ = from(this.userRepository.count({ mobileNumber: dto.mobileNumber, isActive: true, isDeleted: false }));

return userExists$.pipe(
switchMap((count: number, _index: number) => {
Expand Down Expand Up @@ -132,6 +132,7 @@ export class UserService {
return from(
this.userRepository.findOne({
idx: index,
isDeleted: false
}),
).pipe(
mergeMap((user) => {
Expand Down
13 changes: 9 additions & 4 deletions src/socket-io.adapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import type { INestApplicationContext } from "@nestjs/common";
import type { ConfigService } from "@nestjs/config";
import { IoAdapter } from "@nestjs/platform-socket.io";
import { createAdapter } from "@socket.io/redis-adapter";
import { createClient } from "redis";
import { Redis } from "ioredis";
import type { Adapter } from "socket.io-adapter";
import type { Namespace, Server, ServerOptions } from "socket.io";

Expand All @@ -21,9 +21,14 @@ export class SocketIOAdapter extends IoAdapter {
* @returns a promise that resolves to void.
*/
async connectToRedis(): Promise<void> {
const pubClient = createClient({
url: this.configService.get("redis.url", { infer: true }),
});
const pubClient = new Redis(
{
host: this.configService.get("redis.host", { infer: true }),
port: this.configService.get("redis.port", { infer: true }),
username: this.configService.get("redis.username", { infer: true }),
password: this.configService.get("redis.password", { infer: true }),
}
);
const subClient = pubClient.duplicate();

await Promise.allSettled([pubClient.connect(), subClient.connect()]);
Expand Down

0 comments on commit d86fb06

Please sign in to comment.