Skip to main content

Client

Struct Client 

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

One connection to an Action Cable server and the subscriptions running over it. Build one with builder, start it with connect, and hang up with close. Handles are cheap to clone and share the connection.

The connection lives until close, not until the last handle is dropped: a client that is never closed keeps reconnecting.

Implementations§

Source§

impl Client

Source

pub fn builder(url: impl Into<String>) -> ClientBuilder

A client for an Action Cable endpoint, typically wss://host/cable. It speaks over the built-in WebSocket transport unless ClientBuilder::transport says otherwise.

Source

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

Starts the client and returns once the server has sent its welcome. Failed attempts are retried until that happens, the server tells us not to come back, or the attempts run out, so wrap the call in tokio::time::timeout for a deadline.

Dropping the future bounds the wait, not the client: where the Go client stops on a context that ends, this one keeps dialing until close, because a dropped future is how a select! loses a race and killing the connection over that would surprise everyone. A caller that has given up should close, and last_error says what the client was waiting out.

Returns Error::AlreadyConnected on a client that is already running, and on one that has stopped, whatever stopped it.

Source

pub fn connected(&self) -> bool

Whether a connection is up and welcomed.

Source

pub async fn done(&self)

Waits until the client has stopped for good — closed, told by the server not to come back, out of attempts, or stopped by stop_on_error — and will neither reconnect nor deliver anything more. err says why. Where the Go client hands out a Done() channel, here the wait is the future itself.

Source

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

Why the client stopped, and None while it is still running or has yet to be started.

Source

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

Why the last connection attempt failed, whether or not the client is still trying. It is what a connect that ran out of patience was waiting out, so a credential that can’t be built doesn’t hide behind a deadline. None until an attempt fails.

Source

pub async fn subscribe( &self, request: impl Into<SubscribeRequest>, ) -> Result<Subscription, Error>

Subscribes to a channel and returns once the server confirms it. Takes an Identifier, or a SubscribeRequest when there are callbacks to attach. The subscription outlives reconnects, resubscribed automatically, so it stays valid until Subscription::unsubscribe.

Subscribing to an identifier the client already holds joins the existing subscription rather than asking the server again, which Rails would ignore: a confirmed one confirms the new handle at once, and one still waiting shares its verdict with the new handle.

Returns Error::Rejected when the channel turns the subscription down, and Error::NotConnected before connect. Dropping the future, from a tokio::time::timeout say, forgets the subscription and, when nothing else holds the identifier, tells the server to drop it too; a confirmation that arrives later goes nowhere.

Source

pub async fn close(&self)

Hangs up, stops reconnecting, and ends every subscription’s streams. Safe to call twice, and final: connect afterwards returns Error::Closed.

Trait Implementations§

Source§

impl Clone for Client

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 Client

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