7

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!

2
  • 1
    You are mixing nest v5 and nest v6. Different major versions are not guaranteed to interoperate properly. Please update all your nest dependencies to v6, see docs.nestjs.com/migration-guide Commented Apr 13, 2019 at 12:37
  • 1
    This starts the gateway properly :) it is an nrwl/nx workspace including the 5.5.0 deps. Adding additional packages via npm installed 6.1.0. Commented Apr 13, 2019 at 12:44

1 Answer 1

12

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.
Sign up to request clarification or add additional context in comments.

2 Comments

apparently this is also true for minors. In my case in different packages for nest I had ^9.0 and ^9.4. bumped all and it started :/
this was very time consuming stupid bug to fix, thank you!!!

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.