Easiest way to talk to a Rust/wasm32 app running on localhost Chrome?

I have a crate that builds on both x86_64 and wasm32.

On x86_64, I can easily get a 'REPL' as follows:

  • setup a http post handler
  • evaluate the incoming msg
  • return the result
    I can get a very basic 'REPL' by sending post msgs over curl

Now, consider this app built on wasm32. One way to do this communication is:

localhost/chrome <- websocket -> local http server <-> curl

Here, the local http server is functioning as a 'router'

  • curl send msg to http server via POST
  • http server, via websocket, passes data to localhost/chrome
  • localhost/chrome, via websocket, sends data back to http server
  • http server sends response back to curl

Question: is there anyway to cut out the http server ? I.e.

local program <- ??? -> localhost/chrome

It sounds like you are trying to use some code running inside a page/worker on Chrome as a server, but as far as I know both the browser and curl are intended as clients.

I'd be quite surprised if a browser let you start a TCP listener, so that rendezvous HTTP server might be necessary.

Alternatively, what about creating a helper program that starts up the websockets server and prints a URL you can open in your browser. Then when Chrome connects you do the normal websockets thing to get a REPL.

1 Like

This, unfortunately, matches my current understanding. I was hoping there was some cool technique I was unaware of.

You might be interested in this.

1 Like

I think I did the thing you want to do in this game I wrote. The code is here and a playable version of the game is here.

Originally, I had the game run on a server, communicating with the player’s browser via a websocket. I wanted to host the game on Github Pages, so I used wasm to put the game in the player’s browser. It works pretty well, though this means the AI is weaker on phones than on computers.

Instead of a websocket, the browser now uses a webworker so that thinking won’t make the user interface less responsive. Only the webworker can call into the compiled wasm.

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.