9 releases (4 breaking)
0.8.1 | Feb 4, 2023 |
---|---|
0.8.0 | Nov 25, 2022 |
0.7.0 | Aug 14, 2022 |
0.5.0 | Jul 31, 2022 |
#602 in Network programming
122 downloads per month
Used in reqwest-streams
25KB
500 lines
axum streams for Rust
Library provides HTTP response streaming support for axum web framework:
- JSON array stream format
- JSON lines stream format
- CSV stream
- Protobuf len-prefixed stream format
This type of responses are useful when you are reading huge stream of objects from some source (such as database, file, etc) and want to avoid huge memory allocation.
Quick start
Cargo.toml:
[dependencies]
axum-streams = { version = "0.8", features=["json", "csv", "protobuf"] }
Compatibility matrix
axum | axum-streams |
---|---|
0.6 | 0.8 |
0.5 | 0.7 |
Example code:
#[derive(Debug, Clone, Deserialize, Serialize)]
struct MyTestStructure {
some_test_field: String
}
fn my_source_stream() -> impl Stream<Item=MyTestStructure> {
// Simulating a stream with a plain vector and throttling to show how it works
stream::iter(vec![
MyTestStructure {
some_test_field: "test1".to_string()
}; 1000
]).throttle(std::time::Duration::from_millis(50))
}
async fn test_json_array_stream() -> impl IntoResponse {
StreamBodyAs::json_array(source_test_stream())
}
async fn test_json_nl_stream() -> impl IntoResponse {
StreamBodyAs::json_nl(source_test_stream())
}
async fn test_csv_stream() -> impl IntoResponse {
StreamBodyAs::csv(source_test_stream())
}
All examples available at examples directory.
To run example use:
# cargo run --example json-example --features json
Need client support?
There is the same functionality for:
Licence
Apache Software License (ASL)
Author
Abdulla Abdurakhmanov
Dependencies
~6–12MB
~198K SLoC