-
-
Notifications
You must be signed in to change notification settings - Fork 202
/
main.go
44 lines (35 loc) · 943 Bytes
/
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
package main
import (
"context"
"database/sql"
"fmt"
"time"
"github.com/uptrace/bun"
"github.com/uptrace/bun/dialect/pgdialect"
"github.com/uptrace/bun/driver/pgdriver"
"github.com/uptrace/bun/extra/bundebug"
)
func main() {
ctx := context.Background()
dsn := "postgres://postgres:@localhost:5432/postgres?sslmode=disable"
sqldb := sql.OpenDB(pgdriver.NewConnector(pgdriver.WithDSN(dsn)))
db := bun.NewDB(sqldb, pgdialect.New())
db.AddQueryHook(bundebug.NewQueryHook(bundebug.WithVerbose(true)))
ln := pgdriver.NewListener(db)
go func() {
for i := 0; i < 3; i++ {
payload := time.Now().Format(time.RFC3339)
if err := pgdriver.Notify(ctx, db, "mychan1", payload); err != nil {
panic(err)
}
time.Sleep(time.Second)
}
_ = ln.Close()
}()
if err := ln.Listen(ctx, "mychan1", "mychan2"); err != nil {
panic(err)
}
for notif := range ln.Channel() {
fmt.Println(notif.Channel, notif.Payload)
}
}