Client

Struct Client 

Source
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>

Source

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?;
Source

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?;
Source

pub fn inner(&self) -> &Client

Get the underlying async-nats client.

Source

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?;
Source

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?;
Source

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?;
Source

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),
    }
}
Source

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?;
Source

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?;
Source

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();
Source

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");
Source

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");
Source

pub async fn flush(&self) -> Result<()>

Flush any pending messages.

Trait Implementations§

Source§

impl<C: Clone + CodecType> Clone for Client<C>

Source§

fn clone(&self) -> Client<C>

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more

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> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V

Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more