pub struct Stream { /* private fields */ }Expand description
Stream represents a realtime report stream. Safe for concurrent usage. When HA mode is enabled and at least 2 origins are provided, the Stream will maintain at least 2 concurrent connections to different instances to ensure high availability, fault tolerance and minimize the risk of report gaps.
Implementations§
Source§impl Stream
impl Stream
Sourcepub async fn new(
config: &Config,
feed_ids: Vec<ID>,
) -> Result<Stream, StreamError>
pub async fn new( config: &Config, feed_ids: Vec<ID>, ) -> Result<Stream, StreamError>
Establishes a streaming WebSocket connection that sends reports for the given feedID(s) after they are verified.
§Arguments
config- A validatedConfiginstance.feedIDs- A comma-separated list of Data Streams feed IDs.
§Endpoint:
/api/v1/ws§Type:
- WebSocket
§Sample Request:
GET /api/v1/ws?feedIDs=<feedID1>,<feedID2>,...§Sample Response:
{
"report": {
"feedID": "Hex encoded feedId.",
"fullReport": "A blob containing the report context and body. Encode the fee token into the payload before passing it to the contract for verification.",
"validFromTimestamp": "Report's earliest applicable timestamp (in seconds).",
"observationsTimestamp": "Report's latest applicable timestamp (in seconds)."
}
}§Error Response Codes
| Status Code | Description |
|---|---|
| 400 Bad Request | This error is triggered when: - There is any missing/malformed query argument. - Required headers are missing or provided with incorrect values. |
| 401 Unauthorized User | This error is triggered when: - Authentication fails, typically because the HMAC signature provided by the client doesn’t match the one expected by the server. - A user requests access to a feed without the appropriate permission or that does not exist. |
| 500 Internal Server | Indicates an unexpected condition encountered by the server, preventing it from fulfilling the request. This error typically points to issues on the server side. |
Sourcepub async fn listen(&mut self) -> Result<(), StreamError>
pub async fn listen(&mut self) -> Result<(), StreamError>
Starts listening for reports on the Stream. This method will spawn a new task for each WebSocket connection.
Sourcepub async fn read(&mut self) -> Result<WebSocketReport, StreamError>
pub async fn read(&mut self) -> Result<WebSocketReport, StreamError>
Reads the next available report on the Stream. Reads blocks until a report is received, the context is canceled or all underlying connections are in a error state.
§Returns
WebSocketReport- The next available report.
Sourcepub async fn close(&mut self) -> Result<(), StreamError>
pub async fn close(&mut self) -> Result<(), StreamError>
Closes the Stream. It is the caller’s responsibility to call close when the stream is no longer needed.
Sourcepub fn get_stats(&self) -> StatsSnapshot
pub fn get_stats(&self) -> StatsSnapshot
Returns basic stats about the Stream.
§Returns
StatsSnapshot- A snapshot of the current Stream statistics.accepted- Total number of accepted reports.deduplicated- Total number of deduplicated reports when in HA.total_received- Total number of received reports.partial_reconnects- Total number of partial reconnects when in HA.full_reconnects- Total number of full reconnects.configured_connections- Number of configured connections if in HA.active_connections- Current number of active connections.