microsoft / WSL

Issues found on WSL
https://docs.microsoft.com/windows/wsl
MIT License
17.44k stars 822 forks source link

Can't connect to WSL2 application via Websockets when using Postman like application #9549

Open filipe-costa opened 1 year ago

filipe-costa commented 1 year ago

Version

Microsoft Windows [Version 10.0.22621.1105]

WSL Version

Kernel Version

5.15.79.1

Distro Version

Ubuntu 22.04

Other Software

Postman for Windows, version 10.8.7 Insomnia for Windows, version 2022.7.5

Repro Steps

Demo application - https://github.com/filipe-costa/repro-demo-wsl2-websockets

Expected Behavior

I expected to be able to connect to the WSL2 application that has a WebSocket endpoint without any issues. On the other hand, it is possible to connect to it via a Web Browser.

Actual Behavior

Could not connect to ws://localhost:3000/
Error: connect ECONNREFUSED 127.0.0.1:3000

Similar issues: https://github.com/microsoft/WSL/issues/7849 https://github.com/microsoft/WSL/issues/4204

Diagnostic Logs

WslLogs-2023-01-27_18-46-42.zip

filipe-costa commented 1 year ago

Some screenshots to aid the diagnostic logs.

Regular HTTP requests go through without an issue:

ca0d454f5176c8591a0519ee809d29ec0b65a69b

Fails for WebSockets:

db99b46d790a3329fd351c23aa7788ced0468ee6

Works within browser context:

0f921b37f7d33eae565db160440e1cf432f7b4bf
GimpMaster commented 1 year ago

I'm seeing this same issue. My guess is that they automatically try connecting to 127.0.0.1 which WSL doesn't like.

m-salman-afzal commented 1 year ago

I have same issue. I'm unable to connect to either Postman or any other application.

m-salman-afzal commented 1 year ago

@filipe-costa My issue was server was started on ipv6 which was unable to listen. You can use sudo lsof -i -P -n | grep LISTEN to check if your port is ipv6 or ipv4. If ipv6, then try putting 0.0.0.0 after port. The issue for connection refused will be solved.

BUT, you might get a new error socket hang up. Now, this will be because you are using socket.io library and going through the documentation, it seems like it expects socket.io-client, its own library for proper handshake to establish a connection. So, we might not be able to do it from postman at all.

What you can try is using the base ws library which is barebone of websockets (upon which socket.io runs). I was able to establish a connection through it. But being barebone, you will have to do everything yourself if you go down this road.

m-salman-afzal commented 1 year ago

Also, just going through postman in detail, it does have a separate request type for socket.io and it works.

AscTh commented 7 months ago

@filipe-costa To make a postman call, you must specify the wsl ip address, not localhost. You can find it using the command wsl hostname -I. The result will look like this.

изображение

However, this does not work for socket.io And I haven't figured out why yet.