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
67.69k stars 7.63k forks source link

Socket.io Gateway does send events, if method returns WsReponse object #12365

Closed tobiasmuecksch closed 1 year ago

tobiasmuecksch commented 1 year ago

Is there an existing issue for this?

Current behavior

I'm evaluating NestJS for our next socket.io Project. Therefore i cloned the official sample here: https://github.com/nestjs/nest/tree/master/sample/02-gateways

I've observed, that the server only responds to the identity event and not to the events event. I've added some console.logs and found out that the method is triggered correctly, but the given Response in form of an observable is not emitted at all.

Minimum reproduction code

https://github.com/nestjs/nest/tree/master/sample/02-gateways

Steps to reproduce

  1. Clone the official sample here: https://github.com/nestjs/nest/tree/master/sample/02-gateways
  2. Update all nestjs dependencies to 10.2.5
  3. Start server npm run start:debug
  4. Serve client `npx static-server . --port 1337``
  5. Open client example in browser http://localhost:1337
  6. Open console and see for yourself. The server only responds to the identity-event, which is implemented with a Promise. The server does not respond to events, which is implemented using an Observable<WsResponse>.

Expected behavior

The event events should be emitted three times, with the appropriate payloads (1, 2, 3).

Package

Other package

No response

NestJS version

10.2.5

Packages versions

{
  "name": "nest-typescript-starter",
  "version": "1.0.0",
  "description": "Nest TypeScript starter repository",
  "license": "MIT",
  "scripts": {
    "prebuild": "rimraf dist",
    "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 ./e2e/jest-e2e.json"
  },
  "dependencies": {
    "@nestjs/common": "10.2.5",
    "@nestjs/core": "10.2.5",
    "@nestjs/platform-express": "10.2.5",
    "@nestjs/platform-socket.io": "10.2.5",
    "@nestjs/websockets": "10.2.5",
    "@socket.io/redis-adapter": "8.2.1",
    "class-transformer": "0.5.1",
    "class-validator": "0.14.0",
    "reflect-metadata": "0.1.13",
    "rimraf": "5.0.1",
    "rxjs": "7.8.1",
    "socket.io": "4.7.2"
  },
  "devDependencies": {
    "@nestjs/cli": "10.1.17",
    "@nestjs/schematics": "10.0.2",
    "@nestjs/testing": "10.2.5",
    "@types/express": "4.17.17",
    "@types/jest": "29.5.4",
    "@types/node": "20.6.0",
    "@types/supertest": "2.0.12",
    "@types/ws": "8.5.5",
    "@typescript-eslint/eslint-plugin": "6.6.0",
    "@typescript-eslint/parser": "6.6.0",
    "eslint": "8.49.0",
    "eslint-config-prettier": "9.0.0",
    "eslint-plugin-import": "2.28.1",
    "jest": "29.6.4",
    "prettier": "3.0.3",
    "redis": "4.6.8",
    "supertest": "6.3.3",
    "ts-jest": "29.1.1",
    "ts-loader": "9.4.4",
    "ts-node": "10.9.1",
    "tsconfig-paths": "4.2.0",
    "typescript": "5.2.2"
  },
  "jest": {
    "moduleFileExtensions": [
      "js",
      "json",
      "ts"
    ],
    "rootDir": "src",
    "testRegex": ".*\\.spec\\.ts$",
    "transform": {
      "^.+\\.(t|j)s$": "ts-jest"
    },
    "collectCoverageFrom": [
      "**/*.(t|j)s"
    ],
    "coverageDirectory": "../coverage",
    "testEnvironment": "node"
  }
}

Node.js version

18.16.0

In which operating systems have you tested?

Other

No response

kamilmysliwiec commented 1 year ago

Just tested and confirmed that it works as expected image

Please, use our Discord channel (support) for such questions. We are using GitHub to track bugs, feature requests, and potential improvements.