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
impl Client
Sourcepub fn builder(url: impl Into<String>) -> ClientBuilder
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.
Sourcepub async fn connect(&self) -> Result<(), Error>
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.
Sourcepub async fn done(&self)
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.
Sourcepub fn err(&self) -> Option<Error>
pub fn err(&self) -> Option<Error>
Why the client stopped, and None while it is still running or has yet to be
started.
Sourcepub fn last_error(&self) -> Option<Error>
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.
Sourcepub async fn subscribe(
&self,
request: impl Into<SubscribeRequest>,
) -> Result<Subscription, Error>
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.
Sourcepub async fn close(&self)
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.