npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

postgres-ipc

v1.3.1

Published

PG-Client wrapper for making NOTIFY/LISTEN/UNLISTEN queries. Exposed as async functions or as EventEmitter (continuation of pg-ipc).

Downloads

34

Readme

postgres-ipc

PG-Client wrapper for making NOTIFY/LISTEN/UNLISTEN queries. Exposed as async functions and as an EventEmitter (continuation of pg-ipc).

See more about Postgres notifications here: https://www.postgresql.org/docs/15/libpq-notify.html

Features

  • easy switch from abandoned pg-ipc project
  • seamless EventEmitter interface with typings
  • async methods for more controlled access
  • auto reconnect without console spam (you wont even notice a disconnect occurred - well as long you don't try and query the client while it's disconnected)
  • auto encode/decode JSON to send complex data structures

Example 1: It's just an EventEmitter?

import PostgresIPCClient from 'postgres-ipc'
// const PostgresIPCClient = require('postgres-ipc')

// Same constructor as Client form pg but defaults to process.env.POSTGRES_CONN_URI
const ipc = new PostgresIPCClient(process.env.POSTGRES_CONN_URI)

// You can add listeners before connecting.
ipc.on("channel-name", (notification) => {
    if (notification.processId === ipc.processId) return // Ignore notifications sent by this client like this.
    console.log(notification.payload.message)
})
// Once connected it will auto query 'LISTEN channel-name' since you added this listener here.

// Haven't connected yet? No problem... This NOTIFY query will automatically be sent once you connect.
ipc.emit("channel-name", { message: "hello" })

// Basically the same thing but this is awaitable, will return the error (not throw it) 
// and already logs the error to console.error for you.
ipc.notify("channel-name", { message: "hello" })

// This is a deprecated alias for notify bcs pg-ipc had this.
ipc.send("channel-name", { message: "hello" })

// Remember to connect ;)
ipc.connect().then(async () => {
    // once destroyed you can't connect again
    await ipc.destroy()
    // also feel free to call this as many times as you feel fit
    await ipc.destroy()
})

// You can call connect as many times as your heart desires.
ipc.connect() 

Example 2: Let me await that

import PostgresIPCClient from 'postgres-ipc'
// const PostgresIPCClient = require('postgres-ipc')

const ipc = new PostgresIPCClient()
ipc.connect().then(async () => {
    // Otherwise blocked channels like error and debug can be listened for like this
    await ipc.listen("channel1", "error")
    await ipc.listen(["channel2", "debug", "end"])

    // and captured like this
    ipc.on("notification", (notification) => {
        if (notification.channel === "error") console.log("Notification on error channel!", notification.payload)
    })
    
    await ipc.unlisten("channel2", "debug", "end")
    await ipc.unlisten(["channel1", "error"])
    await ipc.unlisten() // (Unlisten to all)
    console.log(ipc.channels()) // These are the channels you are currently listening for.

    await ipc.notify(
        "IMPORTANT",
        "Remember this won't throw an error, it will return an error that has already been logged."
    )
    await ipc.destroy()
})

Take a look at these events

ipc.on('notification', (notification) => console.log("Notification from any channel!", notification))
ipc.on('notify', (channel, payload) => console.log("A PG NOTIFY query was successfully made!", channel, payload))
ipc.on('debug', (message) => console.log("See exactly what's happening:", message))
// console.error is already added as a default error handler. You can overwrite it though by adding your own listener:
ipc.on('error', (err) => console.log("One of your listeners threw an error:", err))
ipc.on('end', () => console.log("I was once connected but I got destroyed :("))

Install

npm install postgres-ipc