actioncable/lib.rs
1//! A client for Rails' Action Cable.
2//!
3//! A [`Client`] owns one WebSocket connection to an Action Cable server and multiplexes any
4//! number of channel subscriptions over it. It keeps the connection alive the way the
5//! official JavaScript client does: the server beats a ping every three seconds, and a
6//! connection that goes quiet for longer than [`ClientBuilder::stale_after`] is torn down
7//! and redialed with backoff. Subscriptions survive reconnects; they are resubscribed as
8//! soon as the server says welcome.
9//!
10//! ```no_run
11//! use actioncable::{Client, Identifier};
12//! use serde_json::json;
13//!
14//! # async fn run() -> Result<(), actioncable::Error> {
15//! let client = Client::builder("wss://example.com/cable").build()?;
16//! client.connect().await?;
17//!
18//! let room = client
19//! .subscribe(Identifier::new("RoomChannel").param("id", 42))
20//! .await?;
21//!
22//! let reader = room.clone();
23//! tokio::spawn(async move {
24//! while let Some(message) = reader.next().await {
25//! println!("{message}");
26//! }
27//! });
28//!
29//! room.perform("speak", json!({ "body": "Hello!" })).await?;
30//! room.unsubscribe().await?;
31//! client.close().await;
32//! # Ok(())
33//! # }
34//! ```
35//!
36//! Two things are pluggable. A [`Transport`] carries bytes: the built-in
37//! `WebSocketTransport` speaks RFC 6455 over tokio-tungstenite behind the default
38//! `websocket` feature, and any other WebSocket library can be dropped in behind the same
39//! trait with [`ClientBuilder::transport`]. Without the feature and without a transport,
40//! [`ClientBuilder::build`] returns [`Error::NoTransport`]. A [`Protocol`] speaks one Action Cable
41//! wire format, negotiated as one WebSocket subprotocol: [`V1Json`] implements
42//! `actioncable-v1-json`, and a new format is a new `Protocol` rather than a fork of this
43//! client. [`ClientBuilder::protocols`] offers several, and the server picks the one it
44//! knows.
45//!
46//! Where the Go client takes a `context`, this one takes nothing: [`Client::connect`] and
47//! [`Client::subscribe`] wait until the server answers or the client stops, and a caller
48//! who wants a deadline wraps the call in [`tokio::time::timeout`]. Dropping either future
49//! is safe. The client keeps running — [`Client::last_error`] says what it is failing on —
50//! and a subscription the server never confirmed is forgotten.
51//!
52//! A client that stops for good says so through [`Client::done`] and [`Client::err`]: the
53//! server hung up and said not to come back, the attempts
54//! [`ClientBuilder::max_attempts`] allows ran out, or
55//! [`ClientBuilder::stop_on_error`] recognized a failure no reconnect can repair.
56//!
57//! Where the Go client takes callbacks, this one offers both: a stream of [`Event`]s on
58//! every [`Subscription`], and [`SubscribeRequest`] for the callback form on top of the
59//! same stream. Headers that expire come from a [`HeaderProvider`], which a closure
60//! returning a future satisfies. Nothing here logs through a logger of its own; the
61//! client's chatter goes to `tracing`.
62//!
63//! The `test-support` feature adds `actioncable::test_support`, an in-memory transport a
64//! test plays the server on, for testing code built on this client without a network.
65
66mod builder;
67mod client;
68mod error;
69mod identifier;
70mod message;
71mod protocol;
72mod subscription;
73#[cfg(feature = "test-support")]
74pub mod test_support;
75mod transport;
76mod v1_json;
77#[cfg(feature = "websocket")]
78mod websocket;
79
80pub use builder::{ClientBuilder, HeaderProvider};
81pub use client::Client;
82pub use error::{DisconnectReason, Error};
83pub use identifier::Identifier;
84pub use message::Message;
85pub use protocol::{Command, CommandName, Incoming, Kind, Protocol, SUBPROTOCOL_UNSUPPORTED};
86pub use subscription::{Event, SubscribeRequest, Subscription};
87pub use transport::{Conn, DialOptions, Transport};
88pub use v1_json::{SUBPROTOCOL_V1_JSON, V1Json};
89#[cfg(feature = "websocket")]
90pub use websocket::WebSocketTransport;
91
92/// Compiles the README's examples as doctests.
93#[cfg(all(doctest, feature = "websocket"))]
94#[doc = include_str!("../README.md")]
95pub struct ReadmeExamples;