Skip to content

Commit e24d08b

Browse files
committed
docs: add example
1 parent a63fd95 commit e24d08b

File tree

2 files changed

+71
-0
lines changed

2 files changed

+71
-0
lines changed

README.md

+37
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,43 @@
22

33
A generic pubsub implementation for Go.
44

5+
```go
6+
package main
7+
8+
import (
9+
"bytes"
10+
"context"
11+
"fmt"
12+
"log/slog"
13+
14+
"github.com/google/uuid"
15+
"github.com/picosh/pubsub"
16+
)
17+
18+
func main() {
19+
ctx := context.TODO()
20+
logger := slog.Default()
21+
broker := pubsub.NewMulticast(logger)
22+
23+
chann := []*pubsub.Channel{
24+
pubsub.NewChannel("my-topic"),
25+
}
26+
27+
go func() {
28+
writer := bytes.NewBufferString("my data")
29+
pubID := uuid.NewString()
30+
broker.Pub(ctx, pubID, writer, chann, false)
31+
}()
32+
33+
reader := bytes.NewBufferString("")
34+
subID := uuid.NewString()
35+
broker.Sub(ctx, subID, reader, chann, false)
36+
37+
// result
38+
fmt.Println("data from pub:", reader)
39+
}
40+
```
41+
542
## pubsub over ssh
643

744
The simplest pubsub system for everyday automation needs.

cmd/basic/main.go

+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package main
2+
3+
import (
4+
"bytes"
5+
"context"
6+
"fmt"
7+
"log/slog"
8+
9+
"github.com/google/uuid"
10+
"github.com/picosh/pubsub"
11+
)
12+
13+
func main() {
14+
ctx := context.TODO()
15+
logger := slog.Default()
16+
broker := pubsub.NewMulticast(logger)
17+
18+
chann := []*pubsub.Channel{
19+
pubsub.NewChannel("my-topic"),
20+
}
21+
22+
go func() {
23+
writer := bytes.NewBufferString("my data")
24+
pubID := uuid.NewString()
25+
_ = broker.Pub(ctx, pubID, writer, chann, false)
26+
}()
27+
28+
reader := bytes.NewBufferString("")
29+
subID := uuid.NewString()
30+
_ = broker.Sub(ctx, subID, reader, chann, false)
31+
32+
// result
33+
fmt.Println("data from pub:", reader)
34+
}

0 commit comments

Comments
 (0)