...

Source file src/github.com/jackc/pgx/v5/examples/chat/main.go

Documentation: github.com/jackc/pgx/v5/examples/chat

     1  package main
     2  
     3  import (
     4  	"bufio"
     5  	"context"
     6  	"fmt"
     7  	"os"
     8  
     9  	"github.com/jackc/pgx/v5/pgxpool"
    10  )
    11  
    12  var pool *pgxpool.Pool
    13  
    14  func main() {
    15  	var err error
    16  	pool, err = pgxpool.New(context.Background(), os.Getenv("DATABASE_URL"))
    17  	if err != nil {
    18  		fmt.Fprintln(os.Stderr, "Unable to connect to database:", err)
    19  		os.Exit(1)
    20  	}
    21  
    22  	go listen()
    23  
    24  	fmt.Println(`Type a message and press enter.
    25  
    26  This message should appear in any other chat instances connected to the same
    27  database.
    28  
    29  Type "exit" to quit.`)
    30  
    31  	scanner := bufio.NewScanner(os.Stdin)
    32  	for scanner.Scan() {
    33  		msg := scanner.Text()
    34  		if msg == "exit" {
    35  			os.Exit(0)
    36  		}
    37  
    38  		_, err = pool.Exec(context.Background(), "select pg_notify('chat', $1)", msg)
    39  		if err != nil {
    40  			fmt.Fprintln(os.Stderr, "Error sending notification:", err)
    41  			os.Exit(1)
    42  		}
    43  	}
    44  	if err := scanner.Err(); err != nil {
    45  		fmt.Fprintln(os.Stderr, "Error scanning from stdin:", err)
    46  		os.Exit(1)
    47  	}
    48  }
    49  
    50  func listen() {
    51  	conn, err := pool.Acquire(context.Background())
    52  	if err != nil {
    53  		fmt.Fprintln(os.Stderr, "Error acquiring connection:", err)
    54  		os.Exit(1)
    55  	}
    56  	defer conn.Release()
    57  
    58  	_, err = conn.Exec(context.Background(), "listen chat")
    59  	if err != nil {
    60  		fmt.Fprintln(os.Stderr, "Error listening to chat channel:", err)
    61  		os.Exit(1)
    62  	}
    63  
    64  	for {
    65  		notification, err := conn.Conn().WaitForNotification(context.Background())
    66  		if err != nil {
    67  			fmt.Fprintln(os.Stderr, "Error waiting for notification:", err)
    68  			os.Exit(1)
    69  		}
    70  
    71  		fmt.Println("PID:", notification.PID, "Channel:", notification.Channel, "Payload:", notification.Payload)
    72  	}
    73  }
    74  

View as plain text