eclipse / paho.mqtt.rust

paho.mqtt.rust
Other
516 stars 102 forks source link

WebAssembly (WASM) support #157

Closed zavalyshyn closed 1 year ago

zavalyshyn commented 2 years ago

Hi,

First of all, thanks for making this library available for Rust developers. I can successfully compile and run it on a standard Linux machine.

I wonder if there are plans for WebAssembly support, or more specifically, wasm32-unknown-unknown build target.

Right now my attempts to compile the code using paho.mqtt.rust fail because of the openssl library that fails to cross-compile for the wasm32 target.

Any ideas on what I can try to make it work?

Thanks

Just in case I show my Cargo.toml file

[package]
name = "mqtt-example"
version = "0.1.0"
license = "MIT/Apache-2.0"
edition = "2018"

[dependencies]
paho-mqtt = { git = "https://github.com/eclipse/paho.mqtt.rust.git", branch = "master", features = ["vendored-ssl"]}
wasm-bindgen = "0.2"
# openssl = { version = '0.10', features = ["vendored"], optional = true }

[[bin]]
name = "sub"
path = "src/sub/subscribe_mqtt_rust.rs"

[[bin]]
name = "pub"
path = "src/pub/public_mqtt_rust.rs"
fpagliughi commented 2 years ago

Hey @zavalyshyn. I haven't done much with WebAssembly other than downloading an e-book and reading the 1st chapter! It does seem a worth goal to support it, but I suppose, at the moment, we're locked into what we can do with the dependencies (Paho C and OpenSSL).

Out of curiosity, have you tried it without SSL? Something like:

[dependencies]
paho-mqtt = { version = "0.11", default-features = false, features = ["bundled"] }

If that worked, you would only be able to connect with TCP or unsecure WebSockets, but it might be a starting point. If it doesn't work, it would indicate a lot more trouble moving forward.

zavalyshyn commented 2 years ago

@fpagliughi thanks for a suggestion. I've just tried that and the build still failed but this time not not due to underlying openssl dependencies but due to paho.mqtt.c code using pthread.h header files for logging. Unfortunately, threads are not yet supported by wasi-sdk, which I'm using to build for wasm32-wasi target. Is there a flag I could pass to build with no threads support?

fpagliughi commented 2 years ago

Sadly, no. The C lib spawns a few threads to do all of the I/O for the library.

fpagliughi commented 1 year ago

Sorry. Looks like there's nothing we can do here at this point.