Skip to content
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

WebSocket Gateway - doesn't work with ExpressAdapter #2355

Closed
adrianpiesiak opened this issue Jun 6, 2019 · 4 comments
Closed

WebSocket Gateway - doesn't work with ExpressAdapter #2355

adrianpiesiak opened this issue Jun 6, 2019 · 4 comments
Labels
needs triage This issue has not been looked into

Comments

@adrianpiesiak
Copy link

Bug Report

Current behavior

WebSocket Gateway is not working properly when Nest application is created by using ExpressAdapter.

It seems that the gateway is not linked properly to the http service - client gets 404 upon connection, gateway itself seems to be initiated (OnGatewayInit is executed with meaningful result).

Input Code

I've created minimal code to reproduce.
In the main.ts there are two bootstrap method implementations - one works, second one doesn't.

There is client.js file to simulate the client. Simply run it with node client.js - it should produce console logs in both server (when socket is properly connected) and in client (upon connection error).

https://github.com/adrianpiesiak/NestJs-socket-bug

Expected behavior

The WebSocketGateway should work in both bootstrapping methods

Environment

Node: 10.15.2
@nestjs/core@6.3.1
@nestjs/common@6.3.1
@nestjs/platform-socket.io@6.3.1
@nestjs/platform-express@6.3.1

@adrianpiesiak adrianpiesiak added the needs triage This issue has not been looked into label Jun 6, 2019
@kfsjostrand
Copy link

Try to

const httpServer = await https.createServer(httpsOptions, server).listen(port);
app.useWebSocketAdapter(new IoAdapter(httpServer));
await app.init();

@kamilmysliwiec
Copy link
Member

kamilmysliwiec commented Jun 6, 2019

If you replace this line:

http.createServer(server).listen(3000);

with this line

await app.listen(3000);

it's gonna work fine. Why are you trying to spin up http server manually? You can access HTTP server created by Nest easily:

const httpServer = app.getHttpServer();

Also, you don't have to use ExpressAdapter to have full control over express instance - you can simply call this instead:

const express = app.getHttpAdapter().getInstance();

see https://docs.nestjs.com/faq/http-adapter

@adrianpiesiak
Copy link
Author

adrianpiesiak commented Jun 7, 2019

EDIT:
@kamilmysliwiec - The example wasn't showing the intention - which is, instantiate the app, so that you can get a some applications services instances injected into the main.ts file, so that you can read e.g. http / https / certificates / etc and apply them to the http server, without breaking the socket gateway ;)

Thanks for quick answer, but sadly your answer with just replacing the initialization to
await app.init()
isn't going to work for me, as it doesn't allow to specify https options.

@lock
Copy link

lock bot commented Nov 6, 2019

This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.

@lock lock bot locked as resolved and limited conversation to collaborators Nov 6, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
needs triage This issue has not been looked into
Projects
None yet
Development

No branches or pull requests

3 participants