axum streams for Rust
Library provides HTTP response streaming support for axum web framework:
- JSON array stream format
- Support for simple envelopes structures when you need to include your array inside some object (only for first level)
- JSON lines stream format
- CSV stream
- Protobuf len-prefixed stream format
- Apache Arrow IPC stream format
- Text stream
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:
[]
= { = "0.21", =["json", "csv", "protobuf", "text", "arrow"] }
Compatibility matrix
axum | axum-streams |
---|---|
0.8 | v0.20+ |
0.7 | v0.11-0.19 |
Example code:
async
async
async
async
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:
Configuration of the frame size
By default, the library produces an HTTP frame per item in the stream.
You can change this is using StreamAsOptions
:
.buffering_ready_items
.json_array
new
Error handling
The library provides a way to propagate errors in the stream:
async
JSON array inside another object
Sometimes you need to include your array inside some object, e.g.:
The wrapping object that includes data
field here is called envelope further.
You need to define both of your structures: envelope and records inside:
And use json_array_with_envelope
instead of json_array
.
Have a look at json-array-complex-structure.rs for detail example.
The support is limited:
- Only first level of nesting is supported to avoid complex implementation with performance impact.
- You need either remove the target array field from
envelope
structure or use this Serde trick on the field to avoid JSON serialization issues:
Licence
Apache Software License (ASL)
Author
Abdulla Abdurakhmanov