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.

Feature request: Support static WebSockets `wss://` and `ws://` port(s) via env var `DOTNET_WATCH_AUTO_RELOAD_WS_ENDPOINT` in `dotnet watch`

See original GitHub issue

Disclaimer: Iā€™m new here, so please let me know if Iā€™m doing anything at all wrong and Iā€™ll fix it. šŸ˜„

Is your feature request related to a problem? Please describe.

Each time dotnet watch restarts (manually, or in response to a rude edit, etc) random free ports (via port :0) are chosen for the ws:// and wss:// WebSockets connection,

I can appreciate why this feature defaults to ā€œa random free portā€ (i.e. you want to make the feature ā€œjust workā€, rather than trying to bind to a user-defined static port), but this has some negative effects of the developer experience, i.e. that:

  1. The WebSockets connection(s) cannot be automatically resumed (reconnected to the existing browser tabs) by aspnetcore-browser-refresh.js, as the ports have changed.
  2. Practically, this means that you need to F5 the browser window, to reload the page, so that aspnetcore-browser-refresh.js will pick up the new WS ports.
  3. Thus, no more ā€œhotā€ or even ā€œliveā€ reload for you, until you F5.

This issue has been raised twice before:

  1. https://github.com/dotnet/aspnetcore/issues/39608
  2. https://github.com/dotnet/aspnetcore/issues/33823

Describe the solution youā€™d like

Setting the environment variable as follows would allow developers to opt-in to static wss:// and ws:// WebSockets ports to be specified, e.g. like:

DOTNET_WATCH_AUTO_RELOAD_WS_ENDPOINT=https://localhost:43399,http://localhost:8099

Iā€™d like to propose this as a minor change to .NET SDK 7.x (as this can be shipped as an additive non-breaking change).

I have a PR here which shows an example implementation.

See: https://github.com/dotnet/sdk/pull/31575/files for the code.

Additional context

The original (implemented and closed) browser live reload feature (as envisaged by @DamianEdwards) even covered the scenario weā€™re talking about here, but it doesnā€™t work because of the random port selection on restart.

The JS block should treat any disconnection of the WebSocket as indication it needs to perform the browser refresh. In the case of dotnet watch, this should only occur after it has been determined that the application server has successfully restarted after the project has been rebuilt and restarted. ā€“ Damian Edwards

See: https://github.com/dotnet/aspnetcore/issues/23412#issuecomment-655205989 for more details.

Challenges I am having

Iā€™m currently struggling to work out how to build .NET SDK 7.x from source. I have it building from source as a patch to .NET SDK 8, but my example app doesnā€™t work currently when ported to .NET 8, so itā€™s making it hard to test the patch locally. Please point me to the correct branch and build ā€œhow toā€ and Iā€™m happy to rebase this from the 7.x branch.

Update: Iā€™ve found branches release/7.0.2xx, release/7.0.3xx, and release/7.0.4xx, which all compile to produce v7 of the SDK fine, but apologies, Iā€™m not sure if Iā€™m supposed to raise PRs against one/many of those, or some other upstream 7.x branch?

Weā€™d also need to adjust the behaviour of aspnetcore-browser-refresh.js slightly so that it will e.g. window.location.reload() when it detects a subsequent drop and reconnection of the WebSocket(s), but at this stage, Iā€™m just focussed on seeing if this change to static ports is one thatā€™s amenable to the team.

Thanks, and I appreciate you taking the time to consider my proposal. šŸ„°

Cheers,

Tod.

cc: @tmat @arkalyanms (as code owners of Area-Watch)

Issue Analytics

  • State:open
  • Created 5 months ago
  • Comments:9 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
todthomsoncommented, Apr 14, 2023

Yep those are totally fair concerns. One of the hardest part of these kinds of tooling experiences is coordinating all the moving pieces to avoid those dreaded browser error screens. I admit Iā€™m surprised dotnet watch is restarting the websocket server after each app restart, as ultimately thatā€™s what causing the browser to lose the connection and thus not get the signal to refresh. Iā€™m trying to cast my mind back but I canā€™t remember why that would be the case. Perhaps that can be revisited.

Appreciate that mate. I canā€™t really say for sure that dotnet watch is restarting, only that when a ā€œrude editā€ happens, and dotnet watch chooses to reboot dotnet run, then WebSockets connections end up with new ports, which as you say means the connection is (sadly) lost for good (until the F5 grabs the new port configuration from the server).

Iā€™ll experiment and report back asap. Thanks.

1reaction
todthomsoncommented, Apr 14, 2023

Presuming it doesnā€™t result in a 404 because the web server is restarting, and it doesnā€™t leave everyone elseā€™s browsers with blank tabs, I am all for it. šŸ˜ƒ

Iā€™ll experiment and report back asap.

Read more comments on GitHub >

github_iconTop Results From Across the Web

WebSockets support in ASP.NET Core
This article explains how to get started with WebSockets in ASP.NET Core. WebSocket (RFC 6455) is a protocol that enables two-way persistentĀ ...
Read more >
dotnet watch command - .NET CLI
The dotnet watch command is a file watcher that runs a dotnet command when changes in source code are detected.
Read more >
.NET 6 Hot Reload and "Refused to connect to ws ...
But port numbers change, right? Let's do just wss:, only in Development. Now, if I'm using both CSPs and WebSockets (ws:, wss:) in...
Read more >
Connecting to Websocket on shared hosting when port is ...
I built a program in c# (asp.net) that connects to wss://stream.binance.com:9443/ws using WebSocket4Net, and gets ticker data which is ...
Read more >
Connect via websockets - Render
Is it possible to connect to a websocket server? Here is a snippet of my server code: const ws_port = process.env.PORT; const wss...
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