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

@simple-discovery/amqp

v3.0.0

Published

Service discovery over AMQP 0-9-1 (RabbitMQ) fanout exchanges with the simple-discovery signed envelope

Readme

@simple-discovery/amqp

Discovery qua AMQP 0-9-1 (RabbitMQ): mọi process kết nối tới cùng một broker publish thông tin của mình lên một fanout exchange và nhận thông tin của nhau qua một queue riêng. Hợp với hệ thống đã có RabbitMQ.

Gói tin giống hệt các transport khác của simple-discovery: msgpack, ký HMAC-SHA256, chống phát lại.

Cài đặt

bun add @simple-discovery/amqp rxjs

Chạy trên Node.js ≥ 18 và Bun. Dùng amqplib v2.

Ví dụ

import { AmqpDiscovery } from '@simple-discovery/amqp'

type Worker = { host: string; httpPort: number }

const discovery = new AmqpDiscovery<Worker>({
  url: 'amqp://user:[email protected]',
  namespace: 'shop',
  tags: ['worker'],
  node_id: 'worker-1',
  key: process.env.SIMPLE_DISCOVERY_KEY!,
})

discovery.subscribe(message => console.log(message.node_id, message.data))

await discovery.broadcast({
  node_id: 'worker-1',
  namespace: 'shop',
  tags: ['worker'],
  version: '1',
  created_at: Date.now(),
  seq: 1,
  data: { host: '10.0.0.5', httpPort: 3000 },
})

discovery.close()

Nội dung message, namespace, tags, node_id và vòng đời (status$, close()) giống @simple-discovery/udp.

Tuỳ chọn

| Tuỳ chọn | Biến môi trường | Mặc định | Ý nghĩa | | --- | --- | --- | --- | | namespace, tags, node_id | – | (bắt buộc: namespace, tags) | Như mọi transport. | | key | SIMPLE_DISCOVERY_KEY | 'simple-discovery' | Khoá ký gói. Luôn đặt khoá riêng. | | url | SIMPLE_DISCOVERY_AMQP_URL | amqp://127.0.0.1 | Broker. Bỏ qua khi có connection. | | socketOptions | – | – | Tuỳ chọn socket của amqplib (TLS...). | | recovery | – | thử lại vô hạn, 100 ms → 30 s | Backoff khi kết nối lại (initialDelay, maxDelay, maxRetries...). | | connection | – | – | Dùng một kết nối amqplib có sẵn, xem bên dưới. | | exchange | – | simple-discovery.<namespace> | Fanout exchange. | | packetTtlMs | – | 30000 | Bỏ gói lệch giờ quá mức này. |

SIMPLE_DISCOVERY_AMQP_DEBUG=1 in lỗi kết nối ra stderr.

Dùng kết nối có sẵn

import { connect } from 'amqplib'
import { AmqpDiscovery } from '@simple-discovery/amqp'

const connection = await connect(process.env.AMQP_URL!)
const discovery = new AmqpDiscovery<{ host: string }>({ connection, namespace: 'shop', tags: ['worker'] })

Discovery mở channel riêng trên kết nối này và close() chỉ đóng channel đó. Discovery không tự kết nối lại một kết nối bạn truyền vào: nếu channel bị đóng, stream báo lỗi. Muốn tự kết nối lại, truyền url thay vì connection.

Cách hoạt động

  • Discovery khai báo fanout exchange simple-discovery.<namespace> (non-durable) và một queue riêng cho mỗi node (server đặt tên, exclusive, autoDelete). Queue tự biến mất khi node ngắt kết nối.
  • Node vào sau biết ngay các node đã có nhờ cờ hello ở lần broadcast đầu: mọi node đang có mặt trả lời đúng một lần.
  • Mất kết nối: dùng cơ chế recovery của amqplib 2, thử lại vô hạn theo backoff. Sau mỗi lần kết nối lại, discovery khai báo lại exchange/queue rồi broadcast kèm hello. Nếu chỉ channel bị đóng mà kết nối còn sống, discovery đóng kết nối để amqplib dựng lại từ đầu.
  • Broker chưa lên khi khởi động: discovery cứ thử lại và ở trạng thái not_ready; broadcast() đợi tới khi kết nối được. Đặt recovery.maxRetries nếu muốn dừng và báo lỗi sau một số lần.
  • Trong lúc đang kết nối lại, broadcast() bị bỏ qua; lần broadcast sau khi có kết nối sẽ đi.
  • Không có tin rời đi: ứng dụng tự broadcast định kỳ và coi node im lặng quá lâu là đã rời đi.

Chạy ở production

  • User RabbitMQ cần quyền configure (tạo exchange/queue), writeread trên vhost dùng cho discovery.
  • Mọi process dùng chung key, namespace và cùng vhost.
  • Đồng bộ giờ các máy (NTP): gói lệch quá packetTtlMs bị bỏ im lặng.