ntex-rs / ntex-mqtt

MQTT Client/Server framework for v5 and v3.1.1 protocols

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

tokio::main support

Antiarchitect opened this issue · comments

Is there any chance I can use v3::MqttServer from within tokio::spawn?

Like this:

...
#[tokio::main]
async fn main() {
    ...

    let mqtt_server = tokio::spawn(async move {
        v3::MqttServer::new(handshake_v3).publish(publish_v3).run().await
    });
    let _join = tokio::try_join!(mqtt_server).expect("Cannot join spawned futures!");
}

I am not planning to support tokio directly. support potentially could be added to ntex-rt, similar to actix-rt. but do not expect direct tokio support in ntex-xxx crates, I am planing to make tokio completely optional.

I mean I'm now trying to implement mqtt-to-amqp proxy in order to avoid using RabbitMQ MQTT plugin as we having troubles with it. What I need is MQTT Server that receives messages and AMQP 0.9 client (possibly lapin crate) that will send them to RabbitMQ via AMQP with some routing magic. Cannot use this AMQP 1.0 crate as RabbitMQ does not support 1.0 out of the box. Trying to find PoC variant to spawn this MQTT server and lapin client in parallel and interchange data between them via some thread-protected buffer. Any thoughts on how to do that?

if lapin supports tokio then you can just start it within #[ntex::main]

It does with some additional crate. But would tokio::spawn or ntex::spawn work this way:

#[ntex::main]
async fn main() {
    ...

    let mqtt_server = tokio::spawn(async move {
        v3::MqttServer::new(handshake_v3).publish(publish_v3).run().await
    });
    let amqp_client = tokio::swpawn(async move {
       Somelapinmagic
    });
    let _join = tokio::try_join!(mqtt_server, amqp_client).expect("Cannot join spawned futures!");
}

should work, ntex::rt::spawn should work as well

ntex starts multiple single threaded run-times

Thank you so much :) Will experiment on this!