Skip to content

Latest commit

 

History

History

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
 
 
 
 
 
 
 
 

README.md

Caryatid simple publish-subscribe example

This example is the simplest possible pub-sub model. There are two modules, a Publisher and a Subscriber. The publisher publishes a message on topic sample.test and the subscriber subscribes to sample.#. The messages are JSON objects.

There are two message buses, the internal in-memory one and an external RabbitMQ one. The routing is set up so all messages go to both buses, so the subscriber gets two copies of the message.

How to run it

$ cd examples/simple
$ cargo run

Points of interest

Take a look at the definition of MType in main.rs

type MType = serde_json::Value;

and how it is used to define the type of the Process:

    let mut process = Process::<MType>::create(config).await;

In the the subscriber you can see how you can subscribe with a lambda which receives the message wrapped in an Arc:

        context.message_bus.subscribe(&topic,
                                      |message: Arc<serde_json::Value>| {
           info!("Received: {:?}", message);
        })?;

Then in the publisher you can see it asynchronously publishing a message:

        context.run(async move {

            let test_message = Arc::new(json!({
                "message": "Hello, world!",
            }));

            info!("Sending {:?}", test_message);

            message_bus.publish(&topic, test_message)
                .await.expect("Failed to publish message");
        });