1.7.0 • Published 2 years ago
pg-listen-pid v1.7.0
A fork of pg-listen that exposes the processId from eventemitter.
Installation
# using npm:
npm install pg-listen-pidUsage
import createSubscriber from "pg-listen-pid"
import { databaseURL } from "./config"
// Accepts the same connection config object that the "pg" package would take
const subscriber = createSubscriber({ connectionString: databaseURL })
subscriber.notifications.on("my-channel", (payload, processId) => {
  // Payload as passed to subscriber.notify() (see below)
  console.log("Received notification in 'my-channel':", payload, "with PID:", processId)
})
subscriber.events.on("error", (error) => {
  console.error("Fatal database connection error:", error)
  process.exit(1)
})
process.on("exit", () => {
  subscriber.close()
})
export async function connect () {
  await subscriber.connect()
  await subscriber.listenTo("my-channel")
}
export async function sendSampleMessage () {
  await subscriber.notify("my-channel", {
    greeting: "Hey, buddy.",
    timestamp: Date.now()
  })
}For more details see pg-listen.
License
MIT
1.7.0
2 years ago