Load a WebAssembly Function Written in Rust and Invoke it from JavaScript

InstructorNik Graf

Share this video with your friends

Send Tweet

In this lesson we are going to setup a project from scratch by introducing the JavaScript snippet to load a WebAssembly module. We demonstrate two different ways and showcase the benefit of the streaming solution. Once the module is loaded we can invoke a function previously exported from our Rust code.

Nik Grafinstructor
~ 6 years ago

@Steve totally, just pinged the Egghead team to fix it. thx!

Juliette
~ 6 years ago

Hi Nik,

When I run http (from the utils directory) in my terminal, this prompt is returned:

http: error: the following arguments are required: URL

When I run ```http url``, I get an error:

http: error: ConnectionError: HTTPConnectionPool(host='url', port=80): Max retries exceeded with url: / (Caused by NewConnectionError('<requests.packages.urllib3.connection.HTTPConnection object at 0x101bff518>: Failed to establish a new connection: [Errno 8] nodename nor servname provided, or not known',)) while doing GET request to URL: http://url/

What would be the best way to troubleshoot this issue?

Thanks so much :-)

Mirza Pasic
~ 6 years ago

@Juliette you're not invoking rust http server, your http is most likely an alias for the httpie client. Try this: ~/.cargo/bin/http

Valery Kobzar
~ 6 years ago

Cargo.toml:

... [lib] path = "src/lib.rs" crate-type = ["cdylib"]

Without 'path' variable cargo dont produce utils.wasm file in target/wasw32-unknown-unknown directory

Juliette
~ 6 years ago

Thank you @Mirza! I'm going to give that a try tomorrow after my first cup of coffee or so. Cheers:-)

Nik Grafinstructor
~ 6 years ago

Thanks for helping @Mirza

EdmundsEcho
~ 5 years ago

FYI I needed to prefix my build commands with rustup run nightly cargo build ... in order to avoid crate errors and a suggestion that wasm32-unknown-unknown target may not be installed.

Andrey Kolybelnikov
~ 5 years ago

WebAssembly.instantiateStreaming is not supported in Safari, and either in Safari iOS, just in case someone like me is going to wonder why the results are different in Chrome and Safari: https://caniuse.com/#search=instantiateStreaming

Jeremy Swanborough
~ 5 years ago

I don't have an alias for http, command not found: http also ~/.cargo/bin/http doesn't exist, is there a way to install it?