pub struct Client<C: CodecType> { /* private fields */ }Expand description
A typed NATS client wrapper with configurable codec.
§Type Parameters
C- The codec type to use for message serialization (e.g.,MsgPackCodec,JsonCodec)
§Example
use intercom::{Client, MsgPackCodec};
// Using MessagePack codec (default)
let msgpack_client = Client::<MsgPackCodec>::connect("nats://localhost:4222").await?;Implementations§
Source§impl<C: CodecType> Client<C>
impl<C: CodecType> Client<C>
Sourcepub async fn connect<A: ToServerAddrs>(addrs: A) -> Result<Self>
pub async fn connect<A: ToServerAddrs>(addrs: A) -> Result<Self>
Connect to a NATS server.
§Example
use intercom::{Client, MsgPackCodec};
let client = Client::<MsgPackCodec>::connect("nats://localhost:4222").await?;Sourcepub async fn connect_with_options<A: ToServerAddrs>(
addrs: A,
options: ConnectOptions,
) -> Result<Self>
pub async fn connect_with_options<A: ToServerAddrs>( addrs: A, options: ConnectOptions, ) -> Result<Self>
Connect to a NATS server with custom options.
§Example
use intercom::{Client, MsgPackCodec};
let client = Client::<MsgPackCodec>::connect_with_options(
"nats://localhost:4222",
async_nats::ConnectOptions::new().name("my-app")
).await?;Sourcepub async fn publish<T: Serialize>(
&self,
subject: &str,
message: &T,
) -> Result<()>
pub async fn publish<T: Serialize>( &self, subject: &str, message: &T, ) -> Result<()>
Publish a typed message to a subject.
§Type Parameters
T- The message type (must implement Serialize)
§Example
use intercom::{Client, MsgPackCodec};
use serde::{Deserialize, Serialize};
#[derive(Serialize, Deserialize)]
struct MyMessage {
content: String,
}
let client = Client::<MsgPackCodec>::connect("nats://localhost:4222").await?;
// Using turbofish syntax
client.publish::<MyMessage>("subject", &MyMessage { content: "hello".into() }).await?;Sourcepub async fn request<T: Serialize, R: DeserializeOwned>(
&self,
subject: &str,
message: &T,
) -> Result<R>
pub async fn request<T: Serialize, R: DeserializeOwned>( &self, subject: &str, message: &T, ) -> Result<R>
Publish a typed message and wait for a reply.
§Type Parameters
T- The request message type (must implement Serialize)R- The response message type (must implement DeserializeOwned)
§Example
use intercom::{Client, MsgPackCodec};
use serde::{Deserialize, Serialize};
#[derive(Serialize, Deserialize)]
struct Request { query: String }
#[derive(Serialize, Deserialize)]
struct Response { result: String }
let client = Client::<MsgPackCodec>::connect("nats://localhost:4222").await?;
let response = client.request::<Request, Response>(
"service",
&Request { query: "hello".into() }
).await?;Sourcepub async fn publish_with_reply<T: Serialize>(
&self,
subject: &str,
reply: &str,
message: &T,
) -> Result<()>
pub async fn publish_with_reply<T: Serialize>( &self, subject: &str, reply: &str, message: &T, ) -> Result<()>
Publish a message with a specific reply subject.
§Type Parameters
T- The message type (must implement Serialize)
§Example
use intercom::{Client, MsgPackCodec};
use serde::{Deserialize, Serialize};
#[derive(Serialize, Deserialize)]
struct MyMessage { content: String }
let client = Client::<MsgPackCodec>::connect("nats://localhost:4222").await?;
client.publish_with_reply::<MyMessage>(
"subject",
"reply.subject",
&MyMessage { content: "hello".into() }
).await?;Sourcepub async fn subscribe<T: DeserializeOwned>(
&self,
subject: &str,
) -> Result<Subscriber<T, C>>
pub async fn subscribe<T: DeserializeOwned>( &self, subject: &str, ) -> Result<Subscriber<T, C>>
Subscribe to a subject with typed messages.
Returns a Subscriber that implements futures::Stream.
§Type Parameters
T- The message type (must implement DeserializeOwned)
§Example
use intercom::{Client, MsgPackCodec};
use serde::{Deserialize, Serialize};
use futures::StreamExt;
#[derive(Serialize, Deserialize, Debug)]
struct MyMessage { content: String }
let client = Client::<MsgPackCodec>::connect("nats://localhost:4222").await?;
let mut subscriber = client.subscribe::<MyMessage>("subject").await?;
while let Some(result) = subscriber.next().await {
match result {
Ok(msg) => println!("Received: {:?}", msg.payload),
Err(e) => eprintln!("Error: {}", e),
}
}Sourcepub async fn queue_subscribe<T: DeserializeOwned>(
&self,
subject: &str,
queue_group: &str,
) -> Result<Subscriber<T, C>>
pub async fn queue_subscribe<T: DeserializeOwned>( &self, subject: &str, queue_group: &str, ) -> Result<Subscriber<T, C>>
Subscribe to a subject as part of a queue group.
Messages are load-balanced across subscribers in the same queue group.
§Type Parameters
T- The message type (must implement DeserializeOwned)
§Example
use intercom::{Client, MsgPackCodec};
use serde::{Deserialize, Serialize};
#[derive(Serialize, Deserialize, Debug)]
struct MyMessage { content: String }
let client = Client::<MsgPackCodec>::connect("nats://localhost:4222").await?;
let subscriber = client.queue_subscribe::<MyMessage>("subject", "my-queue").await?;Sourcepub fn publisher<T: Serialize>(&self, subject: &str) -> Publisher<T, C>
pub fn publisher<T: Serialize>(&self, subject: &str) -> Publisher<T, C>
Create a typed publisher for a subject.
Returns a Publisher that implements futures::Sink.
§Type Parameters
T- The message type (must implement Serialize)
§Example
use intercom::{Client, MsgPackCodec};
use serde::{Deserialize, Serialize};
use futures::SinkExt;
#[derive(Serialize, Deserialize)]
struct MyMessage { content: String }
let client = Client::<MsgPackCodec>::connect("nats://localhost:4222").await?;
let mut publisher = client.publisher::<MyMessage>("subject");
publisher.send(MyMessage { content: "hello".into() }).await?;Sourcepub fn jetstream(&self) -> JetStreamContext<C>
pub fn jetstream(&self) -> JetStreamContext<C>
Create a JetStream context.
§Example
use intercom::{Client, MsgPackCodec};
let client = Client::<MsgPackCodec>::connect("nats://localhost:4222").await?;
let jetstream = client.jetstream();Sourcepub fn jetstream_with_domain(&self, domain: &str) -> JetStreamContext<C>
pub fn jetstream_with_domain(&self, domain: &str) -> JetStreamContext<C>
Create a JetStream context with a custom domain.
§Example
use intercom::{Client, MsgPackCodec};
let client = Client::<MsgPackCodec>::connect("nats://localhost:4222").await?;
let jetstream = client.jetstream_with_domain("my-domain");Sourcepub fn jetstream_with_prefix(&self, prefix: &str) -> JetStreamContext<C>
pub fn jetstream_with_prefix(&self, prefix: &str) -> JetStreamContext<C>
Create a JetStream context with a custom prefix.
§Example
use intercom::{Client, MsgPackCodec};
let client = Client::<MsgPackCodec>::connect("nats://localhost:4222").await?;
let jetstream = client.jetstream_with_prefix("my-prefix");Trait Implementations§
Auto Trait Implementations§
impl<C> Freeze for Client<C>
impl<C> !RefUnwindSafe for Client<C>
impl<C> Send for Client<C>
impl<C> Sync for Client<C>
impl<C> Unpin for Client<C>where
C: Unpin,
impl<C> !UnwindSafe for Client<C>
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more