Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package main
- import (
- "context"
- "fmt"
- "time"
- "github.com/jackc/pgx/v5/pgxpool"
- )
- const PSQL_HOST = "postgresql://root:root@mysrv.dev/test"
- func main() {
- ctx := context.Background()
- dbconfig, err := pgxpool.ParseConfig(PSQL_HOST)
- if err != nil {
- panic(err)
- }
- pool, err := pgxpool.NewWithConfig(context.Background(), dbconfig)
- if err != nil {
- panic(err)
- }
- // defer pool.Close()
- go func() {
- _, err := pool.Exec(ctx, "listen test")
- if err != nil {
- panic(err)
- }
- conn, err := pool.Acquire(ctx)
- if err != nil {
- panic(err)
- }
- fmt.Println("Listening for notifications")
- notification, err := conn.Conn().WaitForNotification(ctx)
- if err != nil {
- panic("Error waiting for notification: " + err.Error())
- }
- for notification != nil {
- fmt.Println("Received notification:", notification)
- }
- }()
- pool.Exec(ctx, "CREATE TABLE IF NOT EXISTS test (key TEXT PRIMARY KEY, value TEXT)")
- pool.Exec(ctx, "INSERT INTO test (key, value) VALUES ('key', 'value')")
- go func() {
- ticker := time.NewTicker(5 * time.Second)
- for range ticker.C {
- pool.Exec(ctx, "NOTIFY test")
- }
- }()
- for {
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement