Crates.io | meshpulse |
lib.rs | meshpulse |
version | 0.2.1 |
source | src |
created_at | 2024-02-09 15:20:25.239591 |
updated_at | 2024-06-13 09:30:03.559913 |
description | Meshpulse is a Rust library for easy communication between microservices, based on either MQTT, AMQP, gRPC or HTTP. |
homepage | https://github.com/tluijken/meshpulse |
repository | https://github.com/tluijken/meshpulse |
max_upload_size | |
id | 1134032 |
size | 28,338 |
Meshpulse is a Rust crate designed to facilitate seamless communication between microservices through events or RPC (Remote Procedure Call) calls. It abstracts away the complexities of communication protocols such as MQTT, gRPC, AMQP, or HTTP, allowing developers to focus on building robust, scalable microservices without getting bogged down by the intricacies of inter-service communication. Features
To use Meshpulse in your Rust project, simply add it as a dependency in your Cargo.toml file:
[dependencies]
# To use with MQTT
meshpulse = { version = "0.2.0", features = ["mqtt"]}
Here's a basic example demonstrating how to use Meshpulse for event-driven communication:
use meshpulse::prelude::*;
#[derive(Serialize, Deserialize, Event)]
struct TestEvent {
message: String,
}
fn main() {
let event = TestEvent {
message: "hello".to_string(),
};
let result = event.publish();
assert_eq!(result.is_ok(), true);
}
use meshpulse::prelude::*;
#[derive(Serialize, Deserialize, Event)]
struct TestEvent {
message: String,
}
fn main() {
let sub_result = TestEvent::subscribe(|event| {
println!("Received event: {:?}", event.message);
});
assert_eq!(sub_result.is_ok(), true);
// When you're no longer interested in events...unsubscribe
let unsub_result = sub_result.unwrap().unsubscribe();
assert_eq!(unsub_result.is_ok(), true);
}
To and execute an RPC request
use meshpulse::prelude::*;
#[derive(Serialize, Deserialize, RpcRequest)]
struct MultiplierRequest{
number: i32,
multiplier: i32
}
async fn main() {
let request = MultiplierRequest {
number: 5,
multiplier: 2
};
let response = request.request::<i32>().await.unwrap();
assert_eq!(response, 10);
}
To subscribe to RPC requests and assign a handler function for requests:
use meshpulse::prelude::*;
fn handle_multiplier_request(request: MultiplierRequest) -> Result<i32, Box<dyn std::error::Error>> {
Ok(request.number * request.multiplier)
}
let handler = RpcRequestHandler::start(handle_multiplier_request);
// keep the handler alive as long as required.
// To stop the handler
handler.stop();
To communicate with over MQTT, Meshpulse will need the following environment variables
MQTT_HOST='tcp://your-host:1883'
MQTT_USERNAME='your-username'
MQTT_PASSWORD='some secret passw0rd'
For more detailed usage instructions and examples, please refer to the documentation. Contributing
Contributions are welcome! If you encounter any issues or have suggestions for improvement, please feel free to open an issue or submit a pull request on the GitHub repository. License
This project is licensed under the MIT License - see the LICENSE file for details.