Nest: Websocket example doesn't work

Created on 21 Jan 2018  路  13Comments  路  Source: nestjs/nest

I'm submitting a...


[ ] Regression 
[ ] Bug report
[ ] Feature request
[x] Documentation issue or request
[ ] Support request => Please do not submit support request here, instead post your question on Stack Overflow.

Current behavior


Websocket example doesn't work

Minimal reproduction of the problem with instructions


I cloned master, installed example 02 dependencies and ran it. When I open ws.html, it says WebSocket connection to 'ws://localhost:81/' failed: Error in connection establishment: net::ERR_CONNECTION_REFUSED. Looking at the code, the port should be the default (3000), not 81. If I change it to localhost:3000 it still doesn't work but the error is now ws.html:4 WebSocket connection to 'ws://localhost:3000/' failed: Connection closed before receiving a handshake response

Environment


Nest version: 4.5.8 (master)


For Tooling issues:
- Node version: 6.11.1  
- Platform:  MacOS 10.12.6 

Others:

type

Most helpful comment

As mentioned by @kamilmysliwiec this fixed it for me:

import { NestFactory } from '@nestjs/core'
import { WsAdapter } from '@nestjs/platform-ws' // <---
import { AppModule } from './app.module'

async function bootstrap() {
  const app = await NestFactory.create(AppModule)
  app.useWebSocketAdapter(new WsAdapter(app)) // <---
  await app.listen(3000)
}
bootstrap()

I did not find it necessary to specify a unique port in the WebSocket Gateway definition

All 13 comments

Hi @andrewbents,
Fixed, let's try now.

@kamilmysliwiec
the client in index.html kind of works, but not in ws.html using plain websockets. You also forgot to change the port there, but If I set it to 3000 it still says Connection closed before receiving a handshake response. Am I missing something?

To use ws.html you need to set up a ws adapter (it'll force Nest to use ws instead of socket.io). Full path /src/common/adapters/ws-adapter.ts, tutorial https://docs.nestjs.com/websockets/adapter

@kamilmysliwiec still doesn't work. I use this adapter (app.useWebSocketAdapter(new WsAdapter());) and the error is the same

I have the same problem.

@kamilmysliwiec Can you try the ws example?

@fengcen You should also define port there - https://github.com/nestjs/nest/blob/master/examples/02-gateways/src/events/events.gateway.ts#L12
@WebSocketGateway({ port: 3001})

@vologab Can I use the same port for http and websocket ?

not working for me either

why is it closed?

Still does not work for me

As mentioned by @kamilmysliwiec this fixed it for me:

import { NestFactory } from '@nestjs/core'
import { WsAdapter } from '@nestjs/platform-ws' // <---
import { AppModule } from './app.module'

async function bootstrap() {
  const app = await NestFactory.create(AppModule)
  app.useWebSocketAdapter(new WsAdapter(app)) // <---
  await app.listen(3000)
}
bootstrap()

I did not find it necessary to specify a unique port in the WebSocket Gateway definition

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.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

janckerchen picture janckerchen  路  3Comments

tronginc picture tronginc  路  3Comments

rlesniak picture rlesniak  路  3Comments

anyx picture anyx  路  3Comments

mishelashala picture mishelashala  路  3Comments