All Projects β†’ andywer β†’ Pg Listen

andywer / Pg Listen

Licence: mit
πŸ“‘ PostgreSQL LISTEN & NOTIFY for node.js that finally works.

Programming Languages

typescript
32286 projects

Projects that are alternatives of or similar to Pg Listen

Samples
Community driven repository for Dapr samples
Stars: ✭ 104 (-70.11%)
Mutual labels:  events, pubsub
evon
Fast and versatile event dispatcher code generator for Golang
Stars: ✭ 15 (-95.69%)
Mutual labels:  events, pubsub
Rocketman
πŸš€ Rocketman help build event-based/pub-sub code in Ruby
Stars: ✭ 139 (-60.06%)
Mutual labels:  events, pubsub
Twitchlib
C# Twitch Chat, Whisper, API and PubSub Library. Allows for chatting, whispering, stream event subscription and channel/account modification. Supports .NET Core 2.0
Stars: ✭ 519 (+49.14%)
Mutual labels:  events, pubsub
pg-pubsub
Reliable PostgreSQL LISTEN/NOTIFY with inter-process lock support
Stars: ✭ 50 (-85.63%)
Mutual labels:  events, pubsub
Aquameta
Web development platform built entirely in PostgreSQL
Stars: ✭ 987 (+183.62%)
Mutual labels:  events, postgresql
micro-typed-events
The smallest, most convenient typesafe TS event emitter you'll ever need
Stars: ✭ 39 (-88.79%)
Mutual labels:  events, pubsub
Flare
Flare is a service that notify changes of HTTP endpoints
Stars: ✭ 110 (-68.39%)
Mutual labels:  notifications, pubsub
dry-events
Pub/sub system
Stars: ✭ 102 (-70.69%)
Mutual labels:  events, pubsub
dead-simple
πŸ’€πŸ’‘ Dead simple PubSub and EventEmitter in JavaScript
Stars: ✭ 21 (-93.97%)
Mutual labels:  events, pubsub
Sysend.js
Send messages between open pages or tabs in same browser
Stars: ✭ 347 (-0.29%)
Mutual labels:  events, notifications
watermill-http
HTTP Pub/Sub for the Watermill project.
Stars: ✭ 16 (-95.4%)
Mutual labels:  events, pubsub
Crypto Coin Alerts
An application that let you set alerts for the prices of several cryptocurrencies
Stars: ✭ 72 (-79.31%)
Mutual labels:  postgresql, notifications
Eventd
A simple daemon to track remote or local events and do actions the user wants to
Stars: ✭ 43 (-87.64%)
Mutual labels:  events, notifications
Postgresql2websocket
Send PostgreSQL notifications over websockets
Stars: ✭ 58 (-83.33%)
Mutual labels:  postgresql, notifications
Go Sdk
Dapr SDK for go
Stars: ✭ 149 (-57.18%)
Mutual labels:  events, pubsub
Sobjectizer
An implementation of Actor, Publish-Subscribe, and CSP models in one rather small C++ framework. With performance, quality, and stability proved by years in the production.
Stars: ✭ 172 (-50.57%)
Mutual labels:  pubsub, message-passing
angular-PubSub
Angular 1.x implementation of the Publish–Subscribe pattern.
Stars: ✭ 32 (-90.8%)
Mutual labels:  events, pubsub
watermill-amqp
AMQP Pub/Sub for the Watermill project.
Stars: ✭ 27 (-92.24%)
Mutual labels:  events, pubsub
Wisper
A micro library providing Ruby objects with Publish-Subscribe capabilities
Stars: ✭ 3,014 (+766.09%)
Mutual labels:  events, pubsub

pg-listen

Postgres LISTEN & NOTIFY that works

Build Status NPM Version


PostgreSQL can act as a message broker: Send notifications with arbitrary payloads from one database client to others.

Works with node.js 8+ and plain JavaScript or TypeScript 3. Uses the Postgres NOTIFY statement and subscribes to notifications using LISTEN.

Features

    πŸ“‘  Send and subscribe to messages

    β³  Continuous connection health checks

    β™»οΈ  Reconnects automatically

    β—️  Proper error handling

    πŸ‘Œ  Type-safe API


Installation

# using npm:
npm install pg-listen

# using yarn:
yarn add pg-listen

Usage

import createSubscriber from "pg-listen"
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) => {
  // Payload as passed to subscriber.notify() (see below)
  console.log("Received notification in 'my-channel':", payload)
})

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()
  })
}

API

For details see dist/index.d.ts.

Error & event handling

instance.events.on("connected", listener: () => void)

The connected event is emitted once after initially establishing the connection and later once after every successful reconnect. Reconnects happen automatically when pg-listen detects that the connection closed or became unresponsive.

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.

This error event is usually emitted after multiple attempts to reconnect have failed.

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.

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) directly, 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

Set the DEBUG environment variable to pg-listen:* to enable debug logging.

License

MIT

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].