17 releases (6 breaking)

0.7.3 Sep 28, 2023
0.7.2 Jul 29, 2023
0.7.0 Jun 10, 2023
0.5.0 Mar 31, 2023
0.1.0 Nov 23, 2021

#108 in WebAssembly

Download history 845/week @ 2024-01-12 317/week @ 2024-01-19 398/week @ 2024-01-26 526/week @ 2024-02-02 335/week @ 2024-02-09 421/week @ 2024-02-16 349/week @ 2024-02-23 271/week @ 2024-03-01 433/week @ 2024-03-08 368/week @ 2024-03-15 484/week @ 2024-03-22 1096/week @ 2024-03-29 876/week @ 2024-04-05 468/week @ 2024-04-12 1674/week @ 2024-04-19 711/week @ 2024-04-26

4,103 downloads per month
Used in 5 crates (3 directly)


5.5K SLoC

Fluvio SmartModules

This crate provides types and macros for creating custom SmartModules, which are WebAssembly modules that may be used to modify the behavior of Fluvio streams to consumers. The currently supported SmartModule types are filters, which may describe records to keep in or discard from a stream.

Writing SmartModules

See the examples directory for full examples.

All SmartModules require adding crate-type = ['cdylib'] to the Cargo.toml. For a quick setup using cargo-generate, see the SmartModule template.

name = "fluvio-wasm-filter"
version = "0.1.0"
authors = ["Fluvio Contributors <team@fluvio.io>"]
edition = "2018"

crate-type = ['cdylib']

fluvio-smartmodule = "0.1.0"


For filtering, write your smartmodule using #[smartmodule(filter)] on your top-level function. Consider this the "main" function of your SmartModule.

use fluvio_smartmodule::{smartmodule, Record, Result};

pub fn filter(record: &Record) -> Result<bool> {
    let string = std::str::from_utf8(record.value.as_ref())?;

This filter will keep only records whose contents contain the letter a.


Mapping functions use #[smartmodule(map)], and are also a top-level entrypoint.

use fluvio_smartmodule::{smartmodule, Record, RecordData, Result};

pub fn map(record: &Record) -> Result<(Option<RecordData>, RecordData)> {
    let key = record.key.clone();

    let string = std::str::from_utf8(record.value.as_ref())?;
    let int = string.parse::<i32>()?;
    let value = (int * 2).to_string();

    Ok((key, value.into()))

This SmartModule will read each input Record as an integer (i32), then multiply it by 2.


Aggregate functions are a way to combine the data from many input records. Each time the aggregate function is called, it receives an "accumulated" value as well as the value of the current record in the stream, and is expected to combine the accumulator with the value to produce a new accumulator. This new accumulator value will be passed to the next invocation of aggregate with the next record value. The resulting stream of values is the output accumulator from each step.

use fluvio_smartmodule::{smartmodule, Result, Record, RecordData};

pub fn aggregate(accumulator: RecordData, current: &Record) -> Result<RecordData> {
    let mut acc = String::from_utf8(accumulator.as_ref().to_vec())?;
    let next = std::str::from_utf8(current.value.as_ref())?;

This SmartModule reads each record as a string and appends it to the accumulator string.


ArrayMap functions are used to take one input record and create zero to many output records. This can be used to chop up input records that logically represent more than one data point and turn them into independent records. Below is an example where we take JSON arrays and convert them into a stream of the inner JSON objects.

use fluvio_smartmodule::{smartmodule, Result, Record, RecordData};

pub fn array_map(record: &Record) -> Result<Vec<(Option<RecordData>, RecordData)>> {
    // Read the input record as a JSON array
    let array = serde_json::from_slice::<Vec<serde_json::Value>>(record.value.as_ref())?;
    // Convert each individual value from the array into its own JSON string
    let strings = array
        .map(|value| serde_json::to_string(&value))
        .collect::<core::result::Result<Vec<String>, _>>()?;
    // Return a list of records to be flattened into the output stream
    let kvs = strings
        .map(|s| (None, RecordData::from(s)))


This project is licensed under the Apache license.


Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in Fluvio by you, shall be licensed as Apache, without any additional terms or conditions.


~81K SLoC