Become an expert in R — Interactive courses, Cheat Sheets, certificates and more!
Get Started for Free

postgresWaitForNotify

Wait for and return any notifications that return within timeout


Description

Once you subscribe to notifications with LISTEN, use this to wait for responses on each channel.

Usage

postgresWaitForNotify(conn, timeout = 1)

Arguments

conn

a PqConnection object, produced by DBI::dbConnect()

timeout

How long to wait, in seconds. Default 1

Value

If a notification was available, a list of:

channel

Name of channel

pid

PID of notifying server process

payload

Content of notification

If no notifications are available, return NULL

Examples

# For running the examples on systems without PostgreSQL connection:
if (postgresHasDefault()) {
    library(DBI)
    library(callr)

    # listen for messages on the grapevine
    db_listen <- dbConnect(RPostgres::Postgres())
    dbExecute(db_listen, "LISTEN grapevine")

    # Start another process, which sends a message after a delay
    rp <- r_bg(function () {
        library(DBI)
        Sys.sleep(0.3)
        db_notify <- dbConnect(RPostgres::Postgres())
        dbExecute(db_notify, "NOTIFY grapevine, 'psst'")
        dbDisconnect(db_notify)
    })

    # Sleep until we get the message
    n <- NULL
    while (is.null(n)) {
        n <- RPostgres::postgresWaitForNotify(db_listen, 60)
    }
    stopifnot(n$payload == 'psst')

    # Tidy up
    rp$wait()
    dbDisconnect(db_listen)
}

RPostgres

'Rcpp' Interface to 'PostgreSQL'

v1.3.2
GPL-3
Authors
Hadley Wickham [aut], Jeroen Ooms [aut], Kirill Müller [aut, cre] (<https://orcid.org/0000-0002-1416-3412>), RStudio [cph], R Consortium [fnd], Tomoaki Nishiyama [ctb] (Code for encoding vectors into strings derived from RPostgreSQL)
Initial release
2021-04-12

We don't support your browser anymore

Please choose more modern alternatives, such as Google Chrome or Mozilla Firefox.