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 🙏

© 2026 – Pkg Stats / Ryan Hefner

@platformatic/leader

v0.2.0

Published

PostgreSQL advisory lock-based leader election with notification channels

Readme

@platformatic/leader

PostgreSQL advisory lock-based leader election with notification channels.

Ensures only one instance in a cluster acts as the leader, using PostgreSQL advisory locks for distributed coordination and LISTEN/NOTIFY for real-time event routing.

Install

npm install @platformatic/leader

Usage

Election only

'use strict'

const pg = require('pg')
const createLeaderElector = require('@platformatic/leader')

const pool = new pg.Pool({
  connectionString: 'postgres://localhost/mydb'
})

const leader = createLeaderElector({
  pool,
  lock: 4242,
  onLeadershipChange: (isLeader) => {
    if (isLeader) {
      console.log('I am the leader — starting work')
    } else {
      console.log('No longer the leader — stopping work')
    }
  }
})

leader.start()

console.log(leader.isLeader()) // true or false

await leader.stop()
await pool.end()

With notification channels

'use strict'

const pg = require('pg')
const createLeaderElector = require('@platformatic/leader')

const pool = new pg.Pool({
  connectionString: 'postgres://localhost/mydb'
})

const leader = createLeaderElector({
  pool,
  lock: 4242,
  poll: 10000,
  channels: [
    {
      channel: 'my_events',
      onNotification: (payload) => {
        console.log('Received:', payload)
      }
    }
  ],
  onLeadershipChange: (isLeader) => {
    console.log('Leadership changed:', isLeader)
  }
})

leader.start()

// Send a notification to the leader
await leader.notify({ action: 'refresh' }, 'my_events')

// Check if this instance is the leader
console.log(leader.isLeader())

// Stop the leader elector
await leader.stop()
await pool.end()

API

createLeaderElector(options)

Returns an object with { start, stop, notify, isLeader }.

Options

| Option | Type | Required | Default | Description | |---|---|---|---|---| | pool | pg.Pool | Yes | - | A node-postgres pool instance | | lock | number | Yes | - | PostgreSQL advisory lock ID | | poll | number | No | 10000 | Polling interval in milliseconds | | channels | Array | No | - | Notification channel configurations (omit for election-only mode) | | log | object | No | pino() | Logger with info, debug, warn, error methods | | onLeadershipChange | function | No | null | Callback invoked with (isLeader: boolean) when leadership status changes |

Each channel in the channels array must have:

  • channel (string) - PostgreSQL notification channel name
  • onNotification (function) - Handler called with the parsed payload when a notification arrives

Methods

  • start() - Begins the leader election loop. Returns a promise.
  • stop() - Stops the leader election loop and releases resources.
  • notify(payload, channelName) - Sends a notification on the given channel. The payload is JSON-serialized.
  • isLeader() - Returns true if this instance is currently the leader.

How It Works

  1. Each instance periodically attempts to acquire a PostgreSQL advisory lock using pg_try_advisory_lock.
  2. The instance that acquires the lock becomes the leader and starts listening for notifications via LISTEN.
  3. Only the leader processes incoming notifications and routes them to the appropriate channel handler.
  4. If the leader stops or loses the lock, another instance acquires it and takes over.
  5. The onLeadershipChange callback fires whenever the leadership status transitions.

Development

Start PostgreSQL:

docker compose up -d

Run tests:

node --test test/*.test.js

License

Apache-2.0