Skip to content

Write a NATS subscriber plug-in

You need to install a NATS server: https://docs.nats.io/running-a-nats-service/introduction/installation

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
package main

import (
    "github.com/extism/go-pdk"
)

//export hostPrintln
func hostPrintln(offset uint64) uint64

func Println(text string) {
    memoryText := pdk.AllocateString(text)
    hostPrintln(memoryText.Offset())
}

//export message
func message() uint64 {
    // read function argument from the memory
    input := pdk.Input()

    Println("πŸ‘‹ message: " + string(input))

    return 0
}

func main() {}
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
use extism_pdk::*;

extern "C" {
    fn hostPrintln(ptr: u64) -> u64;
}

pub fn println(text: String) {
    let mut memory_text: Memory = extism_pdk::Memory::new(text.len());
    memory_text.store(text);
    unsafe { hostPrintln(memory_text.offset) };
}

#[plugin_fn]
pub fn message(input: String) -> FnResult<u64> {

    println("πŸ‘‹ message: ".to_string() + &input);

    Ok(0)
}

Build

1
2
3
4
#!/bin/bash
tinygo build -scheduler=none --no-debug \
    -o natssub.wasm \
    -target wasi main.go
1
2
3
4
5
#!/bin/bash
cargo clean
cargo build --release --target wasm32-wasi
ls -lh ./target/wasm32-wasi/release/*.wasm
cp ./target/wasm32-wasi/release/*.wasm .

Run

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
#!/bin/bash

./slingshot nats subscribe \
--wasm=./natssub.wasm \
--handler=message \
--url=nats://0.0.0.0:4222 \
--connection-id=natsconn01 \
--subject=news

# Output:
🌍 NATS URL      : *****
🌍 NATS Connection Id: natsconn01
πŸš€ handler           : message
πŸ“¦ wasm              : ./natssub.wasm
πŸ“Ί Subject           : news

Trigger the plugin

You need a NATS client. It's easy to write one with Go:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
package main

import (
    "fmt"
    "github.com/nats-io/nats.go"
)

func main() {
    // Connect to a server
    //nc, err := nats.Connect("nats://0.0.0.0:4222")
    nc, err := nats.Connect(nats.DefaultURL)

    if err != nil {
        fmt.Printlnln(err.Error())
    }
    defer nc.Close()

    err = nc.Publish("news", []byte("Hello World"))

    if err != nil {
        fmt.Printlnln(err.Error())
    }
}

Publish message(s):

1
go run main.go

Output

1
πŸ‘‹ message: {"id":"natscli","subject":"news","data":"Hello World"}