Skip to content

Write a NATS publisher plug-in

We are going to use it to publish message to the NATS subscriber

  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
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
package main

import (
    "errors"

    "github.com/extism/go-pdk"
    "github.com/valyala/fastjson"
)

//export hostPrintln
func hostPrintln(offset uint64) uint64

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

//export hostGetEnv
func hostGetEnv(offset uint64) uint64

func GetEnv(name string) string {
    // copy the name of the environment variable to the shared memory
    variableName := pdk.AllocateString(name)
    // call the host function
    offset := hostGetEnv(variableName.Offset())

    // read the value of the result from the shared memory
    variableValue := pdk.FindMemory(offset)
    buffer := make([]byte, variableValue.Length())
    variableValue.Load(buffer)

    // cast the buffer to string and return the value
    envVarValue := string(buffer)
    return envVarValue
}

var parser = fastjson.Parser{}

//export hostInitNatsConnection
func hostInitNatsConnection(offset uint64) uint64

func InitNatsConnection(natsConnectionId string, natsUrl string) (string, error) {
    jsonStrArguments := `{"id":"` + natsConnectionId + `","url":"` + natsUrl + `"}`
    // Copy the string value to the shared memory
    arguments := pdk.AllocateString(jsonStrArguments)

    // Call the host function with Json string argument
    offset := hostInitNatsConnection(arguments.Offset())

    // Get result from the shared memory
    // The host function (hostInitNatsConnection) returns a JSON buffer:
    // {
    //   "success": "the NATS connexion id",
    //   "failure": "error message if error, else empty"
    // }
    memoryResult := pdk.FindMemory(offset)
    buffer := make([]byte, memoryResult.Length())
    memoryResult.Load(buffer)

    JSONData, err := parser.ParseBytes(buffer)
    if err != nil {
        return "", err
    }
    if len(JSONData.GetStringBytes("failure")) == 0 {
        return string(JSONData.GetStringBytes("success")), nil
    } else {
        return "", errors.New(string(JSONData.GetStringBytes("failure")))
    }

}


//export hostNatsPublish
func hostNatsPublish(offset uint64) uint64

func NatsPublish(natsServer string, subject string, data string) (string, error) {
    // Prepare the arguments for the host function
    // with a JSON string:
    // {
    //    "id": "id of the NATS client",
    //    "subject": "name",
    //    "data": "Bob Morane"
    // }
    //jsonStr := `{"url":"` + natsServer + `","subject":"` + subject + `","data":"` + data + `"}`
    jsonStr := `{"id":"` + natsServer + `","subject":"` + subject + `","data":"` + data + `"}`

    // Copy the string value to the shared memory
    arguments := pdk.AllocateString(jsonStr)

    // Call host function with the offset of the arguments
    offset := hostNatsPublish(arguments.Offset())

    // Get result from the shared memory
    // The host function (hostNatsPublish) returns a JSON buffer:
    // {
    //   "success": "the message",
    //   "failure": "error message if error, else empty"
    // }
    memoryResult := pdk.FindMemory(offset)
    buffResult := make([]byte, memoryResult.Length())
    memoryResult.Load(buffResult)

    JSONData, err := parser.ParseBytes(buffResult)

    if err != nil {
        return "", err
    }
    if len(JSONData.GetStringBytes("failure")) == 0 {
        return string(JSONData.GetStringBytes("success")), nil
    } else {
        return "", errors.New(string(JSONData.GetStringBytes("failure")))
    }
}

//export publish
func publish() uint64 {
    input := pdk.Input()

    natsURL := GetEnv("NATS_URL")
    Println("πŸ’œ NATS_URL: " + natsURL)
    idNatsConnection, errInit := InitNatsConnection("natsconn01", natsURL)
    if errInit != nil {
        Println("😑 " + errInit.Error())
    } else {
        Println("πŸ™‚ " + idNatsConnection)
    }

    res, err := NatsPublish("natsconn01", "news", string(input))

    if err != nil {
        Println("😑 " + err.Error())
    } else {
        Println("πŸ™‚ " + res)
    }
    return 0
}

func main() {}
  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
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
use extism_pdk::*;
use serde::{Serialize, Deserialize};
use thiserror::Error;

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) };
}

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

pub fn get_env(name: String) -> String {
    // copy the name of the environment variable to the shared memory
    let mut variable_name: Memory = extism_pdk::Memory::new(name.len());
    variable_name.store(name);

    // call the host function
    let offset: u64 = unsafe { hostGetEnv(variable_name.offset) };

    // read the value of the result from the shared memory
    let variable_value: Memory = extism_pdk::Memory::find(offset).unwrap();

    // return the value
    return variable_value.to_string().unwrap()
}

#[derive(Serialize, Deserialize, Debug)]
struct NatsConfig {
    pub id: String,
    pub url: String,
}

#[derive(Serialize, Deserialize, Debug)]
struct NatsMessage {
    pub id: String,
    pub subject: String,
    pub data: String,
}

#[derive(Serialize, Deserialize, Debug)]
struct StringResult {
    pub success: String,
    pub failure: String,
}

#[derive(Error, Debug)]
pub enum NatsError {
    #[error("Nats Connection issue")]
    ConnectionFailure,
    #[error("Store issue")]
    MessageFailure,
    #[error("Not found")]
    NotFound,
}

extern "C" {
    fn hostInitNatsConnection(offset: u64) -> u64;
}

pub fn init_nats_connection(nats_connection_id: String, nats_url: String) -> Result<String, Error> {
    // Prepare the arguments for the host function
    // with a JSON string:
    // {
    //    "id": "id of the NATS connection",
    //    "url": "URL of the NATS server"
    // }
    let args = NatsConfig {
        id: nats_connection_id,
        url: nats_url,
    };
    let json_str: String = serde_json::to_string(&args).unwrap();

    // Copy the string value to the shared memory
    let mut memory_json_str: Memory = extism_pdk::Memory::new(json_str.len());
    memory_json_str.store(json_str);

    // Call host function with the offset of the arguments
    let offset: u64 = unsafe { hostInitNatsConnection(memory_json_str.offset) };

    // Get result from the shared memory
    // The host function (hostInitNatsConnection) returns a JSON buffer:
    // {
    //   "success": "id of the connection",
    //   "failure": "error message if error, else empty"
    // }
    let memory_result: Memory = extism_pdk::Memory::find(offset).unwrap();

    let json_string:String = memory_result.to_string().unwrap();
    let result: StringResult = serde_json::from_str(&json_string).unwrap();

    if result.failure.is_empty()  {
        return Ok(result.success);
    } else {
        return Err(NatsError::ConnectionFailure.into());
    }
}

extern "C" {
    fn hostNatsPublish(offset: u64) -> u64;
}

pub fn nats_publish(nats_connection_id: String, subject: String, data: String) -> Result<String, Error> {
    // Prepare the arguments for the host function
    // with a JSON string:
    // {
    //    "id": "id of the NATS client",
    //    "subject": "name",
    //    "data": "Bob Morane"
    // }
    let args = NatsMessage {
        id: nats_connection_id, 
        subject: subject,
        data: data,
    };
    let json_str: String = serde_json::to_string(&args).unwrap();

    // Copy the string value to the shared memory
    let mut memory_json_str: Memory = extism_pdk::Memory::new(json_str.len());
    memory_json_str.store(json_str);

    // Call host function with the offset of the arguments
    let offset: u64 = unsafe { hostNatsPublish(memory_json_str.offset) };

    // Get result from the shared memory
    // The host function returns a JSON buffer:
    // {
    //   "success": "OK",
    //   "failure": "error message if error, else empty"
    // }
    let memory_result: Memory = extism_pdk::Memory::find(offset).unwrap();

    let json_string:String = memory_result.to_string().unwrap();
    let result: StringResult = serde_json::from_str(&json_string).unwrap();

    if result.failure.is_empty()  {
        return Ok(result.success);
    } else {
        return Err(NatsError::MessageFailure.into());
    }


}

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

    let nats_url : String = get_env("NATS_URL".to_string());
    let nats_connection : Result<String, Error> = init_nats_connection("natsconn01".to_string(), nats_url);

    match nats_connection {
        Ok(value) => println("πŸ¦€ nats connection: ".to_string() + &value),
        Err(error) => println("😑 error: ".to_string() + &error.to_string()),
    }

    match nats_publish("natsconn01".to_string(), "news".to_string(), input.to_string()) {
        Ok(value)  => println("πŸ¦€ πŸ™‚ ".to_string() + &value),
        Err(error) => println("😑 error: ".to_string() + &error.to_string()),
    }

    Ok(0)
}

Build

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

Run the plug-in to publish a message

1
2
3
4
export NATS_URL="nats://0.0.0.0:4222"
./slingshot run --wasm=./natspub.wasm \
--handler=publish \
--input="I πŸ’œ Wasm ✨"

On the subscriber side, you shoul read:

1
πŸ‘‹ message: {"subject":"news","data":"I πŸ’œ Wasm ✨"}