Skip to content

Write a Redis subscriber plug-in

 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() {}

Build

1
2
3
4
#!/bin/bash
tinygo build -scheduler=none --no-debug \
    -o redissub.wasm \
    -target wasi main.go

Run

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

./slingshot redis subscribe \
--wasm=./redissub.wasm \
--handler=message \
--uri=${REDIS_URI} \
--client-id=pubsubcli \
--channel=news

# Output:
🌍 redis URI      : *****
🌍 redis Client Id: pubsubcli
πŸš€ handler        : message
πŸ“¦ wasm           : ./redissub.wasm
πŸ“Ί channel        : news

Trigger the plugin

Connect to the Redis server:

1
2
#!/bin/bash
redis-cli -u  ${REDIS_URI}

Publish message(s):

1
redis.aivencloud.com:17170> PUBLISH news "Hello World"

Output

1
πŸ‘‹ message: {"id":"pubsubcli","channel":"news","payload":"Hello World"}