pub struct SubscribeRequest { /* private fields */ }Expand description
What to subscribe to, and what to do when its connection changes. The callbacks mirror
the Go client’s OnConnected, OnDisconnected and OnRejected; the same events also
arrive on Subscription::next_event, so use whichever reads better. A bare
Identifier converts into a request with no callbacks, which is what
Client::subscribe takes most of the time.
use actioncable::{Client, Identifier, SubscribeRequest};
let room = client
.subscribe(
SubscribeRequest::new(Identifier::new("RoomChannel"))
.on_connected(|reconnected| async move {
if reconnected {
println!("catch up");
}
})
.on_disconnected(|will_reconnect| async move {
println!("gone, back soon: {will_reconnect}");
}),
)
.await?;Callbacks are async: each returns a future, and |x| async move { .. } is how a plain
closure becomes one. They run on their own tokio task, one at a time, in the order the
events happened, never on the connection’s task, and the next event waits for the
current callback to finish. Client::close,
Client::subscribe and
Subscription::unsubscribe can all be awaited from inside one. They live as long as
the Subscription does: dropping the last handle ends the callbacks along with
delivery.
Implementations§
Source§impl SubscribeRequest
impl SubscribeRequest
Sourcepub fn new(identifier: Identifier) -> SubscribeRequest
pub fn new(identifier: Identifier) -> SubscribeRequest
A request for identifier, with no callbacks yet.
Sourcepub fn on_connected<Fut>(
self,
callback: impl Fn(bool) -> Fut + Send + Sync + 'static,
) -> SubscribeRequest
pub fn on_connected<Fut>( self, callback: impl Fn(bool) -> Fut + Send + Sync + 'static, ) -> SubscribeRequest
Called every time the server confirms the subscription, including after a reconnect,
which is what reconnected reports.
Sourcepub fn on_disconnected<Fut>(
self,
callback: impl Fn(bool) -> Fut + Send + Sync + 'static,
) -> SubscribeRequest
pub fn on_disconnected<Fut>( self, callback: impl Fn(bool) -> Fut + Send + Sync + 'static, ) -> SubscribeRequest
Called when the connection drops, with whether the client intends to dial again.
Sourcepub fn on_rejected<Fut>(
self,
callback: impl Fn() -> Fut + Send + Sync + 'static,
) -> SubscribeRequest
pub fn on_rejected<Fut>( self, callback: impl Fn() -> Fut + Send + Sync + 'static, ) -> SubscribeRequest
Called when the channel rejects the subscription.