pub struct PushSubscription(_);
Expand description

A PushSubscription receives Messages published to specific NATS Subjects.

Implementations

Get the next message non-protocol message, or None if the subscription has been unsubscribed or the connection closed.

Example

if let Some(message) = subscription.next() {
    println!("Received {}", message);
}

Try to get the next non-protocol message, or None if no messages are present or if the subscription has been unsubscribed or the connection closed.

Example
if let Some(message) = subscription.try_next() {
    println!("Received {}", message);
}

Get the next message, or a timeout error if no messages are available for timout.

Example
if let Ok(message) = subscription.next_timeout(std::time::Duration::from_secs(1)) {
    println!("Received {}", message);
}

Returns a blocking message iterator. Same as calling iter().

Example
for message in subscription.messages() {
    println!("Received message {:?}", message);
}

Returns a blocking message iterator.

Example
for message in subscription.iter() {}

Returns a non-blocking message iterator.

Example
for message in subscription.try_iter() {

}

Returns a blocking message iterator with a time deadline for blocking.

Example
for message in subscription.timeout_iter(std::time::Duration::from_secs(1)) {
    println!("Received message {:?}", message);
}

Attach a closure to handle messages. This closure will execute in a separate thread. The result of this call is a Handler which can not be iterated and must be unsubscribed or closed directly to unregister interest. A Handler will not unregister interest with the server when drop(&mut self) is called.

Example
context.subscribe("with_handler")?.with_handler(move |message| {
    println!("received {}", &message);
    Ok(())
});

Attach a closure to process and acknowledge messages. This closure will execute in a separate thread.

The result of this call is a Handler which can not be iterated and must be unsubscribed or closed directly to unregister interest. A Handler will not unregister interest with the server when drop(&mut self) is called.

Example
context.subscribe("with_process_handler")?.with_process_handler(|message| {
    println!("Received {}", &message);

    Ok(())
});

Process and acknowledge a single message, waiting indefinitely for one to arrive.

Does not acknowledge the processed message if the closure returns an Err.

Example

let mut subscription = context.subscribe("process")?;
subscription.process(|message| {
    println!("Received message {:?}", message);

    Ok(())
})?;

Process and acknowledge a single message, waiting up to timeout configured timeout before returning a timeout error.

Does not ack the processed message if the internal closure returns an Err.

Example

let mut subscription = context.subscribe("process_timeout")?;
subscription.process_timeout(std::time::Duration::from_secs(1), |message| {
    println!("Received message {:?}", message);

    Ok(())
})?;

Sends a request to fetch current information about the target consumer.

Example
let subscription = context.subscribe("foo")?;
let info = subscription.consumer_info()?;

Unsubscribe a subscription immediately without draining. Use drain instead if you want any pending messages to be processed by a handler, if one is configured.

Example
let subscription = context.subscribe("unsubscribe")?;
subscription.unsubscribe()?;

Close a subscription. Same as unsubscribe

Use drain instead if you want any pending messages to be processed by a handler, if one is configured.

Example
let subscription = context.subscribe("close")?;
subscription.close()?;

Send an unsubscription and flush the connection, allowing any unprocessed messages to be handled by a Subscription

After the flush returns, we know that a round-trip to the server has happened after it received our unsubscription, so we shut down the subscriber afterwards.

A similar method exists on the Connection struct which will drain all subscriptions for the NATS client, and transition the entire system into the closed state afterward.

Example
let mut subscription = context.subscribe("drain")?;

context.publish("drain", "foo")?;
context.publish("drain", "bar")?;
context.publish("drain", "baz")?;

subscription.drain()?;

assert!(subscription.next().is_some());

Trait Implementations

Returns a copy of the value. Read more

Performs copy-assignment from source. Read more

Formats the value using the given formatter. Read more

The type of the elements being iterated over.

Which kind of iterator are we turning this into?

Creates an iterator from a value. Read more

The type of the elements being iterated over.

Which kind of iterator are we turning this into?

Creates an iterator from a value. Read more

Auto Trait Implementations

Blanket Implementations

Gets the TypeId of self. Read more

Immutably borrows from an owned value. Read more

Mutably borrows from an owned value. Read more

Returns the argument unchanged.

Calls U::from(self).

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

Should always be Self

The resulting type after obtaining ownership.

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

🔬 This is a nightly-only experimental API. (toowned_clone_into)

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

The type returned in the event of a conversion error.

Performs the conversion.

The type returned in the event of a conversion error.

Performs the conversion.