websockets / ws

Simple to use, blazing fast and thoroughly tested WebSocket client and server for Node.js
MIT License
21.34k stars 2.3k forks source link

ws issues with custom hostname #2197

Closed turnerEP closed 5 months ago

turnerEP commented 5 months ago

Is there an existing issue for this?

Description

I run my application in WSL 2 on win 10
When I start my server like this

  const wss = new WebSocketServer({
    server: server,
    path: "/subscribe"
  });
 server.listen(port, "0.0.0.0", () => {
      logger.info(`${appName} ${version} listening on ${hostname}:${port}`, {
        context: logContext
      });
    });

I can't connect to the websocket with my test software. (ECONNREFUSED ::1:8080)
Normal http requests work as usual.

When I just listen without a given hostname everything works as intended. When deployed in a k8s environment there seems to be no issue

ws version

8.16.0

Node.js Version

v20.9.0

System

System: OS: Linux 5.15 Ubuntu 20.04.6 LTS (Focal Fossa) CPU: (6) x64 11th Gen Intel(R) Core(TM) i7-11800H @ 2.30GHz Memory: 8.76 GB / 15.62 GB Container: Yes Shell: 5.0.17 - /bin/bash

Expected result

I can connect to my local websocket when a hostname is set

Actual result

ECONNREFUSED ::1:8080

Attachments

No response

lpinca commented 5 months ago

You are forcing IPv4 via 0.0.0.0 but the client uses IPv6. Try this:

const ws = new WebSocket(url, { family: 4 });
turnerEP commented 5 months ago

You are forcing IPv4 via 0.0.0.0 but the client uses IPv6. Try this:

const ws = new WebSocket(url, { family: 4 });

I forgot to mention that I use the workflow with an existing http server. I updated my issue..
I did not notice that my client only uses ipv6 for localhost only... It is weird that normal http requests just work with localhost, while websocket requires 127.0.0.1 explicitly. I guess this issue only affects me with my old insonmia version...