-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmain.go
53 lines (44 loc) · 1.13 KB
/
main.go
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
package main
import (
"context"
"fmt"
"os"
"time"
"github.com/redis/go-redis/v9"
"goa.design/pulse/streaming"
"goa.design/pulse/streaming/options"
)
func main() {
// Create Redis client
rdb := redis.NewClient(&redis.Options{Addr: "localhost:6379", Password: os.Getenv("REDIS_PASSWORD")})
ctx := context.Background()
if err := rdb.Ping(ctx).Err(); err != nil {
panic(err)
}
// Create stream
stream, err := streaming.NewStream("sr-stream", rdb)
if err != nil {
panic(err)
}
// Don't forget to destroy the stream when done
defer stream.Destroy(ctx)
// Add a new event
id, err := stream.Add(ctx, "event", []byte("payload"))
if err != nil {
panic(err)
}
fmt.Printf("event id: %s\n", id)
// Create reader that reads from the beginning and waits for
// events for up to 100ms
reader, err := stream.NewReader(ctx,
options.WithReaderStartAtOldest(),
options.WithReaderBlockDuration(100*time.Millisecond))
if err != nil {
panic(err)
}
// Don't forget to close the reader when done
defer reader.Close()
// Consume event
ev := <-reader.Subscribe()
fmt.Printf("event: %s, payload: %s\n", ev.EventName, ev.Payload)
}