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

amqp-middlewares

v0.0.6

Published

amqp-middlewares is a toolset to create message processors for AMQP message brokers. It exports a `MessagingPipeline`, which allows the combination of message processors to filter, manipulate, consume from, and publish to AMQP queues. `amqp-middlewares` a

Downloads

2

Readme

amqp-middlewares

amqp-middlewares is a toolset to create message processors for AMQP message brokers. It exports a MessagingPipeline, which allows the combination of message processors to filter, manipulate, consume from, and publish to AMQP queues. amqp-middlewares also exports a MessageProcessor interface, which you can implement to create custom message processors to fulfill other needs.

Installation

You can install amqp-middlewares with npm:

npm install amqp-middlewares

Getting started

After installing the package, you can import the tools to create a pipeline and process messages.

Including a timestamp header and publishing a message

The following example will show you how to use the messaging pipeline to achieve two things: add a "timestamp" header to a message and publish it to the "sample" exchange with the "operation" routing key.


import { Message, MessagingPipeline, PublisherMessageProcessor, AmqpClientFactory } from 'amqp-middlewares';

...
// Define a basic config for the AmqpClient
const config = {
  host: 'my-amqp-host',
  port: 5672,
  username: 'my-username',
  password: 'my-password',
  vhost: '/'
}

// Then, create an AmqpClient using the factory
const amqpClient = await AmqpClientFactory.GetClientInstance(config);

// Build your messaging pipeline. We'll use two processors for this pipeline
const pipeline = new MessagingPipeline([
  new TimestamperMessageProcessor(),
  new PublisherMessageProcessor(amqpClient, 'sample', 'operation')
]);

/*
  We can then create our message and run it through the pipeline. The message 
  processors are executed in the order that they are defined.
*/

const message = new Message('Hello, world!');
await pipeline.run(message);

/*
  At this point, we can expect the message to have been published to the
  "sample" exchange with the "operation" routing key. The 
  TimestamperMessageProcessor should also have added a "timestamp" header to
  the message.
*/

Creating a custom message processor

amqp-middlewares exports a MessageProcessor interface, which can be used to add custom functionality to a messaging pipeline. To create a custom message processor, all you need to do implement the MessageProcessor interface in a new class and pass it along to the messaging pipeline.

The MessageProcessor interface defines a handle function, which receives a Message as the only parameter. The handle function returns a Promise, which must resolve to either of the two possible values:

  • A new Message, which will be passed along to the next processor in the pipeline;
  • null, which will stop the execution of the MessagingPipeline.

The following example shows how to create a message processor that adds a new timestamp field to a JSON message.

import { MessageProcessor, Message } from 'amqp-middlewares';

class JSONTimestamperMessageProcessor implements MessageProcessor {

  function handle(message: Message): Promise<Message | null> {
    const content = JSON.parse(message.payload);

    content['timestamp'] = new Date.getTime();
    const newPayload = JSON.stringify(content);

    return Promise.resolve(new Message(newPayload, message.headers));
  }
}

You can now create a pipeline with the new message processor, as such:

const pipeline = new MessagingPipeline([
  new JSONTimestamperMessageProcessor()
]);