Browser page reload on web server rebuild

Good day

I'm going thru ch20 of the Rust book about tiny web server implementation.

How can I implement automatic browser window reload on source code rebuild and restart with cargo watch?

PS:

Now I use interval VSCode browser opened in side tab with source code, but I plan to move to Google Chrome to make WASM part later.

PSS:

I want to write some sort of Node-RED analog for use as tiny SOHO IoT server distributed as a single .executable binary without any files in set -- all resources should be compiled into a server.exe with include_bytes!().

That's gonna require a frontend, isn't it? Also, you will need lots of new code to communicate between your client and your server to make sure your client knows when your code in your backend is updated. I think you're probably gonna write one yourself, but use a community library instead.

The quick and dirty solution would probably be some sort of long running connection initiated from the front end (websocket, Server Sent Events, etc.) and have the front end try to reconnect relatively quickly after a disconnect.

Then you can use something simple like cargo watch and when the front end sees a disconnect go into a successful reconnect it can force a reload.

The long running connection doesn't need to send any data here, other than what's required to keep it alive (ping/pong messages in the case of websockets).

Have a look at the tower-livereload crate. Claims to work with all Rust web server frameworks.

1 Like