Skip to content

Commit

Permalink
keyexpr API with wasm restored (#103)
Browse files Browse the repository at this point in the history
* restored keyexpr wasm, example reworked to webpack to support it

* execute examples from root scripts

* no dist in deno

* readme updated

* examples start corrected

* use keyexpr api in chat example

* obsolete readmes removed, serve example index.html from root

* fix grammar in README.md

* "Unordered list indentation" check fix

* Update README.md

Co-authored-by: Charles Schleich <[email protected]>

* build wasm-pack if necessary

---------

Co-authored-by: Charles Schleich <[email protected]>
  • Loading branch information
milyin and Charles-Schleich authored Jan 23, 2025
1 parent b3772f9 commit 25f742e
Show file tree
Hide file tree
Showing 24 changed files with 910 additions and 1,301 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -52,4 +52,4 @@ uuid = { version = "1.3.0", default-features = false, features = [
uhlc = { version = "0.8.0", default-features = false } # Default features are disabled due to usage in no_std crates

[workspace.metadata.bin]
zenohd = { git = "https://github.com/eclipse-zenoh/zenoh.git", branch = "main", version = "1.0.0-dev" }
zenohd = { git = "https://github.com/eclipse-zenoh/zenoh.git", branch = "main", version = "1.1.1" }
96 changes: 58 additions & 38 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,16 +19,24 @@ Check the website [zenoh.io](http://zenoh.io) and the [roadmap](https://github.c

## Typescript/Javascript API

This repository provides a Typescript / Javascript binding through the use of the `remote-api-plugin` in this repo.
This repository provides a Typescript / Javascript binding through the use of the `zenoh-plugin-remote-api` in this repo.
The long-term plan is to use zenoh [Zenoh written in Rust](https://github.com/eclipse-zenoh/zenoh) to target WASM.
In its current state, it is not possible to compile Zenoh (Rust) to target WASM, and it will need to undergo a fair
amount of refactoring before that can happen.

The latest version of the zenoh-ts library can be installed from npm:

```sh
npm install @eclipse-zenoh/zenoh-ts
```

Docs can be accessed at [Docs Link](https://eclipse-zenoh.github.io/zenoh-ts/)

The library requires a websocket connection to the `zenohd` daemon through the `zenohd-plugin-remote-api` in the daemon. See the corresponding section below.

---

## How to build and run it
## How to build and use zenoh-ts

> :warning: **WARNING** :warning: : Zenoh and its ecosystem are under active development. When you build from git, make sure you also
build from git any other Zenoh repository you plan to use (e.g. binding, plugin, backend, etc.). It may happen that some changes in git
Expand All @@ -41,9 +49,9 @@ The `zenohd` router and its plugins should be built with the same Zenoh sources,
same set of features. This requirement exists because the router and plugins share common Rust structures, and Rust doesn't guarantee
ABI compatibility of the memory representation of these structures.

Therefore one of the methods below is recommended to ensure that plugin and router are compatible.
Therefore, one of the methods below is recommended to ensure that the plugin and router are compatible.

The file `EXAMPLE_CONFIG.json5` references the `zenoh-plugin-remote-api\EXAMPLE_CONFIG.json5` with minimal necessary set of options to run the plugin. See also full set of available options, like ssl certificate settings in `zenoh-plugin-remote-api\config.json5`.
The file `EXAMPLE_CONFIG.json5` references the `zenoh-plugin-remote-api\EXAMPLE_CONFIG.json5` with the minimal necessary set of options to run the plugin. See also the full set of available options, like SSL certificate settings in `zenoh-plugin-remote-api\config.json5`.

1. Install the latest release of `zenohd` and `zenoh-plugin-remote-api`

Expand All @@ -64,13 +72,13 @@ The file `EXAMPLE_CONFIG.json5` references the `zenoh-plugin-remote-api\EXAMPLE_
brew install zenoh-plugin-remote-api
```

Run the installed zenoh router with example config
Run the installed zenoh router with the example config

```sh
zenohd --config EXAMPLE_CONFIG.json5
```

Expected output should be something similar to:
The expected output should be something similar to:

```txt
zenohd: zenohd v1.0.3 built with rustc 1.75.0 (82e1608df 2023-12-21)
Expand Down Expand Up @@ -99,7 +107,7 @@ The file `EXAMPLE_CONFIG.json5` references the `zenoh-plugin-remote-api\EXAMPLE_
cargo bin zenohd --config EXAMPLE_CONFIG.json5
```

Expected output should be something similar to:
The expected output should be something similar to:

```txt
zenohd: zenohd vc764bf9b built with rustc 1.75.0 (82e1608df 2023-12-21)
Expand All @@ -111,61 +119,73 @@ The file `EXAMPLE_CONFIG.json5` references the `zenoh-plugin-remote-api\EXAMPLE_
zenoh::net::runtime::orchestrator: Zenoh can be reached at: tcp/...
```

### Building the library
### Building the library from sources

1. Make sure that the following utilities are available on your platform.

- [NPM](https://www.npmjs.com/package/npm)
- [yarn](https://classic.yarnpkg.com/lang/en/docs/install/#debian-stable)
- [Typescript](https://www.typescriptlang.org/download/)
- [Rust](https://www.rust-lang.org)
- [deno](https://deno.com/) - for command line examples

2. Navigate to the directory `zenoh-ts`

3. Run the commands:

```sh
yarn install
yarn run build
yarn build
```

### Build and run the command line examples
The result are placed into the `zenoh-ts/dist` directory.

This library is currently compatible with browsers, but not with NodeJS due to websocket library limitations.
To run the command line examples use the javascript runtime [deno](https://deno.com/) which is expected to be consistent with the browser.
This library is currently compatible with browsers, but not with NodeJS due to websocket library limitations.

1. Install [deno](https://deno.com/)
2. Navigate to the `zenoh-ts/examples/deno` directory
3. Install the `zenoh-ts` library by running `yarn install`
4. Run zenohd with the remote_api plugin, configured to websocket port 10000, as described above
5. Run the examples by running `yarn example <PATH TO EXAMPLE>`, i.e. `yarn example src/z_sub.ts`
### Build and run examples

To run publisher and subscriber examples:
For simplicity, the examples can be executed from the `zenoh-ts` directory. You may also go directly to the `zenoh-ts/examples`
directory and explore and run examples there.

```sh
yarn example src/z_pub.rs
```
Make sure that the `zenohd` router with `zenoh-plugin-remote-api` works on localhost and the websocket port is 10000.

```sh
yarn example src/z_sub.rs
```
To run an example, execute the command `yarn start example_dir [example_name]`

The subscriber should start to receive messages from publisher:
The following examples are available:

```text
>> [Subscriber] Received PUT ('demo/example/zenoh-ts-pub': '[0] 122,101,110,111,104')
>> [Subscriber] Received PUT ('demo/example/zenoh-ts-pub': '[1] 122,101,110,111,104')
>> [Subscriber] Received PUT ('demo/example/zenoh-ts-pub': '[2] 122,101,110,111,104')
```
- Command line examples
- Publisher and subscriber

This will start an instance of Deno running the example.
The application will attempt to connect to a `websocket_port` : `10000` where the Remote API plugin is expected to be running.
```sh
yarn start deno z_pub
yarn start deno z_sub
```

### Adding Typescript to your application
- Queryable and get

The latest version of zenoh-ts library can be installed from npm:
```sh
yarn start deno z_queryable
yarn start deno z_get
```

```sh
npm install @eclipse-zenoh/zenoh-ts
```
and many more

- Chat in browser example

```sh
yarn start chat
```

The browser window on [localhost:8080](http://127.0.0.1:8080/index.html) with the chat interface should open. Open another one with the same address, press the "Connect" buttons in both and see how they interact.

### Generating Documentation

See also example application in the [zenoh-ts/examples/chat](zenoh-ts/examples/chat/README.md)
1. Make sure that the [typedoc](https://typedoc.org/) dependency is installed.

2. Navigate to the directory `zenoh-ts`

3. Run the commands:

```bash
npx typedoc src/index.ts
```
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
{
"license": "EPL-2.0",
"dependencies": {
"copy-webpack-plugin": "^12.0.2"
}
Expand Down
7 changes: 7 additions & 0 deletions zenoh-keyexpr-wasm/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ version = "0.1.0"
authors = ["Charles Schleich"]
edition = "2018"
publish = false
description = "A wrapper for zenoh-keyexpr for use with WebAssembly."
repository = "https://github.com/eclipse-zenoh/zenoh-ts"
license = "EPL-2.0"

[lib]
crate-type = ["cdylib", "rlib"]
Expand All @@ -23,3 +26,7 @@ wasm-bindgen-test = "0.3.34"
[profile.release]
# Tell `rustc` to optimize for small code size.
opt-level = "s"

# Disable warninng `unexpected `cfg` condition name: `wasm_bindgen_unstable_test_coverage``
[lints.rust]
unexpected_cfgs = { level = "warn", check-cfg = ['cfg(wasm_bindgen_unstable_test_coverage)'] }
2 changes: 2 additions & 0 deletions zenoh-keyexpr-wasm/rust-toolchain.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
[toolchain]
channel = "1.84.0"
99 changes: 0 additions & 99 deletions zenoh-ts/README.md

This file was deleted.

27 changes: 0 additions & 27 deletions zenoh-ts/examples/chat/README.md

This file was deleted.

2 changes: 1 addition & 1 deletion zenoh-ts/examples/chat/assets/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
<head>
<meta charset="UTF-8">
<title>Zenoh TS Example</title>
<script type="module" src="main.js"></script>
<link rel="stylesheet" href="index.css">
</head>

Expand Down Expand Up @@ -53,6 +52,7 @@
<button id="toggle-log-button">Toggle Log</button>
</div>
</div>
<script type="module" src="bundle.js"></script>
</body>

</html>
11 changes: 7 additions & 4 deletions zenoh-ts/examples/chat/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,16 @@
"type": "module",
"scripts": {
"clean": "rm -rf ./node_modules ./dist ./esm",
"build": "rm -rf dist && mkdir dist && tsc && cp assets/* dist/ && jspm link -m dist/index.html",
"start": "http-server ../.. -c-1 -o examples/chat/dist/index.html"
"build": "rm -rf dist && mkdir dist && webpack --config webpack.config.js && cp assets/* dist/",
"_build": "sh -c '[ ! -d \"node_modules\" ] || [ ! -d \"dist\" ] && yarn clean && yarn install && yarn build || true'",
"start": "sh -c 'yarn _build && http-server dist -c-1 -o index.html'"
},
"devDependencies": {
"http-server": "^14.1.1",
"jspm": "^3.3.3",
"typescript": "^5.7.2"
"typescript": "^5.7.3",
"webpack": "^5.64.4",
"webpack-cli": "^4.9.1",
"ts-loader": "^9.2.6"
},
"dependencies": {
"@eclipse-zenoh/zenoh-ts": "file:../.."
Expand Down
Loading

0 comments on commit 25f742e

Please sign in to comment.