nestjs / nest

A progressive Node.js framework for building efficient, scalable, and enterprise-grade server-side applications with TypeScript/JavaScript 🚀
https://nestjs.com
MIT License
66.9k stars 7.55k forks source link

The @SubscribeMessage decorator only applies to incoming events. When returning data, it does not automatically configure the corresponding event for the outgoing message. #13803

Closed Shawn211 closed 2 months ago

Shawn211 commented 2 months ago

Is there an existing issue for this?

Current behavior

After I generated a basic gateway using the scaffold and started using it, the value returned was not transmitted to the client. Upon debugging, I noticed that the final emit did not include the event specified by the decorator, forcing me to specify again which event to return.

image image

after compatibilizing

image image

So I wonder, is it my misuse or misunderstanding of decorators, or is there another default way to use return? Or is it a bug?

Minimum reproduction code

https://stackblitz.com/edit/nestjs-typescript-starter-ssx8xf?file=src%2Fws%2Fws.gateway.ts

Steps to reproduce

  1. npm i
  2. npm start
  3. ts-node client.ts
  4. watch and edit file ws.gateway.ts
  5. see error: When default code is used, the client cannot receive the returned message content.

Expected behavior

After the @SubscribeMessage decorator specifies the event, I don't have to explicitly indicate the event again with the @WsResponse decorator for the responding return.

Package

Other package

No response

NestJS version

10.3.10

Packages versions

{
  "name": "websocket-sample",
  "version": "0.0.1",
  "description": "",
  "author": "",
  "private": true,
  "license": "UNLICENSED",
  "scripts": {
    "build": "nest build",
    "format": "prettier --write \"src/**/*.ts\" \"test/**/*.ts\"",
    "start": "nest start",
    "start:dev": "nest start --watch",
    "start:debug": "nest start --debug --watch",
    "start:prod": "node dist/main",
    "lint": "eslint \"{src,apps,libs,test}/**/*.ts\" --fix",
    "test": "jest",
    "test:watch": "jest --watch",
    "test:cov": "jest --coverage",
    "test:debug": "node --inspect-brk -r tsconfig-paths/register -r ts-node/register node_modules/.bin/jest --runInBand",
    "test:e2e": "jest --config ./test/jest-e2e.json",
    "prepare": "husky"
  },
  "dependencies": {
    "@nestjs/common": "^10.0.0",
    "@nestjs/config": "^3.2.3",
    "@nestjs/core": "^10.0.0",
    "@nestjs/platform-express": "^10.0.0",
    "@nestjs/platform-socket.io": "^10.3.10",
    "@nestjs/websockets": "^10.3.10",
    "reflect-metadata": "^0.2.0",
    "rxjs": "^7.8.1"
  },
  "devDependencies": {
    "@commitlint/cli": "^19.3.0",
    "@commitlint/config-conventional": "^19.2.2",
    "@jest/globals": "^29.7.0",
    "@nestjs/cli": "^10.0.0",
    "@nestjs/schematics": "^10.0.0",
    "@nestjs/testing": "^10.0.0",
    "@types/express": "^4.17.17",
    "@types/jest": "^29.5.12",
    "@types/node": "^20.3.1",
    "@types/supertest": "^6.0.0",
    "@typescript-eslint/eslint-plugin": "^7.0.0",
    "@typescript-eslint/parser": "^7.0.0",
    "commitizen": "^4.3.0",
    "cz-conventional-changelog": "^3.3.0",
    "eslint": "^8.42.0",
    "eslint-config-prettier": "^9.0.0",
    "eslint-plugin-prettier": "^5.0.0",
    "husky": "^9.0.11",
    "jest": "^29.5.0",
    "lint-staged": "^15.2.7",
    "prettier": "^3.0.0",
    "source-map-support": "^0.5.21",
    "supertest": "^7.0.0",
    "ts-jest": "^29.2.0",
    "ts-loader": "^9.4.3",
    "ts-node": "^10.9.1",
    "tsconfig-paths": "^4.2.0",
    "typescript": "^5.1.3"
  },
  "jest": {
    "moduleFileExtensions": [
      "js",
      "json",
      "ts"
    ],
    "rootDir": "src",
    "testRegex": ".*\\.spec\\.ts$",
    "transform": {
      "^.+\\.(t|j)s$": "ts-jest"
    },
    "collectCoverageFrom": [
      "**/*.(t|j)s"
    ],
    "coverageDirectory": "../coverage",
    "testEnvironment": "node"
  },
  "lint-staged": {
    "*.{js,jsx,ts,tsx}": [
      "prettier --ignore-path .gitignore --write"
    ],
    "*.json,.{eslintrc,prettierrc}": [
      "prettier --ignore-path .gitignore --parser json --write"
    ],
    "*.{html,md,yml}": [
      "prettier --ignore-path .gitignore --single-quote --write"
    ]
  },
  "config": {
    "commitizen": {
      "path": "./node_modules/cz-conventional-changelog",
      "types": {
        "feat": {
          "title": "新功能",
          "description": "新功能"
        },
        "fix": {
          "title": "问题修复",
          "description": "问题修复"
        },
        "docs": {
          "title": "文档更新",
          "description": "文档更新"
        },
        "style": {
          "title": "格式优化",
          "description": "格式优化"
        },
        "refactor": {
          "title": "代码重构",
          "description": "代码重构"
        },
        "perf": {
          "title": "性能优化",
          "description": "性能优化"
        },
        "test": {
          "title": "测试更新",
          "description": "测试更新"
        },
        "build": {
          "title": "项目工具",
          "description": "项目工具"
        },
        "ci": {
          "title": "CI配置",
          "description": "CI配置"
        },
        "chore": {
          "title": "杂项",
          "description": "杂项"
        },
        "revert": {
          "title": "回退",
          "description": "回退"
        }
      }
    }
  }
}

Node.js version

v20.15.0

In which operating systems have you tested?

Other

No response

kamilmysliwiec commented 2 months ago

Thank you for taking the time to submit your report! From the looks of it, this could be better discussed on our Discord. If you haven't already, please join here and send a new post in the #⁠ 🐈 nestjs-help forum. Make sure to include a link to this issue, so you don't need to write it all again. We have a large community of helpful members, who will assist you in getting this to work.