Write a NATS subscriber plug-in
You need to install a NATS server: https://docs.nats.io/running-a-nats-service/introduction/installation
Build
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):
Output
| π message: {"id":"natscli","subject":"news","data":"Hello World"}
|