-
-
Notifications
You must be signed in to change notification settings - Fork 7.6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Socket Gateway Dependency Injection Not Working #13948
Comments
I had to modify your reproduction significantly to even get it to install, compile, and start, and now I'm not sure how you expect us to test the gateway injection. Please provide a minimal reproduction that works and easily shows the problem. |
Sorry about that, was in a rush. Updated the reproduction to be working |
this is just an idea on top of my head, will take out some time to actually test this you need to use the import { Logger } from "@nestjs/common";
@UseFilters(new WsCatchAllFilter())
@WebSocketGateway({
transports: ['websocket'],
})
export class SocketGateway implements OnGatewayConnection, OnGatewayDisconnect {
@WebSocketServer()
server: Server;
private logger: Logger = new Logger(SocketGateway.name);
// your methods here
} give it a try and let me know |
The issue is not logger, I have tried removing it and keeping only some other service injected, the issue persists. The problem is that it cannot inject any dependency |
Is there an existing issue for this?
Current behavior
The dependencies do not get injected for the following code:
What I noticed is that constructor does not get called at all and produces the following error:
this.logger.debug('New client connected. Verifying token...', { id: client.id }); ^ TypeError: Cannot read properties of undefined (reading 'debug') at SocketGateway.handleConnection (/Users/utkuturkoglu/projects/turkoglu/phoenix-new/phoenix-api-gateway/src/socket/gateways/socket.gateway.ts:26:19) at Object.next (/Users/utkuturkoglu/projects/turkoglu/phoenix-new/phoenix-api-gateway/node_modules/@nestjs/websockets/web-sockets-controller.js:77:47) at ConsumerObserver.next (/Users/utkuturkoglu/projects/turkoglu/phoenix-new/phoenix-api-gateway/node_modules/rxjs/src/internal/Subscriber.ts:161:25) at SafeSubscriber._next (/Users/utkuturkoglu/projects/turkoglu/phoenix-new/phoenix-api-gateway/node_modules/rxjs/src/internal/Subscriber.ts:119:22) at SafeSubscriber.next (/Users/utkuturkoglu/projects/turkoglu/phoenix-new/phoenix-api-gateway/node_modules/rxjs/src/internal/Subscriber.ts:75:12) at /Users/utkuturkoglu/projects/turkoglu/phoenix-new/phoenix-api-gateway/node_modules/rxjs/src/internal/operators/distinctUntilChanged.ts:173:22 at OperatorSubscriber._next (/Users/utkuturkoglu/projects/turkoglu/phoenix-new/phoenix-api-gateway/node_modules/rxjs/src/internal/operators/OperatorSubscriber.ts:70:13) at OperatorSubscriber.next (/Users/utkuturkoglu/projects/turkoglu/phoenix-new/phoenix-api-gateway/node_modules/rxjs/src/internal/Subscriber.ts:75:12) at /Users/utkuturkoglu/projects/turkoglu/phoenix-new/phoenix-api-gateway/node_modules/rxjs/src/internal/Subject.ts:68:20 at Object.errorContext (/Users/utkuturkoglu/projects/turkoglu/phoenix-new/phoenix-api-gateway/node_modules/rxjs/src/internal/util/errorContext.ts:29:5)
Minimum reproduction code
https://stackblitz.com/edit/nestjs-typescript-starter-hwu1qr?file=src%2Fsocket%2Fgateways%2Fsocket.gateway.ts
Steps to reproduce
Node Version: 20.10.0
"@nestjs/common": "10.4.1",
"@nestjs/core": "10.4.1",
"@nestjs/platform-socket.io": "^10.4.1",
"@nestjs/websockets": "^10.4.1",
"socket.io": "^4.7.5",
Expected behavior
Dependencies should be injected
Package
@nestjs/common
@nestjs/core
@nestjs/microservices
@nestjs/platform-express
@nestjs/platform-fastify
@nestjs/platform-socket.io
@nestjs/platform-ws
@nestjs/testing
@nestjs/websockets
Other package
No response
NestJS version
10.4.1
Packages versions
Node.js version
20.10.0
In which operating systems have you tested?
Other
No response
The text was updated successfully, but these errors were encountered: