Security News
Fluent Assertions Faces Backlash After Abandoning Open Source Licensing
Fluent Assertions is facing backlash after dropping the Apache license for a commercial model, leaving users blindsided and questioning contributor rights.
PostgreSQL can act as a simple message broker. Send notifications with an arbitrary payload from one database client to other clients, using NOTIFY
and subscribe to notifications using LISTEN
.
Works with plain JavaScript and TypeScript 3.
In one sentence: Because none of the existing packages was working reliably in production.
Using the NOTIFY
and LISTEN
features is not trivial using node-postgres
(pg
), since you cannot use connection pools and even distinct client connections also tend to time out.
There are already a few packages out there, like pg-pubsub
, but neither of them seems to work reliably. Errors are being swallowed, the code is hard to reason about, there is no type-safety, ...
This package aims to fix those shortcomings. Postgres LISTEN & NOTIFY in node that finally works.
# using npm:
npm install pg-listen
# using yarn
yarn add pg-listen:
import createPostgresSubscriber from "pg-listen"
import { databaseURL } from "./config"
const subscriber = createPostgresSubscriber({ connectionString: databaseURL })
subscriber.notifications.on("my-channel", (payload) => {
console.log("Received notification in 'my-channel':", payload)
})
subscriber.events.on("error", (error) => {
// Usually triggered if reconnection attempts have failed repeatedly
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 sendMessage (payload) {
await subscriber.notify("my-channel", payload)
}
See dist/index.d.ts.
instance.events.on("error", listener: (error: Error) => void)
An error
event is emitted for fatal errors that affect the notification subscription. A standard way of handling those kinds of errors would be to console.error()
-log the error and terminate the process with a non-zero exit code.
instance.events.on("notification", listener: ({ channel, payload }) => void)
Emitted whenever a notification is received. You must have subscribed to that channel before using instance.listenTo()
in order to receive notifications.
A more convenient way of subscribing to notifications is the instance.notifications
event emitter.
instance.events.on("reconnect", listener: (attempt: number) => void)
Emitted when a connection issue has been detected and an attempt to re-connect to the database is started.
instance.notifications.on(channelName: string, listener: (payload: any) => void)
The convenient way of subscribing to notifications. Don't forget to call .listenTo(channelName)
to subscribe the Postgres client to this channel in order to receive notifications.
Set the DEBUG
environment variable to pg-listen:*
to enable debug logging.
MIT
FAQs
PostgreSQL LISTEN & NOTIFY that finally works.
The npm package pg-listen receives a total of 28,155 weekly downloads. As such, pg-listen popularity was classified as popular.
We found that pg-listen demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Fluent Assertions is facing backlash after dropping the Apache license for a commercial model, leaving users blindsided and questioning contributor rights.
Research
Security News
Socket researchers uncover the risks of a malicious Python package targeting Discord developers.
Security News
The UK is proposing a bold ban on ransomware payments by public entities to disrupt cybercrime, protect critical services, and lead global cybersecurity efforts.