Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Networking related docs update #150

Merged
merged 16 commits into from
Aug 21, 2023
2 changes: 1 addition & 1 deletion docs/develop/javascript/hello_world.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ Then download the pre-built WasmEdge QuickJS Runtime program, and optionally, AO

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

<!-- prettier-ignore -->
Expand Down
13 changes: 7 additions & 6 deletions docs/develop/javascript/networking.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,16 @@ The QuickJS WasmEdge Runtime supports Node.js's `http` and `fetch` APIs via the

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.

- [Networking](#networking)
- [Prerequisites](#prerequisites)
- [Fetch client](#fetch-client)
- [HTTP server](#http-server)
- [TCP server and client](#tcp-server-and-client)
- [Prerequisites](#prerequisites)
- [Fetch client](#fetch-client)
- [HTTP server](#http-server)
- [TCP server and client](#tcp-server-and-client)

## Prerequisites

[See here](./hello_world#prerequisites)
[Install WasmEdge](../../start/install.md). To make HTTPS requests, install the [WasmEdge TLS plug-in](../../start/install.md#tls-plug-in).

[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.

## Fetch client

Expand Down
76 changes: 49 additions & 27 deletions docs/develop/rust/http_service/client.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
sidebar_position: 1
---

# Client
# HTTP client

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).

Expand All @@ -11,16 +11,15 @@ WasmEdge allows Rust developers to use APIs they are already familiar with to ac
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).
:::

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.
We will discuss HTTP and HTTPS clients using popular Rust APIs.

- [Client](#client)
- [Asynchronous client with hyper](#asynchronous-client-with-hyper)
- [Synchronous client with http_req](#synchronous-client-with-http_req)
- [reqwest client](#reqwest-client)
- [The hyper API (recommended)](#the-hyper-api)
- [The http_req API](#the-http_req-api)
- [The reqwest API](#the-reqwest-api)

## Asynchronous client with hyper
## The hyper API

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.
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.

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

# Run the example
wasmedge wasmedge_hyper_client.wasm
```

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.
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.

```bash
// Build
cd wasmedge_hyper_demo/client-https
cargo build --target wasm32-wasi --release
wasmedge compile target/wasm32-wasi/release/wasmedge_hyper_client_https.wasm wasmedge_hyper_client_https.wasm

// Run
wasmedge wasmedge_hyper_client_https.wasm
```

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`.

```toml
[dependencies]
hyper_wasi = "0.15.0"
```

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.
The [Rust example code](https://github.com/WasmEdge/wasmedge_hyper_demo/blob/main/client/src/main.rs) below shows an HTTP GET request.

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

And here is an HTTP or HTTPS POST request.
The [HTTPS example](https://github.com/WasmEdge/wasmedge_hyper_demo/blob/main/client-https/src/main.rs) is slightly more complex.

```rust
async fn fetch_https_url(url: hyper::Uri) -> Result<()> {
let https = wasmedge_hyper_rustls::connector::new_https_connector(
wasmedge_rustls_api::ClientConfig::default(),
);
let client = Client::builder().build::<_, hyper::Body>(https);
let res = client.get(url).await?;

let body = hyper::body::to_bytes(res.into_body()).await.unwrap();
println!("{}", String::from_utf8(body.into()).unwrap());
Ok(())
}
```

And here is an HTTP POST request.

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

## Synchronous client with http_req
## The http_req API

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.
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.

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

# Run the example
wasmedge get.wasm
Expand All @@ -106,14 +133,14 @@ wasmedge get_https.wasm
... ...
```

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.
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`.

```toml
[dependencies]
http_req_wasi = "0.10"
```

The example below shows an [HTTP GET request](https://github.com/second-state/http_req/blob/master/examples/get.rs).
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`.

```rust
use http_req::request;
Expand All @@ -128,7 +155,7 @@ fn main() {
}
```

And here is an [HTTP POST request](https://github.com/second-state/http_req/blob/master/examples/post.rs).
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`.

```rust
use http_req::request;
Expand All @@ -144,14 +171,9 @@ fn main() {
}
```

<!-- prettier-ignore -->
:::note
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.
:::

## reqwest client
## The reqwest API

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.
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.

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

# Run the example
wasmedge wasmedge_reqwest_demo.wasm
```

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.
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`.

```toml
[dependencies]
Expand Down
15 changes: 9 additions & 6 deletions docs/develop/rust/http_service/server.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,10 @@ sidebar_position: 2

# Server

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).
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.

- [The warp API](#the-warp-api)
- [The hyper API](#the-hyper-api)

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

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

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.
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`.

```toml
[dependencies]
Expand Down Expand Up @@ -68,7 +71,7 @@ async fn main() {

## The hyper API

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.
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.

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

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

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.
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`.

```toml
[dependencies]
Expand Down
13 changes: 8 additions & 5 deletions docs/develop/rust/socket_networking/client.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,14 @@
sidebar_position: 1
---

# Client
# Socket client

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.
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.

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.
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.

- [A simple HTTP client based on TCP sockets](#a-simple-http-client)
- [A non-blocking HTTP client based on TCP sockets](#a-non-blocking-http-client-example)

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

# Run the example
wasmedge http_client.wasm
Expand Down Expand Up @@ -68,7 +71,7 @@ cd wasmedge_wasi_socket/nonblock_http_client/
# Build the Rust Code
cargo build --target wasm32-wasi --release
# Use the AoT compiler for better performance
wasmedgec target/wasm32-wasi/release/nonblock_http_client.wasm nonblock_http_client.wasm
wasmedge compile target/wasm32-wasi/release/nonblock_http_client.wasm nonblock_http_client.wasm

# Run the example
wasmedge nonblock_http_client.wasm
Expand Down
11 changes: 7 additions & 4 deletions docs/develop/rust/socket_networking/server.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,12 @@
sidebar_position: 2
---

# Server
# Socket server

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).
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.

- [An HTTP server example](#an-http-server-example)
- [A non-blocking HTTP server example](#a-non-blocking-http-server-example)

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

# Run the example
$wasmedge http_server.wasm
Expand Down Expand Up @@ -116,7 +119,7 @@ cd wasmedge_wasi_socket
# Build the Rust code
cargo build --target wasm32-wasi --release
# Use the AoT compiler for better performance
wasmedgec target/wasm32-wasi/release/poll_tcp_listener.wasm poll_tcp_listener.wasm
wasmedge compile target/wasm32-wasi/release/poll_tcp_listener.wasm poll_tcp_listener.wasm

# Run the example
wasmedge poll_tcp_listener.wasm
Expand Down
Loading