bytebeamio / rumqtt

The MQTT ecosystem in rust
Apache License 2.0
1.53k stars 235 forks source link

Get incoming events only from async client #330

Open ZodiacWind opened 2 years ago

ZodiacWind commented 2 years ago

I used the AsyncClient of rumqttc to subscribe specific topics. And I only want to handle the requests_rx only because the subscribed topics are concerned only.

I know I cloud use match just like:

    loop {
        match eventloop.poll().await {
            Ok(Event::Incoming(Packet::Publish(p))) => {
                    println!("Incoming = {:?}, {:?}", p.topic, p.payload);
            },
            Ok(Event::Incoming(Packet::PingResp)) |
            Ok(Event::Outgoing(Outgoing::PingReq)) => {},
            Ok(Event::Incoming(i)) => {
                println!("Incoming = {:?}", i);
            },
            Ok(Event::Outgoing(o)) => {
                println!("Outgoing = {:?}", o);
            },
            Err(e) => {
                println!("Error = {:?}", e);
                // XXX: Here I have to re-subscribe to the topics :(
                client.subscribe("#", QoS::AtMostOnce).await.unwrap();
            }
        }

(from #250) to deal with Incoming events only.

But I wonder that is there any solution or method that I can get the incoming events ONLY directly.

I really appreciate it.

de-sh commented 2 years ago

@ZodiacWind we are working on implementing an abstraction that would work something like the following: Subscribing:

let subscriber: Subscriber = client.subscriber("topic/string", QoS);
while let Some(pub) = subscriber.next().await { ... }

Publishing:

let publisher: Publisher = client.publisher("topic/string", QoS);
publisher.publish(data, retained).await;

What are your thoughts on this?

ZodiacWind commented 2 years ago

Good job 👍 . It seems that the Subscriber is what I want. Thanks for your work. Please let me know if there is anything I can do. 😄

de-sh commented 2 years ago

I have written some code as a PoC: https://github.com/bytebeamio/rumqtt/commit/150cec51553ef73c6e5128a1ba39df3141bf7792#diff-fd67087e30ee8bbba41e6c314cab2aea694c64ea2cd37b27cff20fb2f2f1a650

CC: @tekjar