Struct stan::Subscription[][src]

pub struct Subscription { /* fields omitted */ }

NATS Streaming subscription

Example:

 use nats;
 use std::{io, str::from_utf8, time};

 fn main() -> io::Result<()> {
     let nats_url = "nats://127.0.0.1:4222";
     let nc = nats::connect(nats_url)?;
     let sc = stan::connect(nc, "test-cluster", "rust-client-1")?;
      let sub1 = sc
          .subscribe("foo", Default::default())?
          .with_handler(|msg| {
              println!("sub1 got {:?}", from_utf8(&msg.data));
              msg.ack()?;
              println!("manually acked!");
              Ok(())
          });

      sc.subscribe("foo", Default::default())?
          .with_handler(|msg| {
              println!("sub 2 got {:?}", from_utf8(&msg.data));
              Ok(())
          });

      for msg in sc.subscribe("foo", Default::default())?.messages() {
          println!("sub 3 got {:?}", from_utf8(&msg.data));
          msg.ack()?;
      }

      for msg in sc
          .subscribe("foo", Default::default())?
          .timeout_iter(time::Duration::from_secs(1))
      {
          println!("sub 4 got {:?}", from_utf8(&msg.data));
          msg.ack()?;
      }

      Ok(())
  }

Implementations

impl Subscription[src]

pub fn messages(&self) -> Iter<'_>

Notable traits for Iter<'a>

impl<'a> Iterator for Iter<'a> type Item = Message;
[src]

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

      for msg in sc.subscribe("foo", Default::default())?.messages() {
          println!("received: {:?}", from_utf8(&msg.data));
          msg.ack()?;
      }

pub fn iter(&self) -> Iter<'_>

Notable traits for Iter<'a>

impl<'a> Iterator for Iter<'a> type Item = Message;
[src]

Returns a blocking message iterator.

      for msg in sc.subscribe("foo", Default::default())?.iter() {
          println!("received: {:?}", from_utf8(&msg.data));
          msg.ack()?;
      }

pub fn try_iter(&self) -> TryIter<'_>

Notable traits for TryIter<'a>

impl<'a> Iterator for TryIter<'a> type Item = Message;
[src]

Returns a non-blocking message iterator.

      for msg in sc.subscribe("foo", Default::default())?.try_iter() {
          println!("received: {:?}", from_utf8(&msg.data));
          msg.ack()?;
      }

pub fn timeout_iter(&self, timeout: Duration) -> TimeoutIter<'_>

Notable traits for TimeoutIter<'a>

impl<'a> Iterator for TimeoutIter<'a> type Item = Message;
[src]

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

      for msg in sc
          .subscribe("foo", Default::default())?
          .timeout_iter(time::Duration::from_secs(1))
      {
          println!("received: {:?}", from_utf8(&msg.data));
          msg.ack()?;
      }

pub fn with_handler<F>(self, handler: F) -> Handler where
    F: Fn(&Message) -> Result<()> + Send + 'static, 
[src]

Process subscription messages in a separate thread. Messages are automatically acked unless the handler returns an error. Messages can also be manually acked by calling msg.ack().

Examples:

Automatic ack:

     sc.subscribe("foo", Default::default())?
         .with_handler(|msg| {
             println!("{:?}", from_utf8(&msg.data));
             Ok(())
         });

Manual ack:

     sc.subscribe("foo", Default::default())?
         .with_handler(|msg| {
             println!("{:?}", from_utf8(&msg.data));
             msg.ack()?;
             println!("this happens after the ack");
             Ok(())
         });

pub fn next(&self) -> Option<Message>[src]

Get the next message with blocking, or None if the subscription has been closed Note: the message needs to be manually acked!

 use nats;
 use std::{io, str::from_utf8};
 fn main() -> io::Result<()> {
    let nats_url = "nats://127.0.0.1:4222";
    let nc = nats::connect(nats_url)?;
    let sc = stan::connect(nc, "test-cluster", "rust-client-1")?;
    sc.publish("foo", "hello from rust 1")?;

    let sub = sc.subscribe("foo", Default::default())?;
    if let Some(msg) = sub.next() {
       println!("received: {:?}", from_utf8(&msg.data));
       msg.ack()?
    }

    Ok(())
 }

pub fn try_next(&self) -> Option<Message>[src]

Get the next message without blocking, or None if none available Note: the message needs to be manually acked!

 use nats;
 use std::{io, str::from_utf8};
 fn main() -> io::Result<()> {
    let nats_url = "nats://127.0.0.1:4222";
    let nc = nats::connect(nats_url)?;
    let sc = stan::connect(nc, "test-cluster", "rust-client-1")?;
    sc.publish("foo", "hello from rust 1")?;

    let sub = sc.subscribe("foo", Default::default())?;
    if let Some(msg) = sub.try_next() {
       println!("received: {:?}", from_utf8(&msg.data));
       msg.ack()?
    }

    Ok(())
 }

pub fn next_timeout(&self, timeout: Duration) -> Result<Message>[src]

Get the next message without blocking, or None if none available Note: the message needs to be manually acked!

 use nats;
 use std::{io, str::from_utf8};
 fn main() -> io::Result<()> {
    let nats_url = "nats://127.0.0.1:4222";
    let nc = nats::connect(nats_url)?;
    let sc = stan::connect(nc, "test-cluster", "rust-client-1")?;
    sc.publish("foo", "hello from rust 1")?;

    let sub = sc.subscribe("foo", Default::default())?;
    if let Ok(msg) = sub.next_timeout(std::time::Duration::from_secs(1)) {
       println!("received: {:?}", from_utf8(&msg.data));
       msg.ack()?
    }

    Ok(())
 }

pub fn unsubscribe(&self) -> Result<()>[src]

Close this subscription.

For subscriptions that are not durable (i.e. with no durable_name), this is called automatically when the subscription is dropped.

For durable subscriptions, beware that unsubscribing all the clients will also delete the durable queue.

 use nats;
 use std::{io, str::from_utf8};
 fn main() -> io::Result<()> {
    let nats_url = "nats://127.0.0.1:4222";
    let nc = nats::connect(nats_url)?;
    let sc = stan::connect(nc, "test-cluster", "rust-client-1")?;

    let sub = sc.subscribe("foo", Default::default())?;
    sub.unsubscribe();

    Ok(())
 }

Trait Implementations

impl Clone for Subscription[src]

Auto Trait Implementations

Blanket Implementations

impl<T> Any for T where
    T: 'static + ?Sized
[src]

impl<T> Borrow<T> for T where
    T: ?Sized
[src]

impl<T> BorrowMut<T> for T where
    T: ?Sized
[src]

impl<T> From<T> for T[src]

impl<T, U> Into<U> for T where
    U: From<T>, 
[src]

impl<T> Same<T> for T

type Output = T

Should always be Self

impl<T> ToOwned for T where
    T: Clone
[src]

type Owned = T

The resulting type after obtaining ownership.

impl<T, U> TryFrom<U> for T where
    U: Into<T>, 
[src]

type Error = Infallible

The type returned in the event of a conversion error.

impl<T, U> TryInto<U> for T where
    U: TryFrom<T>, 
[src]

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

The type returned in the event of a conversion error.

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