axum-streams 0.6.0

HTTP body streaming support for Axus: JSON/CSV and others
Documentation

Cargo tests and formatting security audit

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.6", features=["json", "csv", "protobuf"] }

Example code:


#[derive(Debug, Clone, Deserialize, Serialize)]
struct MyTestStructure {
  some_test_field: String
}

fn source_test_stream() -> BoxStream<'static, MyTestStructure> {
  // Simulating a stream with a plain vector and throttling to show how it works
  Box::pin(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

Need client support?

There is the same functionality for:

Licence

Apache Software License (ASL)

Author

Abdulla Abdurakhmanov