Skip to content

Commit 340fbf9

Browse files
committed
doc: expand hello example
Signed-off-by: Roman Volosatovs <rvolosatovs@riseup.net>
1 parent ef91798 commit 340fbf9

1 file changed

Lines changed: 84 additions & 35 deletions

File tree

README.md

Lines changed: 84 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -39,53 +39,102 @@ wRPC fully supports the unreleased native [WIT] `stream` and `future` data types
3939

4040
## Quickstart
4141

42-
Here is a guide demonstrating how to bootstrap the Wasm component based example implemented by
43-
[examples/rust/hello-component-server](examples/rust/hello-component-server) and
44-
[examples/rust/hello-component-client](examples/rust/hello-component-client).
42+
wRPC usage examples for different programming languages can be found at [./examples](./examples).
4543

46-
> There are 2 different kinds of examples
47-
> - Native wRPC applications, using a particular wRPC transport (currently, NATS only)
48-
> - Generic Wasm components, that run in a Wasm runtime
44+
There are 2 different kinds of examples:
45+
- Native wRPC applications, tied to a particular wRPC transport (currently, NATS only)
46+
- Generic Wasm components, that need to run in a Wasm runtime. Those can be executed, for example, using `wrpc-wasmtime-nats`, to polyfill imports at runtime and serve exports using wRPC.
4947

50-
### Generic Wasm components example
48+
Here is a guide demonstrating how to bootstrap the Rust Wasm component example implemented by:
49+
- [examples/rust/hello-component-server](examples/rust/hello-component-server)
50+
- [examples/rust/hello-component-client](examples/rust/hello-component-client)
5151

52-
#### Requirement
53-
- `docker` >= 24.0.6 or `nats-server` >= 2.10.20
52+
### `hello` example
53+
54+
In this example we will serve and invoke a simple [`hello`](./examples/wit/hello/hello.wit) application
55+
56+
#### Requirements
57+
- `nats-server` >= 2.10.20 or `docker` >= 24.0.6 (or any other OCI runtime)
5458
- `rust` >= 1.80.1
5559

5660
#### How-To
57-
1. Add the `wasm32-wasi` target
58-
```bash
59-
rustup target add wasm32-wasip1
61+
62+
In this example we will be using `wasm32-wasip1` target, which is available in stable Rust and configured in [`rust-toolchain.toml`](./rust-toolchain.toml) in the root of this repository. [`wasm32-wasip2`](https://doc.rust-lang.org/nightly/rustc/platform-support/wasm32-wasip2.html) can be used as well.
63+
64+
1. Build the client Wasm component:
65+
```sh
66+
cargo build --release -p hello-component-client --target wasm32-wasip1
6067
```
61-
1. Build the server WASM component
62-
```bash
68+
69+
> Output is in target/wasm32-wasip1/release/hello-component-client.wasm
70+
71+
1. Build the server Wasm component:
72+
```sh
6373
cargo build --release -p hello-component-server --target wasm32-wasip1
6474
```
65-
> Output is target/wasm32-wasi/release/hello_component_server.wasm.
66-
1. Build the client WASM component
67-
```bash
68-
cargo build --release -p hello-component-client --target wasm32-wasip1
75+
76+
> Output is in target/wasm32-wasip1/release/hello_component_server.wasm
77+
> NB: Rust uses `_` separators in the filename, because a component is built as a reactor-style library
78+
79+
1. Build the wRPC Wasm runtime:
80+
```sh
81+
cargo build --release --bin wrpc-wasmtime-nats
82+
```
83+
84+
> Output is in target/release/wrpc-wasmtime-nats or target/release/wrpc-wasmtime-nats.exe on Windows
85+
86+
1. Run NATS (more thorough documentation available [here](https://docs.nats.io/running-a-nats-service/introduction/running)):
87+
```sh
88+
nats-server
89+
```
90+
91+
- (optional) using Docker:
92+
```sh
93+
docker run --rm -it --name nats-server -p 4222:4222 nats:2.10.20-alpine3.20
94+
```
95+
96+
1. Serve the `hello` application using the Wasm component:
97+
```sh
98+
./target/release/wrpc-wasmtime-nats serve rust rust ./target/wasm32-wasip1/release/hello_component_server.wasm
99+
```
100+
101+
Sample output:
102+
> INFO async_nats: event: connected
103+
> INFO wrpc_wasmtime_nats_cli: serving instance function name="hello"
104+
105+
1. Call the server Wasm component from the client Wasm component:
106+
```sh
107+
./target/release/wrpc-wasmtime-nats run rust ./target/wasm32-wasip1/release/hello-component-client.wasm
108+
```
109+
110+
Sample output in the client:
111+
> INFO async_nats: event: connected
112+
>hello from Rust
113+
114+
Sample output in the server:
115+
> INFO wrpc_wasmtime_nats_cli: serving instance function invocation headers=None
116+
> INFO wrpc_wasmtime_nats_cli: successfully served instance function invocation
117+
118+
1. Call the server Wasm component using a native wRPC client application:
119+
```sh
120+
cargo run -p hello-nats-client
69121
```
70-
> Output is target/wasm32-wasi/release/hello-component-client.wasm.
71-
1. Run NATS service
72-
- Using docker
73-
```bash
74-
docker run --rm -it --name nats-server -p 4222:4222 nats:2.10.20-alpine3.20
75-
```
76-
- Or run NATS server directly by following the instructions [here](https://docs.nats.io/running-a-nats-service/introduction/running)
77-
1. Serve the server WASM component
78-
```bash
79-
cargo run --release --bin wrpc-wasmtime-nats -- serve -g hello rust rust ./target/wasm32-wasip1/release/hello_component_server.wasm
122+
123+
1. Serve the `hello` using a native wRPC server application:
124+
```sh
125+
cargo run -p hello-nats-server native
80126
```
81-
1. Call the server WASM component from the client WASM component
82-
```bash
83-
cargo run --release --bin wrpc-wasmtime-nats -- run rust ./target/wasm32-wasip1/release/hello-component-client.wasm
127+
128+
It can be called using either the component client or a native wRPC client
129+
130+
1. Call both the native wRPC `hello` server and component wRPC `hello` server using a native wRPC client application:
131+
```sh
132+
cargo run -p hello-nats-client rust native
84133
```
85-
Sample output in client side goes as
86-
```bash
87-
INFO async_nats_wrpc: event: connected
88-
hello from Rust
134+
135+
1. Call native wRPC `hello` server using the client Wasm component
136+
```sh
137+
./target/release/wrpc-wasmtime-nats run native ./target/wasm32-wasip1/release/hello-component-client.wasm
89138
```
90139

91140
## Design

0 commit comments

Comments
 (0)