Hacker News new | ask | show | jobs
by nevi-me 2888 days ago
Hi chinedufn, this looks very interesting. I am at the same stage as you re. "I REALLY want to use this for everything ...".

I see you're using wasm_bindgen quite a bit, but from a cursory look at the code; I can't figure out something:

I can see how you're using wasm in the front-end, but could you please elaborate a bit more about in the backend? I presume that your backend is Rust based?

I spent last weekend porting some code from NodeJS to a wasm module. I got it sort of working on Node, but haven't tried it on the browser yet (ran out of weekend).

I'm quite optimistic about wasm [and Rust with wasm_bindgen]. I'd been thinking of writing a native add-on for Node, but the idea that I can use wasm instead is exciting.

1 comments

Sure!

The backend is pretty much this (messy) file - https://github.com/chinedufn/percy/blob/master/examples/isom... .

It

1. Pulls in your application crate 2. Initializes your app (more or less sets initial state 3. Renders your app's virtual DOM into an HTML string 4. Serves the HTML to the client, along with the initial state serialized into JSON in a script tag (using serde-json) 5.Also serves the WASM script and the JS that initializes the WASM

So that's your server crate. Then your client crate also pulls in your same application crate and you compile it to WebAssembly and that's what runs browser side.

Feel free to let me know if any of that was poorly explained!

Thanks, that makes sense. I haven't built web servers with Rust as yet; so it was a bit difficult to follow the code.
This is good enough for an example, but for a "real" web server, you wouldn't do this; you'd use a framework that handles a lot of this kind of thing for you. I think this code was adapted from the final chapter of the Rust book, which is mostly about learning how to write a simple thing than being production-ready.