Expand description
Inspired and compatible with Moleculer JS
You can currently do all the basics of emit, broadcast and call.
However it only works with the NATS transporter and JSON serializer/deserializer.
§Getting Started
Simple example showing how to receive an event, and responding to a request, for more check the examples folder.
use std::error::Error;
use serde::Deserialize;
use moleculer::{
config::{ConfigBuilder, Transporter},
service::{EventBuilder, Service, ActionBuilder},
EventContext, ActionContext, ServiceBroker,
};
#[tokio::main]
async fn main() -> eyre::Result<()> {
env_logger::init();
color_eyre::install()?;
// build config
let config = ConfigBuilder::default().transporter(Transporter::nats("nats://localhost:4222"))
.build();
// create the first event
let print_hi = EventBuilder::new("printHi").add_callback(print_hi).build();
// create the second event
let print_name = EventBuilder::new("printName")
.add_callback(print_name)
.build();
// create math action
let math_action = ActionBuilder::new("mathAdd").add_callback(math_add).build();
// create a service with events and actions
let greeter_service = Service::new("rustGreeter")
.add_event(print_hi)
.add_event(print_name)
.add_action(math_action);
// create service broker with service
let service_broker = ServiceBroker::new(config).add_service(greeter_service);
// start the service broker
service_broker.start().await;
Ok(())
}
// callback for first event, will be called whenever "printHi" event is received
fn print_hi(_ctx: EventContext) -> Result<(), Box<dyn Error>> {
println!("Hello from Rust");
Ok(())
}
// callback for second event, will be called whenever "printName" event is received
fn print_name(ctx: EventContext) -> Result<(), Box<dyn Error>> {
let msg: PrintNameMessage = serde_json::from_value(ctx.params)?;
println!("Hello to: {} from Rust", msg.name);
Ok(())
}
// callback for math action
fn math_add(ctx: ActionContext) -> Result<(), Box<dyn Error>> {
// get message decode using serde
let msg: ActionMessage = serde_json::from_value(ctx.params.clone())?;
let answer = msg.a + msg.b;
// serialize reply using serde and send reply
let _ = ctx.reply(answer.into());
Ok(())
}
#[derive(Deserialize)]
struct PrintNameMessage {
name: String,
}
#[derive(Deserialize)]
struct ActionMessage {
a: i32,
b: i32,
}Modules§
- config
- Create Config struct using ConfigBuilder with global settings for you micro-service.
- service
- Create Service struct with Events and Actions.
Structs§
- Service
Broker - The struct used to interact with moleculer.
Use
emit(),broadcast()andcall()functions.
Type Aliases§
- Action
Context - An alias to service::Context<service::Action>.
Send a response to a request using
reply(). - Event
Context - An alias to service::Context<service::Event>.
In all contexts
emit(),broadcast()andcall()are available.