Status Events for Rust SDK
Subscription loop
These status events apply to the subscription implementation in the Rust SDK introduced and enabled by default in version 0.3.0.
The PubNub Rust SDK provides status events to notify you about the state of the SDK through the ConnectionStatus
enum.
Subscription-related statuses
The ConnectionStatus
enum defines the following subscription-related values:
Status | Description | Added in version |
---|---|---|
Connected | The connection has been established and is ready to receive real-time updates. Triggered when handshake is successful and subscription begins. | 0.2.1 |
Disconnected | The connection has been intentionally terminated. Triggered when user manually disconnects or unsubscribes. | 0.3.0 |
DisconnectedUnexpectedly(PubNubError) | The connection was unexpectedly lost, with details about the error. This status includes the specific PubNubError that caused the disconnection. | 0.2.1 |
ConnectionError(PubNubError) | Failed to establish the initial connection, with details about the error. This status includes the specific PubNubError that caused the connection failure. | 0.3.0 |
SDK connection lifecycle
The subscribe loop implementation in version 0.3.0 provides a more sophisticated state machine for handling connection statuses compared to earlier versions of the SDK. For more general information on statuses and reconnection policies, refer to SDK Connection Lifecycle.
Error handling
The Rust SDK provides detailed error information through the PubNubError
type, which is included in the ConnectionError
and DisconnectedUnexpectedly
status variants.
Common error types
Errors in the Rust SDK are structured as PubNubError
instances, which provide:
- Error type classification
- Detailed error message
- Context information about the operation that failed
- Underlying cause when available
You can inspect the error details by pattern matching on the status variants that contain errors:
tokio::spawn(client.status_stream().for_each(|status| async move {
match status {
ConnectionStatus::ConnectionError(error) => {
println!("Connection error: {:?}", error);
// Handle connection error
},
ConnectionStatus::DisconnectedUnexpectedly(error) => {
println!("Unexpected disconnection: {:?}", error);
// Handle unexpected disconnection
},
// Handle other status types
_ => {}
}
}));
Subscription handling
You can access the status stream by calling client.status_stream()
on your PubNub client instance. This returns a stream of ConnectionStatus
events that you can observe in your application.