Skip to content

Commit

Permalink
Merge pull request #33 from chinmoy12c/uci-pwa
Browse files Browse the repository at this point in the history
  • Loading branch information
ChakshuGautam authored Sep 7, 2023
2 parents c95ee46 + e64232c commit 369794a
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 26 deletions.
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@
"@nestjs/terminus": "^8.0.1",
"@nestjs/websockets": "^8.0.6",
"@types/cache-manager": "^3.4.2",
"cache-manager": "^4.1.0",
"cache-manager-redis-store": "^3.0.1",
"cache-manager": "^3.6.0",
"cache-manager-redis-store": "^2.0.0",
"ioredis": "^5.3.1",
"jsonwebtoken": "^9.0.0",
"jwks-rsa": "^3.0.1",
Expand Down
2 changes: 1 addition & 1 deletion src/app.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ export class AppController {
public appConfig;
constructor(
private readonly wsg: SocketGateway,
@Inject('CustomCacheToken') private cacheManager: Cache,
@Inject(CACHE_MANAGER) private cacheManager: Cache,
) {
this.appConfig = config().app;
}
Expand Down
15 changes: 11 additions & 4 deletions src/app.module.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import * as winston from 'winston';

import { CacheModule, Logger, Module } from '@nestjs/common';
import { Logger, Module } from '@nestjs/common';
import {
WinstonModule,
utilities as nestWinstonModuleUtilities,
Expand All @@ -14,7 +14,9 @@ import { HttpModule } from '@nestjs/axios';
import { SocketGateway } from './socket/socket.gateway';
import { TerminusModule } from '@nestjs/terminus';
import { config } from './config/config';
import { CustomCacheModule } from './cache.module';
import { CacheModule } from '@nestjs/common';
import * as redisStore from 'cache-manager-redis-store';

@Module({
imports: [
WinstonModule.forRootAsync({
Expand Down Expand Up @@ -58,8 +60,13 @@ import { CustomCacheModule } from './cache.module';
load: [config],
}),
TerminusModule,
CustomCacheModule,
CacheModule.register(),
CacheModule.register({
store: redisStore,
host: process.env.TRANSPORT_SOCKET_CACHE_HOST,
port: process.env.TRANSPORT_SOCKET_CACHE_PORT,
max: 200000,
ttl: 86400,
}),
],
controllers: [AppController, HealthController],
providers: [AppService, SocketGateway, Logger],
Expand Down
15 changes: 0 additions & 15 deletions src/cache.module.ts

This file was deleted.

9 changes: 5 additions & 4 deletions src/socket/socket.gateway.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ export class SocketGateway

constructor(
private readonly appService: AppService,
@Inject('CustomCacheToken') private cacheManager: Cache,
@Inject(CACHE_MANAGER) private cacheManager: Cache,
) {}

afterInit(server: Server) {
Expand All @@ -47,7 +47,8 @@ export class SocketGateway
);
const sessionID = client.handshake.query.deviceId;
const userID = client.handshake.query.deviceId as string;
await this.cacheManager.set(userID, client.id, 86400 * 1000);
await this.cacheManager.set(userID, client.id);
this.logger.log(`Storing userID in cache: ${userID}, id: ${client.id}`);
client['sessionID'] = sessionID;
client['userID'] = userID;
client.join(userID);
Expand All @@ -60,7 +61,7 @@ export class SocketGateway

async handleDisconnect(client: Socket) {
this.logger.log(`Client is disconnected = ${client.id}`);
await this.cacheManager.set(client['userID'], null, 86400 * 1000);
await this.cacheManager.del(client['userID']);
}

@UseGuards(WsGuard)
Expand All @@ -81,7 +82,7 @@ export class SocketGateway
@SubscribeMessage('endConnection')
async handleEndConnection(client: Socket) {
this.logger.log({ msg: 'The client has closed the bot' });
await this.cacheManager.set(client['userID'], null, 86400 * 1000);
await this.cacheManager.del(client['userID']);
client.disconnect(true);
return {};
}
Expand Down

0 comments on commit 369794a

Please sign in to comment.