Purpouse

The goal is use Mqtt with actors.

While using ports and adpates ( a.k.a. hexagonal architecture) we may want to use actix actors to implement the core. Specially if multiple protocols are to be used.

If one of those protocols is MQTT then this is the adapter you need.

Description

This crate is asyncrhonous and should be run within actix_rs executor.

Tough the name suggest this crate is about implementing the adapter as an actor, actually is not pure actor.

It has an actor to let you have an address to allow the core sending messages to topics: MqttPublisher, but the main job is carried by the struct: MqttConnectionManager, including connecting to the server and creating the former actor.

architecture

Usage

Create an instance of MqttConnectionManager. Use the from method and provide all the connection parameters.

Then add the subscriptions you may need before starting the MqttConnectionManager`.

Do all steps inside a actix_rs executor.

For example (more and better examples in the documentation)

```rust

[actix_rt::main]

async fn main() { let myactoraddress = MyActor::new().start();

MqttConnectionManager::from(
    String::from(CLIENT_NAME),
    String::from(IP),
    1883,
    Some(String::from(USER)),
    Some(String::from(PASS)),
    })
    .add_subscription( Box::new(DumpHandler{topic: String::from("testing_topic/a")} ))
    .add_subscription( Box::new(MyHandler{topic: String::from("testing_topic/b"), address: my_actor_address} ))

} ```

Sending Messages

Externd the MqttPublisher with the handler of message of your choice.

Use the methods defined in MqttPublisherto send the message

```rust pub struct StringMessage{ pub payload : String, pub topic: String, }

impl Message for StringMessage { type Result = (); }

impl Handler for MqttPublisher { type Result = ();

fn handle(&mut self, msg: StringMessage, _: &mut Self::Context) -> Self::Result {
    self.publish_str(&msg.topic , &msg.payload);
    ()
}

} ```

Normal responsivities of this impl should limit to:

Testing

MqttPublisher is a type alias that maps to MqttPublisherActor. But during unit testing it is mapped to MqttPublisherActor_Mock.

This mock does not connect to anyplace and does not need the MqttConnectionManager to be initialized or even created.