Skip to content

Commit 8a7b1bf

Browse files
authored
Networking related docs update (#150)
* Update client.md Improve wording and links. Signed-off-by: Michael Yuan <michael@secondstate.io> * Remove duplicate TLS note Signed-off-by: Michael Yuan <michael@secondstate.io> * Improve writing and layout Signed-off-by: Michael Yuan <michael@secondstate.io> * Fix a typo Signed-off-by: Michael Yuan <michael@secondstate.io> * Change list style Signed-off-by: Michael Yuan <michael@secondstate.io> * Re-org socket client chapter Signed-off-by: Michael Yuan <michael@secondstate.io> * Update the socket server chapter Signed-off-by: Michael Yuan <michael@secondstate.io> * Use compile command Signed-off-by: Michael Yuan <michael@secondstate.io> * Use compile command Signed-off-by: Michael Yuan <michael@secondstate.io> * Use compile command Signed-off-by: Michael Yuan <michael@secondstate.io> * Update JS instructions Signed-off-by: Michael Yuan <michael@secondstate.io> * Use compile command Signed-off-by: Michael Yuan <michael@secondstate.io> * Update install instructions Signed-off-by: Michael Yuan <michael@secondstate.io> * fix broken links Signed-off-by: Michael Yuan <michael@secondstate.io> * Fix TLS plugin instructions Signed-off-by: Michael Yuan <michael@secondstate.io> * Improve HTTPS instructions Signed-off-by: Michael Yuan <michael@secondstate.io> --------- Signed-off-by: Michael Yuan <michael@secondstate.io>
1 parent 8ad1b1b commit 8a7b1bf

File tree

7 files changed

+85
-53
lines changed

7 files changed

+85
-53
lines changed

docs/develop/javascript/hello_world.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ Then download the pre-built WasmEdge QuickJS Runtime program, and optionally, AO
1919

2020
```bash
2121
curl -OL https://github.com/second-state/wasmedge-quickjs/releases/download/v0.5.0-alpha/wasmedge_quickjs.wasm
22-
wasmedgec wasmedge_quickjs.wasm wasmedge_quickjs.wasm
22+
wasmedge compile wasmedge_quickjs.wasm wasmedge_quickjs.wasm
2323
```
2424

2525
<!-- prettier-ignore -->

docs/develop/javascript/networking.md

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,16 @@ The QuickJS WasmEdge Runtime supports Node.js's `http` and `fetch` APIs via the
88

99
The networking API in WasmEdge is non-blocking and hence supports asynchronous I/O-intensive applications. With this API, the JavaScript program can open multiple connections concurrently. It polls those connections or registers async callback functions to process data whenever data comes in, without waiting for any one connection to complete its data transfer. That allows the single-threaded application to handle multiple, multiple concurrent requests.
1010

11-
- [Networking](#networking)
12-
- [Prerequisites](#prerequisites)
13-
- [Fetch client](#fetch-client)
14-
- [HTTP server](#http-server)
15-
- [TCP server and client](#tcp-server-and-client)
11+
- [Prerequisites](#prerequisites)
12+
- [Fetch client](#fetch-client)
13+
- [HTTP server](#http-server)
14+
- [TCP server and client](#tcp-server-and-client)
1615

1716
## Prerequisites
1817

19-
[See here](./hello_world#prerequisites)
18+
[Install WasmEdge](../../start/install.md). To make HTTPS requests, install the [WasmEdge TLS plug-in](../../start/install.md#tls-plug-in).
19+
20+
[Install WasmEdge-QuickJS](./hello_world#prerequisites). Make sure that the `modules` directory is located in your local directory where you want to execute the `wasmedge` command.
2021

2122
## Fetch client
2223

docs/develop/rust/http_service/client.md

Lines changed: 49 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
sidebar_position: 1
33
---
44

5-
# Client
5+
# HTTP client
66

77
WasmEdge allows Rust developers to use APIs they are already familiar with to access the Internet via the HTTP or HTTPS protocols. This chapter will cover HTTP client APIs and libraries to access external web services from your WasmEdge app. For HTTP servers in WasmEdge, please see [the next chapter](server).
88

@@ -11,16 +11,15 @@ WasmEdge allows Rust developers to use APIs they are already familiar with to ac
1111
Before we start, ensure [you have Rust and WasmEdge installed](../setup.md). To make HTTPS requests, install the [WasmEdge TLS plug-in](../../../start/install.md#tls-plug-in).
1212
:::
1313

14-
We will discuss HTTP and HTTPS asynchronous clients with hyper, synchronous clients with http_req, and simple clients with request. All of them are popular Rust crates for networking.
14+
We will discuss HTTP and HTTPS clients using popular Rust APIs.
1515

16-
- [Client](#client)
17-
- [Asynchronous client with hyper](#asynchronous-client-with-hyper)
18-
- [Synchronous client with http_req](#synchronous-client-with-http_req)
19-
- [reqwest client](#reqwest-client)
16+
- [The hyper API (recommended)](#the-hyper-api)
17+
- [The http_req API](#the-http_req-api)
18+
- [The reqwest API](#the-reqwest-api)
2019

21-
## Asynchronous client with hyper
20+
## The hyper API
2221

23-
Asynchronous HTTP or HTTPS requests do not block the execution of the calling application. It allows an application to make multiple concurrent HTTP requests and to process responses as they are received. That enables high-performance networking applications in WasmEdge. The [hyper crate](https://crates.io/crates/hyper) is a widely used Rust library to create HTTP and HTTPS networking applications.
22+
The [hyper crate](https://crates.io/crates/hyper) is a widely used Rust library to create HTTP and HTTPS networking applications. We recommend you use it in WasmEdge applications. A key feature of the `hyper` crate is that it is based on the `tokio` runtime, which supports asynchronous network connections. Asynchronous HTTP or HTTPS requests do not block the execution of the calling application. It allows an application to make multiple concurrent HTTP requests and to process responses as they are received. That enables high-performance networking applications in WasmEdge.
2423

2524
<!-- prettier-ignore -->
2625
:::note
@@ -36,20 +35,32 @@ cd wasmedge_hyper_demo/client
3635
# Build the Rust code
3736
cargo build --target wasm32-wasi --release
3837
# Use the AoT compiler to get better performance
39-
wasmedgec target/wasm32-wasi/release/wasmedge_hyper_client.wasm wasmedge_hyper_client.wasm
38+
wasmedge compile target/wasm32-wasi/release/wasmedge_hyper_client.wasm wasmedge_hyper_client.wasm
4039

4140
# Run the example
4241
wasmedge wasmedge_hyper_client.wasm
4342
```
4443

45-
In your Rust application, import [the WasmEdge adapted hyper crate](https://crates.io/crates/hyper_wasi), which uses a special version of single-threaded Tokio that is adapted for WebAssembly. Just add the following line to your Cargo.toml.
44+
The HTTPS version of the demo is as follows. Make sure that you install the [WasmEdge TLS plug-in](../../../start/install.md#tls-plug-in) first.
45+
46+
```bash
47+
// Build
48+
cd wasmedge_hyper_demo/client-https
49+
cargo build --target wasm32-wasi --release
50+
wasmedge compile target/wasm32-wasi/release/wasmedge_hyper_client_https.wasm wasmedge_hyper_client_https.wasm
51+
52+
// Run
53+
wasmedge wasmedge_hyper_client_https.wasm
54+
```
55+
56+
In your Rust application, import [the WasmEdge adapted hyper crate](https://crates.io/crates/hyper_wasi), which uses a special version of single-threaded Tokio that is adapted for WebAssembly. Just add the following line to your `Cargo.toml`.
4657

4758
```toml
4859
[dependencies]
4960
hyper_wasi = "0.15.0"
5061
```
5162

52-
The [Rust example code](https://github.com/WasmEdge/wasmedge_hyper_demo/blob/main/client/src/main.rs) below shows an HTTP or HTTPS GET request.
63+
The [Rust example code](https://github.com/WasmEdge/wasmedge_hyper_demo/blob/main/client/src/main.rs) below shows an HTTP GET request.
5364

5465
```rust
5566
async fn fetch_url_return_str (url: hyper::Uri) -> Result<()> {
@@ -64,7 +75,23 @@ async fn fetch_url_return_str (url: hyper::Uri) -> Result<()> {
6475
println!("{}", String::from_utf8_lossy(&resp_data));
6576
```
6677

67-
And here is an HTTP or HTTPS POST request.
78+
The [HTTPS example](https://github.com/WasmEdge/wasmedge_hyper_demo/blob/main/client-https/src/main.rs) is slightly more complex.
79+
80+
```rust
81+
async fn fetch_https_url(url: hyper::Uri) -> Result<()> {
82+
let https = wasmedge_hyper_rustls::connector::new_https_connector(
83+
wasmedge_rustls_api::ClientConfig::default(),
84+
);
85+
let client = Client::builder().build::<_, hyper::Body>(https);
86+
let res = client.get(url).await?;
87+
88+
let body = hyper::body::to_bytes(res.into_body()).await.unwrap();
89+
println!("{}", String::from_utf8(body.into()).unwrap());
90+
Ok(())
91+
}
92+
```
93+
94+
And here is an HTTP POST request.
6895

6996
```rust
7097
async fn post_url_return_str (url: hyper::Uri, post_body: &'static [u8]) -> Result<()> {
@@ -86,9 +113,9 @@ async fn post_url_return_str (url: hyper::Uri, post_body: &'static [u8]) -> Resu
86113
}
87114
```
88115

89-
## Synchronous client with http_req
116+
## The http_req API
90117

91-
If your WasmEdge application only needs to make sequential requests to external web services, a synchronous client is easier to work with. It allows you to make a request, wait for the response, and move on once the response is fully received. Use the http_req API to make simple synchronous HTTP requests. Build and run [the example](https://github.com/second-state/http_req/) in WasmEdge.
118+
If your WasmEdge application only needs to make sequential requests to external web services, a synchronous client is easier to work with. It allows you to make a request, wait for the response, and move on once the response is fully received. Use the `http_req` API to make simple synchronous HTTP requests. Build and run [the example](https://github.com/second-state/http_req/) in WasmEdge.
92119

93120
```bash
94121
git clone https://github.com/second-state/http_req
@@ -97,7 +124,7 @@ cd http_req/
97124
# Build the Rust Code
98125
cargo build --target wasm32-wasi --release
99126
# Use the AoT compiler to get better performance
100-
wasmedgec target/wasm32-wasi/release/get.wasm get.wasm
127+
wasmedge compile target/wasm32-wasi/release/get.wasm get.wasm
101128

102129
# Run the example
103130
wasmedge get.wasm
@@ -106,14 +133,14 @@ wasmedge get_https.wasm
106133
... ...
107134
```
108135

109-
In your Rust application, import the [http_req_wasi](https://crates.io/crates/http_req_wasi) crate, which is compatible with [http_req](https://github.com/jayjamesjay/http_req) at the API level. Just add the following line to your Cargo.toml.
136+
In your Rust application, import the [http_req_wasi](https://crates.io/crates/http_req_wasi) crate, which is compatible with [http_req](https://github.com/jayjamesjay/http_req) at the API level. Just add the following line to your `Cargo.toml`.
110137

111138
```toml
112139
[dependencies]
113140
http_req_wasi = "0.10"
114141
```
115142

116-
The example below shows an [HTTP GET request](https://github.com/second-state/http_req/blob/master/examples/get.rs).
143+
The example below shows an [HTTP GET request](https://github.com/second-state/http_req/blob/master/examples/get.rs). For HTTPS requests, you can [simply change](https://github.com/second-state/http_req/blob/master/examples/get_https.rs) the `http` URL to `https`.
117144

118145
```rust
119146
use http_req::request;
@@ -128,7 +155,7 @@ fn main() {
128155
}
129156
```
130157

131-
And here is an [HTTP POST request](https://github.com/second-state/http_req/blob/master/examples/post.rs).
158+
And here is an [HTTP POST request](https://github.com/second-state/http_req/blob/master/examples/post.rs). For HTTPS requests, you can [simply change](https://github.com/second-state/http_req/blob/master/examples/post_https.rs) the `http` URL to `https`.
132159

133160
```rust
134161
use http_req::request;
@@ -144,14 +171,9 @@ fn main() {
144171
}
145172
```
146173

147-
<!-- prettier-ignore -->
148-
:::note
149-
In order to make HTTPS requests, you need to [install the wasmedge_TLS plug-in](../../../start/install.md#tls-plug-in). You can then run the HTTPS [GET](https://github.com/second-state/http_req/blob/master/examples/get_https.rs) and [POST](https://github.com/second-state/http_req/blob/master/examples/post_https.rs) examples in the example repo.
150-
:::
151-
152-
## reqwest client
174+
## The reqwest API
153175

154-
The reqwest crate is another popular Rust library to create asynchronous HTTP clients. It is built on top of the hyper API. Many developers find it easier to use. Build and run [the example](https://github.com/WasmEdge/wasmedge_reqwest_demo/) in WasmEdge as follows.
176+
The `reqwest` crate is another popular Rust library to create asynchronous HTTP clients. It is built on top of the `hyper` API. Many developers find it easier to use. But perhaps more importantly, many existing Rust applications use `reqwest`, and you can make them work in WasmEdge by simply replacing the `reqwest` crate with `reqwest_wasi`! Build and run [the example](https://github.com/WasmEdge/wasmedge_reqwest_demo/) in WasmEdge as follows.
155177

156178
<!-- prettier-ignore -->
157179
:::note
@@ -165,13 +187,13 @@ cd wasmedge_reqwest_demo
165187
# Build the Rust code
166188
cargo build --target wasm32-wasi --release
167189
# Use the AoT compiler to get better performance
168-
wasmedgec target/wasm32-wasi/release/wasmedge_reqwest_demo.wasm wasmedge_reqwest_demo.wasm
190+
wasmedge compile target/wasm32-wasi/release/wasmedge_reqwest_demo.wasm wasmedge_reqwest_demo.wasm
169191

170192
# Run the example
171193
wasmedge wasmedge_reqwest_demo.wasm
172194
```
173195

174-
In your Rust application, import [the WasmEdge adapted hyper crate](https://crates.io/crates/hyper_wasi), which uses a special version of single-threaded Tokio that is adapted for WebAssembly. Just add the following line to your Cargo.toml.
196+
In your Rust application, import [the WasmEdge adapted reqwest crate](https://crates.io/crates/reqwest_wasi), which uses a special version of single-threaded Tokio that is adapted for WebAssembly. Just add the following lines to your `Cargo.toml`.
175197

176198
```toml
177199
[dependencies]

docs/develop/rust/http_service/server.md

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,10 @@ sidebar_position: 2
44

55
# Server
66

7-
For WasmEdge to become a cloud-native runtime for microservices, it needs to support HTTP servers. By its very nature, the HTTP server is always asynchronous. In this chapter, we will cover simple HTTP servers based on [the wrap API](#the-warp-api), as well as the [low-level hyper API](#the-hyper-api). For HTTP clients in WasmEdge, please see [the previous chapter](client.md).
7+
For WasmEdge to become a cloud-native runtime for microservices, it needs to support HTTP servers. By its very nature, the HTTP server is always asynchronous (non-blocking -- so that it can handle concurrent requests). This chapter will cover HTTP servers using popular Rust APIs.
8+
9+
- [The warp API](#the-warp-api)
10+
- [The hyper API](#the-hyper-api)
811

912
<!-- prettier-ignore -->
1013
:::note
@@ -22,7 +25,7 @@ cd wasmedge_hyper_demo/server-warp
2225
# Build the Rust code
2326
cargo build --target wasm32-wasi --release
2427
# Use the AoT compiler for better performance
25-
wasmedgec target/wasm32-wasi/release/wasmedge_warp_server.wasm wasmedge_warp_server.wasm
28+
wasmedge compile target/wasm32-wasi/release/wasmedge_warp_server.wasm wasmedge_warp_server.wasm
2629

2730
# Run the example
2831
wasmedge wasmedge_warp_server.wasm
@@ -35,7 +38,7 @@ $ curl http://localhost:8080/echo -X POST -d "WasmEdge"
3538
WasmEdge
3639
```
3740

38-
In your Rust application, import the WasmEdge-adapted warp crate, which uses a special version of single-threaded Tokio adapted for WebAssembly. Just add the following lines to your Cargo.toml.
41+
In your Rust application, import the WasmEdge-adapted `warp_wasi` crate, which uses a special version of single-threaded Tokio adapted for WebAssembly. Just add the following lines to your `Cargo.toml`.
3942

4043
```toml
4144
[dependencies]
@@ -68,7 +71,7 @@ async fn main() {
6871

6972
## The hyper API
7073

71-
The warp crate is convenient to use. But oftentimes, developers need access to lower level APIs. The hyper crate is an excellent HTTP library for that. Build and run [the example](https://github.com/WasmEdge/wasmedge_hyper_demo/blob/main/server/) in WasmEdge as follows.
74+
The `warp` crate is convenient to use. But oftentimes, developers need access to lower-level APIs. The `hyper` crate is an excellent HTTP library for that. Build and run [the example](https://github.com/WasmEdge/wasmedge_hyper_demo/blob/main/server/) in WasmEdge as follows.
7275

7376
```bash
7477
git clone https://github.com/WasmEdge/wasmedge_hyper_demo
@@ -77,7 +80,7 @@ cd wasmedge_hyper_demo/server
7780
# Build the Rust code
7881
cargo build --target wasm32-wasi --release
7982
# Use the AoT compiler to get better performance
80-
wasmedgec target/wasm32-wasi/release/wasmedge_hyper_server.wasm wasmedge_hyper_server.wasm
83+
wasmedge compile target/wasm32-wasi/release/wasmedge_hyper_server.wasm wasmedge_hyper_server.wasm
8184

8285
# Run the example
8386
wasmedge wasmedge_hyper_server.wasm
@@ -90,7 +93,7 @@ $ curl http://localhost:8080/echo -X POST -d "WasmEdge"
9093
WasmEdge
9194
```
9295

93-
In your Rust application, import the WasmEdge adapted hyper crate, which uses a special version of single threaded Tokio that is adapted for WebAssembly. Just add the following line to your Cargo.toml.
96+
In your Rust application, import the WasmEdge adapted `hyper_wasi` crate, which uses a special version of single threaded Tokio that is adapted for WebAssembly. Just add the following lines to your `Cargo.toml`.
9497

9598
```toml
9699
[dependencies]

docs/develop/rust/socket_networking/client.md

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,14 @@
22
sidebar_position: 1
33
---
44

5-
# Client
5+
# Socket client
66

7-
The [wasmedge_wasi_socket](https://github.com/second-state/wasmedge_wasi_socket) crate enables Rust developers to create networking applications and compile them into WebAssembly for WasmEdge Runtime. One of the key features of WasmEdge is that it supports non-blocking sockets. That allows even a single-threaded WASM application to handle concurrent network requests. For example, while the program is waiting for data to stream in from one connection, it can start or handle another connection.
7+
It is possible for WasmEdge applications to open TCP/IP or UDP network sockets in the host system to communicate directly with external applications. One of the key features of WasmEdge is that it supports non-blocking sockets. That allows even a single-threaded WASM application to handle concurrent network requests. For example, while the program is waiting for data to stream in from one connection, it can start or handle another connection. The [wasmedge_wasi_socket](https://github.com/second-state/wasmedge_wasi_socket) crate enables Rust developers to work on the network socket level.
88

9-
While there are many possibilities with sockets, we will demonstrate two familiar use cases, [a simple HTTP client](#a-simple-http-client) and [a non-blocking HTTP client application](#a-non-blocking-http-client-example), in this chapter.
9+
In this chapter, we will show you how to build HTTP clients on TCP sockets. The reason is that the HTTP protocol is relatively simple and could be demonstrated easily. If you use HTTP clients in production, we recommend checking out the [HTTP client](../http_service/client.md) chapter in this book.
10+
11+
- [A simple HTTP client based on TCP sockets](#a-simple-http-client)
12+
- [A non-blocking HTTP client based on TCP sockets](#a-non-blocking-http-client-example)
1013

1114
<!-- prettier-ignore -->
1215
:::note
@@ -24,7 +27,7 @@ cd wasmedge_wasi_socket/http_client/
2427
# Build the Rust Code
2528
cargo build --target wasm32-wasi --release
2629
# Use the AoT compiler to get better performance
27-
wasmedgec target/wasm32-wasi/release/http_client.wasm http_client.wasm
30+
wasmedge compile target/wasm32-wasi/release/http_client.wasm http_client.wasm
2831

2932
# Run the example
3033
wasmedge http_client.wasm
@@ -68,7 +71,7 @@ cd wasmedge_wasi_socket/nonblock_http_client/
6871
# Build the Rust Code
6972
cargo build --target wasm32-wasi --release
7073
# Use the AoT compiler for better performance
71-
wasmedgec target/wasm32-wasi/release/nonblock_http_client.wasm nonblock_http_client.wasm
74+
wasmedge compile target/wasm32-wasi/release/nonblock_http_client.wasm nonblock_http_client.wasm
7275

7376
# Run the example
7477
wasmedge nonblock_http_client.wasm

docs/develop/rust/socket_networking/server.md

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,12 @@
22
sidebar_position: 2
33
---
44

5-
# Server
5+
# Socket server
66

7-
As we described in the [client](client.md) chapter, with the WasmEdge socket API, it is also possible for Rust developers to work directly on the socket level. For WasmEdge to become a cloud-native runtime for microservices, it needs to support HTTP servers. In this chapter, we will discuss[an HTTP server example](#an-http-server-example) and [a non-blocking HTTP server example](#a-non-blocking-http-server-example).
7+
As we described in the [client](client.md) chapter, with the WasmEdge socket API, it is possible for Rust developers to work directly on the TCP and UDP socket level. In this chapter, we will show how to create HTTP servers with the TCP socket API. We chose HTTP here for demonstration purposes due to the simplicity of the HTTP protocol. If you need a production-ready HTTP server, check out the [HTTP server](../http_service/server.md) chapter.
8+
9+
- [An HTTP server example](#an-http-server-example)
10+
- [A non-blocking HTTP server example](#a-non-blocking-http-server-example)
811

912
<!-- prettier-ignore -->
1013
:::note
@@ -22,7 +25,7 @@ cd wasmedge_wasi_socket/http_server
2225
# Build the Rust code
2326
cargo build --target wasm32-wasi --release
2427
# Use the AoT compiler for better performance
25-
wasmedgec target/wasm32-wasi/release/http_server.wasm http_server.wasm
28+
wasmedge compile target/wasm32-wasi/release/http_server.wasm http_server.wasm
2629

2730
# Run the example
2831
$wasmedge http_server.wasm
@@ -116,7 +119,7 @@ cd wasmedge_wasi_socket
116119
# Build the Rust code
117120
cargo build --target wasm32-wasi --release
118121
# Use the AoT compiler for better performance
119-
wasmedgec target/wasm32-wasi/release/poll_tcp_listener.wasm poll_tcp_listener.wasm
122+
wasmedge compile target/wasm32-wasi/release/poll_tcp_listener.wasm poll_tcp_listener.wasm
120123

121124
# Run the example
122125
wasmedge poll_tcp_listener.wasm

0 commit comments

Comments
 (0)