Felix Lemke
Felix Lemke

Reputation: 6488

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!

Upvotes: 7

Views: 6212

Answers (1)

Kim Kern
Kim Kern

Reputation: 60357

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.

Upvotes: 12

Related Questions