Available on crate feature
nats only.Expand description
This module provides bindings between cloudevents-sdk and nats
§Examples
Deserialize nats::Message into Event
use nats_lib as nats;
use cloudevents::binding::nats::MessageExt;
use futures::StreamExt;
#[tokio::main]
async fn main() {
let nc = nats::connect("localhost:4222").await.unwrap();
let mut sub = nc.subscribe("test").await.unwrap();
// Process messages one at a time
sub.for_each_concurrent(1, |nats_message| async move {
let cloud_event = nats_message.to_event().unwrap();
println!("{}", cloud_event.to_string());
}).await;
}Serialize Event into NatsCloudEvent and publish to nats subject
use nats_lib as nats;
use cloudevents::binding::nats::NatsCloudEvent;
use cloudevents::{EventBuilder, EventBuilderV10, Event};
use serde_json::json;
#[tokio::main]
async fn main() {
let nc = nats::connect("localhost:4222").await.unwrap();
let event = EventBuilderV10::new()
.id("123".to_string())
.ty("example.test")
.source("http://localhost/")
.data("application/json", json!({"hello": "world"}))
.build()
.unwrap();
let nats_payload = NatsCloudEvent::from_event(event).unwrap();
nc.publish("whatever.subject.you.like", nats_payload.payload.into()).await.unwrap();
}Structs§
- Nats
Cloud Event - Helper struct containing text data bytes of JSON serialized Event
Traits§
- Message
Ext - Trait implemented by
nats::Messageto enable convenient deserialization toEvent