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
impl Subscription
Sourcepub fn key(&self) -> &str
pub fn key(&self) -> &str
The JSON identifier the server knows this subscription by, and echoes back on everything it sends here.
Sourcepub async fn next(&self) -> Option<Message>
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.
Sourcepub fn err(&self) -> Option<Error>
pub fn err(&self) -> Option<Error>
Why the subscription ended: Error::Unsubscribed, Error::Rejected, or
whatever stopped the client. None while it is live.
Sourcepub async fn next_event(&self) -> Option<Event>
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.
Sourcepub async fn perform(
&self,
action: &str,
data: impl Serialize,
) -> Result<(), Error>
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.
Sourcepub async fn send(&self, data: impl Serialize) -> Result<(), Error>
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.
Sourcepub async fn unsubscribe(&self) -> Result<(), Error>
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.