PgChannelListener

Manages PostgreSQL LISTEN/UNLISTEN subscriptions on a dedicated database connection.

Each instance holds a single dedicated connection that lives for the duration of the listener's lifecycle. Use close (or kotlin.io.use) to release the connection when done.

Typical usage:

db.createChannelListener().use { listener ->
listener.listen("orders", "notifications")
listener.notifications()
.collect { notification ->
println("Received on ${notification.channel}: ${notification.payload}")
}
}

Functions

Link copied to clipboard
abstract fun close()
Link copied to clipboard
abstract fun listen(vararg channels: String): DataResult<Unit>

Subscribes to the given channels. Executes LISTEN for each channel.

Link copied to clipboard
abstract fun notifications(): Flow<PgNotification>

Returns a cold Flow of PgNotification objects received on subscribed channels.

Link copied to clipboard
abstract fun unlisten(vararg channels: String): DataResult<Unit>

Unsubscribes from the given channels. Executes UNLISTEN for each channel.

Link copied to clipboard
abstract fun unlistenAll(): DataResult<Unit>

Unsubscribes from all currently subscribed channels. Executes UNLISTEN *.