Skip to main content

Subscription

Struct Subscription 

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

One channel subscription on a client. Read what the channel sends with next, watch the connection with next_event, and talk back with perform or send.

Handles are cheap to clone and share one stream of messages: a message goes to whichever handle reads it first. The subscription outlives reconnects, resubscribed automatically, until unsubscribe. Dropping the last handle unsubscribes too, when it happens on a tokio runtime; elsewhere it stops delivery and warns, and the server keeps the subscription until the connection ends.

Implementations§

Source§

impl Subscription

Source

pub fn key(&self) -> &str

The JSON identifier the server knows this subscription by, and echoes back on everything it sends here.

Source

pub async fn next(&self) -> Option<Message>

The next message the channel broadcast or transmitted, or None once the subscription is unsubscribed, rejected, or the client is closed — err says which. The last callback has returned by then, so a loop that ends knows none is still running or about to.

Read it promptly. A message that arrives with the buffer full is dropped and logged rather than stalling the connection; ClientBuilder::message_buffer sizes the buffer for a slow reader.

Source

pub fn err(&self) -> Option<Error>

Why the subscription ended: Error::Unsubscribed, Error::Rejected, or whatever stopped the client. None while it is live.

Source

pub async fn next_event(&self) -> Option<Event>

The next connection event, in the order they happened, or None once the subscription is over.

A handle keeps the last sixteen events for a reader that isn’t listening, and a reader further behind than that skips the oldest and is told so in the log. Nothing is lost while events are read as they come, and a handle that never reads them costs nothing however long the client runs.

Source

pub async fn perform( &self, action: &str, data: impl Serialize, ) -> Result<(), Error>

Invokes an action on the channel, the equivalent of the JavaScript client’s perform. data must encode to a JSON object, the only shape Rails routes to an action, or to null, which sends the action alone; anything else is Error::DataNotAnObject.

Source

pub async fn send(&self, data: impl Serialize) -> Result<(), Error>

Delivers data to the channel as-is, without naming an action. Rails routes it to the channel’s receive method.

Source

pub async fn unsubscribe(&self) -> Result<(), Error>

Tells the server to drop the subscription and ends both streams. Other subscriptions to the same identifier keep the server’s; it only hears about the unsubscribe from the last of them.

Trait Implementations§

Source§

impl Clone for Subscription

Source§

fn clone(&self) -> Self

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

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

Performs copy-assignment from source. Read more
Source§

impl Debug for Subscription

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> 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 = !

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

fn try_from(value: U) -> Result<T, !>

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