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 🙏

© 2025 – Pkg Stats / Ryan Hefner

@nextorders/queue

v0.4.2

Published

Designed to simplify working with RabbitMQ in Node.js applications. Provides a high-level API for organizing message queues and processing business entities.

Readme

Queue 🐰

npm version

The @nextorders/queue is a TypeScript library designed to simplify working with RabbitMQ in Node.js applications. The library provides a high-level API for organizing message queues and processing business entities.

RabbitMQ Dashboard

😨 Key Features

  • Type-safe operations with message queues
  • Automatic connection to the RabbitMQ server
  • Declarative creation of queues and exchanges
  • Built-in error handling and retry mechanisms
  • Support for various message types
  • Flexible connection configuration

📦 Installation

You can install the library via npm:

npm install @nextorders/queue

🚀 Usage

1. Define Event Types

Create type definitions for your events:

import type { BaseEventMap, BaseEventMessage, BaseEventMessageHandlerMap } from '@nextorders/queue'

export enum Events {
  UserCreated = 'userCreated',
  EmailSent = 'emailSent',
}

type EventMessage = UserCreated | EmailSent
type EventMap = BaseEventMap<EventMessage>

export type EventHandlerMap = Partial<BaseEventMessageHandlerMap<EventMap>>

type UserCreatedData = {
  id: string
  name: string
  email: string
}
export interface UserCreated extends BaseEventMessage<UserCreatedData> {
  event: typeof Events.UserCreated
}

type EmailSentData = {
  email: string
}
export interface EmailSent extends BaseEventMessage<EmailSentData> {
  event: typeof Events.EmailSent
}

2. Create Entities

Define entities that represent your services:

import { Entity, Repository } from '@nextorders/queue'
import { Events } from './types'

export class User extends Entity {
  constructor(repository: Repository) {
    super({
      name: 'user',
      eventsToConsume: [],
      repository,
    })
  }
}

export class Email extends Entity {
  constructor(repository: Repository) {
    super({
      name: 'email',
      eventsToConsume: [Events.UserCreated],
      repository,
    })
  }
}

3. Create Repository

Create a repository that manages your entities:

import type { EventMessage } from './types'
import { Repository } from '@nextorders/queue'
import { Email, User } from './entities'

class QueueRepository extends Repository {
  user: User = new User(this)
  email: Email = new Email(this)
}

export const repository = new QueueRepository()

4. Connect to RabbitMQ

On service start, connect to your RabbitMQ instance:

import { repository } from './repository'

await repository.connect('amqp://guest:guest@localhost:5672')

5. Publish Events

Create and publish events from your services:

await repository.publish<UserCreated>(Events.UserCreated, {
  id: newUser.id,
  name: newUser.name,
  email: newUser.email,
})

6. Consume Events

Subscribe to events and handle them:

import type { EmailSent, EventHandlerMap, UserCreated } from '../repository/types'
import { repository } from '../repository'
import { Events } from '../repository/types'

// Subscribe to Events and handle them
repository.consume<EventHandlerMap>(repository.email.name, {
  userCreated: handleUserCreated,
})

// Define event handlers
async function handleUserCreated(data: UserCreated['data']): Promise<boolean> {
  try {
    await sendEmail(data.email)
    return true
  } catch (error) {
    console.error('Error handling UserCreated event:', error)
    return false
  }
}

async function sendEmail(email: string): Promise<void> {
  console.warn('Sending email to:', email)

  // Publish Event for other services
  await repository.publish<EmailSent>(Events.EmailSent, {
    email,
  })
}

💁‍♂️ Example: Microservices Architecture

Check out the examples/microservices directory for a complete working example with:

  • Service 1: User creation service
  • Service 2: Email notification service
  • Shared repository with entities
  • Type-safe event definitions

🤝 License

This project is licensed under the MIT License.