Skip to main content

Subscriber

Struct Subscriber 

Source
pub struct Subscriber { /* private fields */ }
Expand description

One subscribed connection. Owns either a TCP socket or an in-process Subscription; the variant is chosen by the URL scheme in Subscriber::open / Subscriber::connect.

Implementations§

Source§

impl Subscriber

Source

pub fn connect(url: &str) -> Result<Self>

Open a fresh connection without subscribing to anything yet. Call Self::subscribe / Self::psubscribe next.

Accepted URLs:

  • kevy://, redis://, tcp:// — TCP RESP server
  • mem://<name>, file:///path — in-process shared bus
  • mem:// (anonymous), rediss://, kevys://, redis://user:pass@… are rejected with io::ErrorKind::Unsupported
Source

pub fn open(url: &str, channels: &[&[u8]]) -> Result<Self>

Open and subscribe to one or more channels in one step. Returns ErrorKind::InvalidInput if channels is empty (use Self::connect for an empty start).

Source

pub fn subscribe(&mut self, channels: &[&[u8]]) -> Result<()>

SUBSCRIBE channel [channel ...]. Per-channel Subscribe acks are delivered via Self::recv.

Source

pub fn psubscribe(&mut self, patterns: &[&[u8]]) -> Result<()>

PSUBSCRIBE pattern [pattern ...]. Patterns use Redis glob syntax (*, ?, […]).

Source

pub fn unsubscribe(&mut self, channels: &[&[u8]]) -> Result<()>

UNSUBSCRIBE [channel ...]. Empty channels unsubscribes from every channel (Redis wire semantics).

Source

pub fn punsubscribe(&mut self, patterns: &[&[u8]]) -> Result<()>

PUNSUBSCRIBE [pattern ...]. Empty patterns unsubscribes from every pattern.

Source

pub fn recv(&mut self) -> Result<PubsubEvent>

Block until the next pubsub frame arrives. Apply Self::set_read_timeout for bounded blocking. Connection close / bus tear-down yields ErrorKind::UnexpectedEof.

Source

pub fn recv_message(&mut self) -> Result<(Vec<u8>, Vec<u8>)>

Block until the next published Message / Pmessage arrives, silently skipping subscription-acknowledgement frames (PubsubEvent::Subscribe / PubsubEvent::Unsubscribe / PubsubEvent::Psubscribe / PubsubEvent::Punsubscribe) along the way.

This is the form most callers want — almost no consumer of pubsub needs to see the ack frames (they’re a wire-protocol detail), so a loop+match around Self::recv is essentially boilerplate. Returns (channel, payload). For pattern matches, channel is the concrete channel the publisher used (matching Redis’s pmessage shape, where pattern is discarded — use Self::recv directly if you need it).

Errors from Self::recv (connection close, timeout, etc.) propagate unchanged.

Source

pub fn hello3(&mut self) -> Result<PubsubEvent>

Negotiate RESP3 on this connection by sending HELLO 3 and draining the ack. Subsequent SUBSCRIBE / PSUBSCRIBE / PUBLISH deliveries arrive as push frames (>N\r\n…) instead of the legacy RESP2 array shape (*N\r\n…); Self::recv accepts both transparently, so existing code keeps working with no other changes.

Remote-only: the embedded backend has no proto negotiation concept (frames go through the in-process bus typed). Calling hello3 on an embedded Subscriber returns io::ErrorKind::Unsupported.

Must be called BEFORE any Self::subscribe / Self::psubscribe — Redis requires HELLO be the first command on a connection that uses it.

Source

pub fn set_read_timeout(&mut self, dur: Option<Duration>) -> Result<()>

Apply (or clear) a read timeout. After setting Some(dur), Self::recv returns an io::Error of kind WouldBlock / TimedOut when no frame arrives within dur.

Source

pub fn events(&mut self) -> SubscriberEvents<'_>

Borrowing iterator over every pubsub frame — ack frames included. Each next() is one blocking Self::recv. Terminates (None) when the underlying stream / bus is gone (ErrorKind::UnexpectedEof); every other error is surfaced as Some(Err(_)) so the caller can decide whether to retry (e.g. a read timeout) or break.

kevy stays 0-deps so this is a std::iter::Iterator, not a futures::Stream. Async runtimes consume it via spawn_blocking (see docs/pubsub.md).

Source

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

Borrowing iterator that silently skips (p)?(un)?subscribe acks and yields the payload tuples consumers actually want. Mirrors Self::recv_message in iterator form. For Pmessage the pattern is discarded — fall back to Self::events if you need it.

Trait Implementations§

Source§

impl Debug for Subscriber

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

Auto Trait Implementations§

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> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

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, 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.