question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Websocket example doesn't work

See original GitHub issue

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:

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Comments:13 (3 by maintainers)

github_iconTop GitHub Comments

3reactions
yoiangcommented, Nov 26, 2019

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

2reactions
kamilmysliwieccommented, Jan 21, 2018

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

Read more comments on GitHub >

github_iconTop Results From Across the Web

Websockets not working - Stack Overflow
Try this example: var url = "ws://echo.websocket.org"; if (!window.WebSocket) alert("WebSocket not supported by this browser"); var myWebSocket = { connect: ...
Read more >
WebSocket - The Modern JavaScript Tutorial
A simple example. To open a websocket connection, we need to create new WebSocket using the special protocol ws in the url:.
Read more >
Troubleshooting connection issues | Socket.IO
You are trying to reach a plain WebSocket server; The server is not reachable; The client is not compatible with the version of...
Read more >
Writing WebSocket client applications - Web APIs | MDN
WebSocket client applications use the WebSocket API to communicate with WebSocket servers using the WebSocket protocol.
Read more >
How do I troubleshoot WebSocket issues in Agents (All ...
Ensure any load balancers or reverse proxies configured in your environment support WebSocket protocols. If they do not, you will see errors such...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found