Comparing version 0.1.4 to 1.0.0-with-cjs-hack
{ | ||
"name": "pg-listen", | ||
"version": "0.1.4", | ||
"version": "1.0.0-with-cjs-hack", | ||
"license": "MIT", | ||
@@ -8,3 +8,3 @@ "description": "PostgreSQL LISTEN & NOTIFY that finally works.", | ||
"repository": "github:andywer/pg-listen", | ||
"main": "./dist/index.js", | ||
"main": "./index.js", | ||
"typings": "./dist/index.d.ts", | ||
@@ -49,3 +49,4 @@ "scripts": { | ||
"files": [ | ||
"dist/**" | ||
"dist/**", | ||
"index.js" | ||
], | ||
@@ -52,0 +53,0 @@ "ava": { |
@@ -5,22 +5,19 @@ # pg-listen - Postgres LISTEN & NOTIFY that works | ||
PostgreSQL can act as a simple message broker. Send notifications with an arbitrary payload from one database client to other clients, using [`NOTIFY`](https://www.postgresql.org/docs/10/static/sql-notify.html) and subscribe to notifications using [`LISTEN`](https://www.postgresql.org/docs/10/static/sql-listen.html). | ||
PostgreSQL can act as a message broker: Send notifications with arbitrary payloads from one database client to others. | ||
Works with plain JavaScript and TypeScript 3. | ||
Works with node.js 8+ and plain JavaScript or TypeScript 3. Uses the Postgres [`NOTIFY`](https://www.postgresql.org/docs/10/static/sql-notify.html) statement and subscribes to notifications using [`LISTEN`](https://www.postgresql.org/docs/10/static/sql-listen.html). | ||
- Send notifications and subscribe to them | ||
- Continuous connection health checking | ||
- Customizable auto-reconnecting | ||
- Proper error handling | ||
- Type-safe code (TypeScript 3.0) | ||
### Features | ||
📡 Send and subscribe to messages | ||
## Why another package? | ||
⏳ Continuous connection health checks | ||
In one sentence: Because none of the existing packages was working reliably in production. | ||
♻️ Reconnects automatically | ||
Using the `NOTIFY` and `LISTEN` features is not trivial using [`node-postgres` (`pg`)](https://www.npmjs.com/package/pg), since you cannot use connection pools and even distinct client connections also tend to time out. | ||
❗️ Proper error handling | ||
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, ... | ||
👌 Type-safe API | ||
This package aims to fix those shortcomings. Postgres LISTEN & NOTIFY in node that finally works. | ||
--- | ||
@@ -34,4 +31,4 @@ | ||
# using yarn | ||
yarn add pg-listen: | ||
# using yarn: | ||
yarn add pg-listen | ||
``` | ||
@@ -43,8 +40,10 @@ | ||
```js | ||
import createPostgresSubscriber from "pg-listen" | ||
import createSubscriber from "pg-listen" | ||
import { databaseURL } from "./config" | ||
const subscriber = createPostgresSubscriber({ connectionString: databaseURL }) | ||
// Accepts the same connection config object that the "pg" package would take | ||
const subscriber = createSubscriber({ connectionString: databaseURL }) | ||
subscriber.notifications.on("my-channel", (payload) => { | ||
// Payload as passed to subscriber.notify() (see below) | ||
console.log("Received notification in 'my-channel':", payload) | ||
@@ -54,3 +53,2 @@ }) | ||
subscriber.events.on("error", (error) => { | ||
// Usually triggered if reconnection attempts have failed repeatedly | ||
console.error("Fatal database connection error:", error) | ||
@@ -69,4 +67,7 @@ process.exit(1) | ||
export async function sendMessage (payload) { | ||
await subscriber.notify("my-channel", payload) | ||
export async function sendSampleMessage () { | ||
await subscriber.notify({ | ||
greeting: "Hey, buddy.", | ||
timestamp: Date.now() | ||
}) | ||
} | ||
@@ -78,3 +79,3 @@ ``` | ||
See [dist/index.d.ts](./dist/index.d.ts). | ||
For details see [dist/index.d.ts](./dist/index.d.ts). | ||
@@ -88,2 +89,4 @@ | ||
This `error` event is usually emitted after multiple attempts to reconnect have failed. | ||
### `instance.events.on("notification", listener: ({ channel, payload }) => void)` | ||
@@ -104,2 +107,13 @@ | ||
## Why another package? | ||
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`)](https://www.npmjs.com/package/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. | ||
## Debugging | ||
@@ -106,0 +120,0 @@ |
23857
8
374
119