├── .dockerignore ├── .env.example ├── .eslintrc.js ├── .gitignore ├── .prettierrc ├── .vscode ├── launch.json └── tasks.json ├── Dockerfile ├── LICENSE ├── README.md ├── nest-cli.json ├── package-lock.json ├── package.json ├── src ├── config │ ├── config.export.ts │ ├── config.schema.ts │ └── config.validate.ts ├── main.ts └── modules │ ├── app │ └── app.module.ts │ ├── controller_example │ ├── controller_example.controller.ts │ ├── controller_example.module.ts │ └── controller_example.service.ts │ ├── example │ ├── example.controller.spec.ts │ ├── example.controller.ts │ ├── example.module.ts │ └── example.service.ts │ └── websocket │ ├── common │ ├── index.ts │ ├── websocket.connection.guard.ts │ ├── websocket.controller.decorator.ts │ └── websocket.gateway.decorator.ts │ ├── guards │ └── example.guard.ts │ ├── interface │ ├── index.ts │ ├── websocket.client.ts │ └── websocket.guard.ts │ ├── websocket.gateway.ts │ ├── websocket.manager.ts │ └── websocket.module.ts ├── test └── app.e2e.ts ├── tsconfig.build.json └── tsconfig.json /.dockerignore: -------------------------------------------------------------------------------- 1 | /node_modules 2 | /dist -------------------------------------------------------------------------------- /.env.example: -------------------------------------------------------------------------------- 1 | NODE_ENV=local 2 | HOST=localhost 3 | WS_PORT=8080 4 | PORT=3000 -------------------------------------------------------------------------------- /.eslintrc.js: -------------------------------------------------------------------------------- 1 | module.exports = { 2 | parser: '@typescript-eslint/parser', 3 | parserOptions: { 4 | project: 'tsconfig.json', 5 | tsconfigRootDir: __dirname, 6 | sourceType: 'module', 7 | }, 8 | plugins: ['@typescript-eslint/eslint-plugin'], 9 | extends: [ 10 | 'plugin:@typescript-eslint/recommended', 11 | 'plugin:prettier/recommended', 12 | ], 13 | root: true, 14 | env: { 15 | node: true, 16 | jest: true, 17 | }, 18 | ignorePatterns: ['.eslintrc.js'], 19 | rules: { 20 | '@typescript-eslint/interface-name-prefix': 'off', 21 | '@typescript-eslint/explicit-function-return-type': 'off', 22 | '@typescript-eslint/explicit-module-boundary-types': 'off', 23 | '@typescript-eslint/no-explicit-any': 'off', 24 | }, 25 | }; 26 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # compiled output 2 | /dist 3 | /node_modules 4 | /build 5 | 6 | # Logs 7 | logs 8 | *.log 9 | npm-debug.log* 10 | pnpm-debug.log* 11 | yarn-debug.log* 12 | yarn-error.log* 13 | lerna-debug.log* 14 | 15 | # OS 16 | .DS_Store 17 | 18 | # Tests 19 | /coverage 20 | /.nyc_output 21 | 22 | # IDEs and editors 23 | /.idea 24 | .project 25 | .classpath 26 | .c9/ 27 | *.launch 28 | .settings/ 29 | *.sublime-workspace 30 | 31 | # IDE - VSCode 32 | .vscode/* 33 | !.vscode/settings.json 34 | !.vscode/tasks.json 35 | !.vscode/launch.json 36 | !.vscode/extensions.json 37 | 38 | # dotenv environment variable files 39 | .env 40 | .env.development.local 41 | .env.test.local 42 | .env.production.local 43 | .env.local 44 | 45 | # temp directory 46 | .temp 47 | .tmp 48 | 49 | # Runtime data 50 | pids 51 | *.pid 52 | *.seed 53 | *.pid.lock 54 | 55 | # Diagnostic reports (https://nodejs.org/api/report.html) 56 | report.[0-9]*.[0-9]*.[0-9]*.[0-9]*.json 57 | -------------------------------------------------------------------------------- /.prettierrc: -------------------------------------------------------------------------------- 1 | { 2 | "singleQuote": true, 3 | "trailingComma": "all" 4 | } -------------------------------------------------------------------------------- /.vscode/launch.json: -------------------------------------------------------------------------------- 1 | { 2 | "version": "0.2.0", 3 | "configurations": [ 4 | { 5 | "type": "node", 6 | "request": "launch", 7 | "name": "Launch Program", 8 | "skipFiles": [ "/**" ], 9 | "program": "${workspaceFolder}/src/main.ts", 10 | "cwd": "${workspaceFolder}", 11 | "console": "integratedTerminal", 12 | "preLaunchTask": "build", 13 | "envFile": "${workspaceFolder}/.env", 14 | "sourceMaps": true 15 | } 16 | ] 17 | } -------------------------------------------------------------------------------- /.vscode/tasks.json: -------------------------------------------------------------------------------- 1 | { 2 | "version": "2.0.0", 3 | "tasks": [ 4 | { 5 | "type": "npm", 6 | "script": "build", 7 | "group": "build", 8 | "problemMatcher": [], 9 | "label": "build", 10 | "detail": "tsc --build" 11 | } 12 | ] 13 | } -------------------------------------------------------------------------------- /Dockerfile: -------------------------------------------------------------------------------- 1 | # syntax=docker/dockerfile:1 2 | FROM node:21.7.3-slim 3 | WORKDIR /usr/src/app 4 | COPY . . 5 | RUN npm install && npm run build:prod && npm cache clean --force 6 | 7 | FROM node:21.7.3-slim 8 | RUN groupadd -r appuser && useradd -r -g appuser -s /sbin/nologin -d /usr/src/app appuser \ 9 | && mkdir -p /usr/src/app \ 10 | && chown -R appuser:appuser /usr/src/app 11 | 12 | WORKDIR /usr/src/app 13 | COPY --from=0 /usr/src/app/ ./ 14 | USER appuser 15 | EXPOSE 3000 16 | CMD ["npm", "run", "start:prod"] -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2024 pendulum 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 |

2 | Nest Logo 3 |

4 | 5 | [circleci-image]: https://img.shields.io/circleci/build/github/nestjs/nest/master?token=abc123def456 6 | [circleci-url]: https://circleci.com/gh/nestjs/nest 7 | 8 |

A progressive Node.js framework for building efficient and scalable server-side applications.

9 |

10 | NPM Version 11 | Package License 12 | NPM Downloads 13 | CircleCI 14 | Coverage 15 | Discord 16 | Backers on Open Collective 17 | Sponsors on Open Collective 18 | Donate us 19 | Support us 20 | Follow us on Twitter 21 |

22 | 24 | 25 | ## Description 26 | 27 | [Nest](https://github.com/nestjs/nest) framework TypeScript starter repository. 28 | 29 | ## Project setup 30 | 31 | ```bash 32 | $ npm install 33 | ``` 34 | 35 | ## Compile and run the project 36 | 37 | ```bash 38 | # development 39 | $ npm run start 40 | 41 | # watch mode 42 | $ npm run start:dev 43 | 44 | # production mode 45 | $ npm run start:prod 46 | ``` 47 | 48 | ## Run tests 49 | 50 | ```bash 51 | # unit tests 52 | $ npm run test 53 | 54 | # e2e tests 55 | $ npm run test:e2e 56 | 57 | # test coverage 58 | $ npm run test:cov 59 | ``` 60 | 61 | ## Resources 62 | 63 | Check out a few resources that may come in handy when working with NestJS: 64 | 65 | - Visit the [NestJS Documentation](https://docs.nestjs.com) to learn more about the framework. 66 | - For questions and support, please visit our [Discord channel](https://discord.gg/G7Qnnhy). 67 | - To dive deeper and get more hands-on experience, check out our official video [courses](https://courses.nestjs.com/). 68 | - Visualize your application graph and interact with the NestJS application in real-time using [NestJS Devtools](https://devtools.nestjs.com). 69 | - Need help with your project (part-time to full-time)? Check out our official [enterprise support](https://enterprise.nestjs.com). 70 | - To stay in the loop and get updates, follow us on [X](https://x.com/nestframework) and [LinkedIn](https://linkedin.com/company/nestjs). 71 | - Looking for a job, or have a job to offer? Check out our official [Jobs board](https://jobs.nestjs.com). 72 | 73 | ## Support 74 | 75 | Nest is an MIT-licensed open source project. It can grow thanks to the sponsors and support by the amazing backers. If you'd like to join them, please [read more here](https://docs.nestjs.com/support). 76 | 77 | ## Stay in touch 78 | 79 | - Author - [Kamil Myśliwiec](https://twitter.com/kammysliwiec) 80 | - Website - [https://nestjs.com](https://nestjs.com/) 81 | - Twitter - [@nestframework](https://twitter.com/nestframework) 82 | 83 | ## License 84 | 85 | Nest is [MIT licensed](https://github.com/nestjs/nest/blob/master/LICENSE). 86 | -------------------------------------------------------------------------------- /nest-cli.json: -------------------------------------------------------------------------------- 1 | { 2 | "$schema": "https://json.schemastore.org/nest-cli", 3 | "collection": "@nestjs/schematics", 4 | "sourceRoot": "src", 5 | "compilerOptions": { 6 | "deleteOutDir": true 7 | } 8 | } 9 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "nestjs-template", 3 | "version": "0.0.1", 4 | "description": "", 5 | "author": "", 6 | "private": true, 7 | "license": "UNLICENSED", 8 | "scripts": { 9 | "build": "nest build", 10 | "format": "prettier --write \"src/**/*.ts\" \"test/**/*.ts\"", 11 | "start": "nest start", 12 | "start:dev": "nest start --watch", 13 | "start:debug": "nest start --debug --watch", 14 | "start:prod": "node dist/src/main", 15 | "lint": "eslint \"{src,apps,libs,test}/**/*.ts\" --fix", 16 | "test": "jest", 17 | "test:watch": "jest --watch", 18 | "test:cov": "jest --coverage", 19 | "test:debug": "node --inspect-brk -r tsconfig-paths/register -r ts-node/register node_modules/.bin/jest --runInBand", 20 | "test:e2e": "jest --config ./test/jest-e2e.json" 21 | }, 22 | "dependencies": { 23 | "@nestjs/common": "^10.0.0", 24 | "@nestjs/core": "^10.0.0", 25 | "@nestjs/platform-express": "^10.0.0", 26 | "@nestjs/platform-ws": "^10.4.6", 27 | "@nestjs/websockets": "^10.4.6", 28 | "class-transformer": "^0.5.1", 29 | "class-validator": "^0.14.1", 30 | "reflect-metadata": "^0.2.0", 31 | "rxjs": "^7.8.1" 32 | }, 33 | "devDependencies": { 34 | "@nestjs/cli": "^10.0.0", 35 | "@nestjs/schematics": "^10.0.0", 36 | "@nestjs/testing": "^10.0.0", 37 | "@types/express": "^4.17.17", 38 | "@types/jest": "^29.5.2", 39 | "@types/node": "^20.3.1", 40 | "@types/supertest": "^6.0.0", 41 | "@types/ws": "^8.5.12", 42 | "@typescript-eslint/eslint-plugin": "^8.0.0", 43 | "@typescript-eslint/parser": "^8.0.0", 44 | "eslint": "^8.42.0", 45 | "eslint-config-prettier": "^9.0.0", 46 | "eslint-plugin-prettier": "^5.0.0", 47 | "jest": "^29.5.0", 48 | "prettier": "^3.0.0", 49 | "source-map-support": "^0.5.21", 50 | "supertest": "^7.0.0", 51 | "ts-jest": "^29.1.0", 52 | "ts-loader": "^9.4.3", 53 | "ts-node": "^10.9.1", 54 | "tsconfig-paths": "^4.2.0", 55 | "typescript": "^5.1.3" 56 | }, 57 | "jest": { 58 | "moduleFileExtensions": [ 59 | "js", 60 | "json", 61 | "ts" 62 | ], 63 | "roots": [ 64 | "src", 65 | "test" 66 | ], 67 | "testRegex": ".*\\.(spec|e2e)\\.ts$", 68 | "transform": { 69 | "^.+\\.(t|j)s$": "ts-jest" 70 | }, 71 | "collectCoverageFrom": [ 72 | "**/*.(t|j)s" 73 | ], 74 | "coverageDirectory": "../coverage", 75 | "testEnvironment": "node" 76 | } 77 | } 78 | -------------------------------------------------------------------------------- /src/config/config.export.ts: -------------------------------------------------------------------------------- 1 | import { ConfigApp } from "./config.schema"; 2 | import { validateEnv } from "./config.validate"; 3 | 4 | export const CONFIG_APP = validateEnv(ConfigApp); -------------------------------------------------------------------------------- /src/config/config.schema.ts: -------------------------------------------------------------------------------- 1 | import { IsNotEmpty, IsNumber, IsString } from "class-validator"; 2 | 3 | export class ConfigApp { 4 | 5 | @IsString() 6 | @IsNotEmpty() 7 | NODE_ENV: string; 8 | 9 | @IsNumber() 10 | @IsNotEmpty() 11 | PORT: number; 12 | 13 | @IsNumber() 14 | @IsNotEmpty() 15 | WS_PORT: number; 16 | 17 | @IsString() 18 | @IsNotEmpty() 19 | HOST: string; 20 | } -------------------------------------------------------------------------------- /src/config/config.validate.ts: -------------------------------------------------------------------------------- 1 | import { plainToInstance } from "class-transformer"; 2 | import { validateSync } from "class-validator"; 3 | 4 | 5 | export function validateEnv(config: new (...args: any[]) => T): T { 6 | const validatedConfig = plainToInstance(config, process.env, { 7 | enableImplicitConversion: true, 8 | }); 9 | const errors = validateSync(validatedConfig, { skipMissingProperties: false }); 10 | if (errors.length > 0) { 11 | throw new Error(errors.toString()); 12 | } 13 | return validatedConfig; 14 | } -------------------------------------------------------------------------------- /src/main.ts: -------------------------------------------------------------------------------- 1 | import { NestFactory } from '@nestjs/core'; 2 | import { AppModule } from './modules/app/app.module'; 3 | import { CONFIG_APP } from './config/config.export'; 4 | import { WsAdapter } from '@nestjs/platform-ws'; 5 | 6 | async function bootstrap() { 7 | const app = await NestFactory.create(AppModule); 8 | app.useWebSocketAdapter(new WsAdapter(app)); 9 | await app.listen(CONFIG_APP.PORT); 10 | } 11 | bootstrap(); 12 | -------------------------------------------------------------------------------- /src/modules/app/app.module.ts: -------------------------------------------------------------------------------- 1 | import { Module } from '@nestjs/common'; 2 | import { ExampleModule } from '../example/example.module'; 3 | import { WebsocketModule } from '../websocket/websocket.module'; 4 | import { SubscribeExampleModule } from '../controller_example/controller_example.module'; 5 | 6 | @Module({ 7 | imports: [ 8 | WebsocketModule, 9 | SubscribeExampleModule, 10 | ], 11 | }) 12 | export class AppModule {} 13 | -------------------------------------------------------------------------------- /src/modules/controller_example/controller_example.controller.ts: -------------------------------------------------------------------------------- 1 | import { Event, WebsocketController } from '../websocket/common'; 2 | import { ExampleService } from './controller_example.service'; 3 | 4 | @WebsocketController() 5 | export class ExampleController { 6 | 7 | constructor(private exampleService: ExampleService) {} 8 | 9 | @Event('example') 10 | public getHello(): string { 11 | return this.exampleService.getHello(); 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /src/modules/controller_example/controller_example.module.ts: -------------------------------------------------------------------------------- 1 | import { Module } from '@nestjs/common'; 2 | import { ExampleService } from './controller_example.service'; 3 | import { ExampleController } from './controller_example.controller'; 4 | 5 | @Module({ 6 | controllers: [ExampleController], 7 | providers: [ExampleService], 8 | }) 9 | export class SubscribeExampleModule {} 10 | -------------------------------------------------------------------------------- /src/modules/controller_example/controller_example.service.ts: -------------------------------------------------------------------------------- 1 | import { Injectable } from '@nestjs/common'; 2 | 3 | @Injectable() 4 | export class ExampleService { 5 | public getHello(): string { 6 | return 'Hello World!'; 7 | } 8 | } 9 | -------------------------------------------------------------------------------- /src/modules/example/example.controller.spec.ts: -------------------------------------------------------------------------------- 1 | import { Test, TestingModule } from '@nestjs/testing'; 2 | import { ExampleController } from './example.controller'; 3 | import { ExampleService } from './example.service'; 4 | 5 | describe('AppController', () => { 6 | let appController: ExampleController; 7 | 8 | beforeEach(async () => { 9 | const app: TestingModule = await Test.createTestingModule({ 10 | controllers: [ExampleController], 11 | providers: [ExampleService], 12 | }).compile(); 13 | 14 | appController = app.get(ExampleController); 15 | }); 16 | 17 | describe('root', () => { 18 | it('should return "Hello World!"', () => { 19 | expect(appController.getHello()).toBe('Hello World!'); 20 | }); 21 | }); 22 | }); 23 | -------------------------------------------------------------------------------- /src/modules/example/example.controller.ts: -------------------------------------------------------------------------------- 1 | import { Controller, Get } from '@nestjs/common'; 2 | import { ExampleService } from './example.service'; 3 | 4 | @Controller() 5 | export class ExampleController { 6 | constructor(private readonly appService: ExampleService) {} 7 | 8 | @Get() 9 | public getHello(): string { 10 | return this.appService.getHello(); 11 | } 12 | } 13 | -------------------------------------------------------------------------------- /src/modules/example/example.module.ts: -------------------------------------------------------------------------------- 1 | import { Module } from '@nestjs/common'; 2 | import { ExampleController } from './example.controller'; 3 | import { ExampleService } from './example.service'; 4 | 5 | @Module({ 6 | imports: [], 7 | controllers: [ExampleController], 8 | providers: [ExampleService], 9 | }) 10 | export class ExampleModule {} 11 | -------------------------------------------------------------------------------- /src/modules/example/example.service.ts: -------------------------------------------------------------------------------- 1 | import { Injectable } from '@nestjs/common'; 2 | 3 | @Injectable() 4 | export class ExampleService { 5 | public getHello(): string { 6 | return 'Hello World!'; 7 | } 8 | } 9 | -------------------------------------------------------------------------------- /src/modules/websocket/common/index.ts: -------------------------------------------------------------------------------- 1 | export * from './websocket.connection.guard'; 2 | export * from './websocket.gateway.decorator'; 3 | export * from './websocket.controller.decorator'; 4 | -------------------------------------------------------------------------------- /src/modules/websocket/common/websocket.connection.guard.ts: -------------------------------------------------------------------------------- 1 | import { IncomingMessage } from "http"; 2 | import { WebSocketClient, WebsocketGuard } from "../interface"; 3 | 4 | export function ConnectionGuard(...guards: (WebsocketGuard | { new(): WebsocketGuard })[]) { 5 | 6 | return function (target: any, propertyKey: string, descriptor: PropertyDescriptor) { 7 | const originalMethod = descriptor.value; 8 | 9 | descriptor.value = async function (client: WebSocketClient, request: IncomingMessage) { 10 | const guardInstances = guards.map(guard => 11 | typeof guard === 'function' ? new guard() : guard 12 | ); 13 | const validation = guardInstances 14 | .every(async guard => await guard.canActivate(client, request)); 15 | 16 | if (validation) { 17 | await originalMethod.call(this, client, request); 18 | } else { 19 | client.close(); 20 | } 21 | }; 22 | return descriptor; 23 | } 24 | } -------------------------------------------------------------------------------- /src/modules/websocket/common/websocket.controller.decorator.ts: -------------------------------------------------------------------------------- 1 | import { Controller, Inject, Logger, OnModuleInit, Type } from "@nestjs/common"; 2 | import { WebsocketManager } from "../websocket.manager"; 3 | 4 | export function WebsocketController() { 5 | const logger = new Logger("EventController"); 6 | 7 | return function >(constructor: T) { 8 | @Controller() 9 | class WrappedController extends constructor implements OnModuleInit { 10 | 11 | @Inject() 12 | readonly websocketManager: WebsocketManager; 13 | 14 | async onModuleInit() { 15 | const methodNames = Object.getOwnPropertyNames(constructor.prototype) 16 | .filter((methodName) => methodName !== 'constructor'); 17 | 18 | for (const methodName of methodNames) { 19 | const descriptor = Object.getOwnPropertyDescriptor(constructor.prototype, methodName); 20 | if (descriptor && typeof descriptor.value === 'function') { 21 | const originalMethod = descriptor.value; 22 | const event = Reflect.getMetadata(methodName, descriptor.value); 23 | descriptor.value = async (...args: any[]) => { 24 | try { 25 | return await originalMethod.apply(this, args); 26 | } catch (err) { 27 | logger.error(err.message); 28 | } 29 | }; 30 | Object.defineProperty(this, methodName, descriptor); 31 | this.websocketManager['addHandler'](event, descriptor.value); 32 | } 33 | } 34 | } 35 | } 36 | return WrappedController; 37 | }; 38 | } 39 | 40 | export function Event(title: string) { 41 | return function (target: any, propertyKey: string, descriptor: PropertyDescriptor) { 42 | Reflect.defineMetadata(propertyKey, title, descriptor.value); 43 | } 44 | } -------------------------------------------------------------------------------- /src/modules/websocket/common/websocket.gateway.decorator.ts: -------------------------------------------------------------------------------- 1 | import { WebSocketGateway } from "@nestjs/websockets"; 2 | import { WebSocketClient } from "../interface"; 3 | import { IncomingMessage } from "node:http"; 4 | import { Logger } from "@nestjs/common"; 5 | 6 | 7 | export function WebSocketGatewayV2(options: { port: number }) { 8 | const logger = new Logger("WebSocket"); 9 | 10 | const wrapMethod = (target: any, propertyKey: string, descriptor: PropertyDescriptor) => { 11 | const originalMethod = descriptor.value; 12 | 13 | descriptor.value = async function (client: WebSocketClient, request: IncomingMessage) { 14 | try { 15 | await originalMethod.apply(this, [client, request]); 16 | } catch (err) { 17 | logger.error(err.message); 18 | } 19 | }; 20 | return descriptor; 21 | } 22 | 23 | return function (constructor: Function) { 24 | WebSocketGateway(options.port, { transports: ['websocket'] })(constructor); 25 | 26 | const methodNames = Object.getOwnPropertyNames(constructor.prototype) 27 | .filter((methodName) => methodName !== 'constructor'); 28 | 29 | for (const methodName of methodNames) { 30 | const descriptor = Object.getOwnPropertyDescriptor( 31 | constructor.prototype, 32 | methodName 33 | ); 34 | if (descriptor && typeof descriptor.value === 'function') { 35 | Object.defineProperty( 36 | constructor.prototype, 37 | methodName, 38 | wrapMethod(constructor, methodName, descriptor) 39 | ); 40 | } 41 | } 42 | }; 43 | } -------------------------------------------------------------------------------- /src/modules/websocket/guards/example.guard.ts: -------------------------------------------------------------------------------- 1 | import { IncomingMessage } from "http"; 2 | import { WebSocketClient } from "../interface"; 3 | import { WebsocketGuard } from "../interface/websocket.guard"; 4 | import { randomUUID } from "node:crypto"; 5 | 6 | export class ExampleGuard implements WebsocketGuard { 7 | canActivate(client: WebSocketClient, request: IncomingMessage): boolean { 8 | 9 | client.userId = 1; 10 | client.connectionId = randomUUID(); 11 | 12 | return true; 13 | } 14 | } 15 | 16 | export class ReallyLastGuard implements WebsocketGuard { 17 | async canActivate(client: WebSocketClient, request: IncomingMessage): Promise { 18 | const jwtVerifyMock = async (token: string) => { 19 | return { userId: 1 }; 20 | }; 21 | // 25.10.2024 it was actual 22 | // It is better to not separate token as Bearer xxx 23 | // Without space between them!!! 24 | const token = request.headers['sec-websocket-protocol']; 25 | const { userId } = await jwtVerifyMock(token); 26 | 27 | client.userId = userId; 28 | client.connectionId = randomUUID(); 29 | 30 | return true; 31 | } 32 | } -------------------------------------------------------------------------------- /src/modules/websocket/interface/index.ts: -------------------------------------------------------------------------------- 1 | export * from './websocket.client'; 2 | export * from './websocket.guard'; 3 | -------------------------------------------------------------------------------- /src/modules/websocket/interface/websocket.client.ts: -------------------------------------------------------------------------------- 1 | import { WebSocket } from 'ws'; 2 | 3 | export interface WebSocketClient extends WebSocket { 4 | connectionId: string; 5 | userId: number; 6 | } -------------------------------------------------------------------------------- /src/modules/websocket/interface/websocket.guard.ts: -------------------------------------------------------------------------------- 1 | import { IncomingMessage } from "http"; 2 | import { WebSocketClient } from "./websocket.client"; 3 | 4 | export interface WebsocketGuard { 5 | canActivate(client: WebSocketClient, request: IncomingMessage): boolean | Promise; 6 | }; -------------------------------------------------------------------------------- /src/modules/websocket/websocket.gateway.ts: -------------------------------------------------------------------------------- 1 | import { WebsocketManager } from './websocket.manager'; 2 | import { WebSocketClient } from './interface'; 3 | import { ExampleGuard } from './guards/example.guard'; 4 | import { ConnectionGuard, WebSocketGatewayV2 } from './common'; 5 | import { OnGatewayConnection, OnGatewayDisconnect } from '@nestjs/websockets'; 6 | import { CONFIG_APP } from 'src/config/config.export'; 7 | import { Inject } from '@nestjs/common'; 8 | 9 | 10 | @WebSocketGatewayV2({ port: CONFIG_APP.WS_PORT }) 11 | export class WebsocketGateway implements OnGatewayConnection, OnGatewayDisconnect { 12 | 13 | @Inject() 14 | private readonly websocketManager: WebsocketManager; 15 | 16 | @ConnectionGuard(ExampleGuard) 17 | public async handleConnection(client: WebSocketClient): Promise { 18 | await this.websocketManager['connect'](client); 19 | } 20 | 21 | public async handleDisconnect(client: WebSocketClient): Promise { 22 | await this.websocketManager['disconnect'](client); 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /src/modules/websocket/websocket.manager.ts: -------------------------------------------------------------------------------- 1 | import { Injectable, Logger } from "@nestjs/common"; 2 | import { WebSocketClient } from "./interface"; 3 | import { RawData } from "ws"; 4 | 5 | @Injectable() 6 | export class WebsocketManager { 7 | 8 | private readonly handlers = new Map(); 9 | private readonly clients = new Map(); 10 | 11 | private async connect(wsClient: WebSocketClient) { 12 | // Here you can use any database or other methods to controll user sessions 13 | this.attachHandlers(wsClient); 14 | this.clients.set(wsClient.connectionId, wsClient); 15 | } 16 | 17 | private async disconnect(wsClient: WebSocketClient) { 18 | // async needs to delete from redis or other dbs 19 | this.clients.delete(wsClient.connectionId); 20 | } 21 | 22 | public send(connectionId: string, data: any) { 23 | if (this.clients.has(connectionId)) { 24 | const client = this.clients.get(connectionId); 25 | client.send(data); 26 | } 27 | } 28 | 29 | public has(connectionId: string): boolean { 30 | return this.clients.has(connectionId); 31 | } 32 | 33 | private addHandler(event: string, handler: Function): void { 34 | if (this.handlers.has(event)) { 35 | this.handlers.get(event).push(handler); 36 | } else { 37 | this.handlers.set(event, [handler]); 38 | } 39 | } 40 | 41 | private attachHandlers(client: WebSocketClient): void { 42 | const getObject = (data: RawData): any => { 43 | try { 44 | return JSON.parse(String(data)); 45 | } catch { 46 | return undefined; 47 | } 48 | }; 49 | 50 | client.on('message', async (data) => { 51 | const obj = getObject(data); 52 | 53 | if (obj && obj.hasOwnProperty('event')) { 54 | if (this.handlers.has(obj.event)) { 55 | const functions = this.handlers.get(obj.event); 56 | const responses = []; 57 | for (const func of functions) { 58 | const res = await func(data); 59 | if (res) { 60 | responses.push(res); 61 | } 62 | } 63 | if (responses.length === 1) { 64 | client.send(JSON.stringify(responses[0])); 65 | } 66 | if (responses.length > 1) { 67 | client.send(JSON.stringify(responses)); 68 | } 69 | } 70 | } 71 | }); 72 | } 73 | } -------------------------------------------------------------------------------- /src/modules/websocket/websocket.module.ts: -------------------------------------------------------------------------------- 1 | import { Global, Module } from "@nestjs/common"; 2 | import { WebsocketGateway } from "./websocket.gateway"; 3 | import { WebsocketManager } from "./websocket.manager"; 4 | 5 | @Global() 6 | @Module({ 7 | providers: [ 8 | WebsocketGateway, 9 | WebsocketManager 10 | ], 11 | exports: [ 12 | WebsocketManager 13 | ] 14 | }) 15 | export class WebsocketModule {} -------------------------------------------------------------------------------- /test/app.e2e.ts: -------------------------------------------------------------------------------- 1 | import { Test, TestingModule } from '@nestjs/testing'; 2 | import { INestApplication } from '@nestjs/common'; 3 | import * as request from 'supertest'; 4 | import { AppModule } from '../src/modules/app/app.module'; 5 | 6 | 7 | describe('AppController (e2e)', () => { 8 | let app: INestApplication; 9 | 10 | beforeEach(async () => { 11 | const moduleFixture: TestingModule = await Test.createTestingModule({ 12 | imports: [AppModule], 13 | }).compile(); 14 | 15 | app = moduleFixture.createNestApplication(); 16 | await app.init(); 17 | }); 18 | 19 | it('/ (GET)', () => { 20 | return request(app.getHttpServer()) 21 | .get('/') 22 | .expect(200) 23 | .expect('Hello World!'); 24 | }); 25 | }); 26 | -------------------------------------------------------------------------------- /tsconfig.build.json: -------------------------------------------------------------------------------- 1 | { 2 | "extends": "./tsconfig.json", 3 | "exclude": ["node_modules", "dist", "**/*spec.ts"] 4 | } 5 | -------------------------------------------------------------------------------- /tsconfig.json: -------------------------------------------------------------------------------- 1 | { 2 | "compilerOptions": { 3 | "module": "commonjs", 4 | "declaration": true, 5 | "removeComments": true, 6 | "emitDecoratorMetadata": true, 7 | "experimentalDecorators": true, 8 | "allowSyntheticDefaultImports": true, 9 | "target": "ES2021", 10 | "sourceMap": true, 11 | "outDir": "./dist", 12 | "baseUrl": "./", 13 | "incremental": true, 14 | "skipLibCheck": true, 15 | "strictNullChecks": false, 16 | "noImplicitAny": false, 17 | "strictBindCallApply": false, 18 | "forceConsistentCasingInFileNames": false, 19 | "noFallthroughCasesInSwitch": false 20 | }, 21 | "include": ["src", "test"], 22 | } 23 | --------------------------------------------------------------------------------