Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NestJS WebSocketGateway does not initialize

According to the NestJS docs I've implemented the websockets gateway and providing it inside of the AppModule. The server is starting properly and I can serve static assets via http successfully. But I can't get the websockets running at all, the ws server is not available at ws://localhost:3333 and the afterInit function is not executed at all. Even not when I am defining the @SubscribeMessage.

The gateway is implemented as

@WebSocketGateway()
export class SocketGateway implements OnGatewayInit {
  afterInit() {
    console.log('Gateway initialized');
  }
}

The AppModule provides the gateway properly

@Module({
  providers: [SocketGateway]
})
export class AppModule {}

And this is the bootstrap implementation

export async function bootstrap() {
  let app = await NestFactory.create(AppModule);

  await app.listen(process.env.port || 3333, () => {
    console.log(`Listening at http://localhost:${port}`);
  });
}

bootstrap();

My dependecies are

"socket.io-client": "^2.2.0",
"@nestjs/common": "5.5.0",
"@nestjs/core": "5.5.0",
"@nestjs/platform-socket.io": "^6.1.0",
"@nestjs/websockets": "^6.1.0"

Maybe you see the problem directly. Thanks for your help, cheers!

like image 534
Felix Lemke Avatar asked Jul 23 '26 07:07

Felix Lemke


1 Answers

In your project, the major versions v5 and v6 of nest are mixed. Different major versions are not guaranteed to interoperate properly. Update all your dependencies to nest v6; you can have a look at the migration guide for additional information about the update.

Run $ npm i @nestjs/core@latest @nestjs/common@latest


When you install new dependencies, watch out for peer dependency warnings from npm like this one:

npm WARN @nestjs/[email protected] requires a peer of @nestjs/common@^6.0.0 but none is installed.
You must install peer dependencies yourself.
like image 137
Kim Kern Avatar answered Jul 24 '26 20:07

Kim Kern



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!