2.11.1 • Published 5 months ago

haredo v2.11.1

Weekly downloads
288
License
MIT
Repository
github
Last release
5 months ago

Haredo

Haredo version 3 introduces breaking changes. See 3.0 Changes

npm npm Build Status Libraries.io dependency status for latest release

RabbitMQ client for Node.js with a focus on simplicity and type safety.

Table of Contents

Features

  • TypeScript
  • Chaining based API
  • Graceful closing
  • Automatic setup of queues and exchanges
  • Automatic acking / nacking based on the promise returned from the subscriber

Usage

Working examples are available on github

Initializing

import { Haredo } from 'haredo';
const haredo = Haredo({
    url: 'amqp://localhost:5672/'
});

Listening for messages

example on GitHub

haredo.queue('my-queue')
    .bindExchange('testExchange', '#', 'topic', { durable: false }) // Can be omitted if you don't want to bind the queue to an exchange right now
    .subscribe(async (message) => {
        console.log(message.data);
    });

Publishing to an exchange

example on GitHub

haredo.exchange('my-exchange').publish({ id: 5, status: 'active' }, 'item.created');

Publishing to a queue

example on GitHub

haredo.queue('my-queue').publish({ id: 5, status: 'inactive' });

Limit concurrency

haredo.queue('my-queue')
    .prefetch(5) // same as .concurrency(5)
    .subscribe(async (message) => {
        console.log(message);
    });

Delayed messages

Note: this requires RabbitMQ Delayed Message Plugin to be installed and enabled on the server.

example on GitHub

interface Message {.exchange
    id: number;
}
const delayedExchange = Exchange<Message>('my-delayed-exchange', 'x-delayed-message').delayed('topic');
await haredo.queue('my-queue')
    .bindExchange(delayedExchange, '#')
    .subscribe((data, { timestamp }) => {
        console.log(`Received message in ${ Date.now() - timestamp }ms id:${ data.id } `);
    });
let id = 0;
while (true) {
    id += 1;
    console.log('Publishing message', id);
    const msg = delayedMessage.json({ id }).timestamp(Date.now());
    await haredo
        .exchange(delayedExchange)
        .delay(1000)
        .publish(msg);
    await delay(2000);
}

Quorum queues with delivery limits

Node: requires RabbitMQ 3.8.0 or higher, see Quorum Queues Overview for more information.

example on GitHub

Message throttling

example on GitHub

await haredo.queue('my-queue')
    .backoff(standardBackoff({
        failThreshold: 3,
        failSpan: 5000,
        failTimeout: 5000
    }))
    .subscribe(() => {
        throw new Error('Nack this message for me')
    });

Dead letter

View on GitHub

Middleware

example on GitHub

import { Middleware } from 'haredo';

const timeMessage: Middleware = ({ queue }, next) => {
    const start = Date.now();
    await next();
    console.log(`Message took ${ Date.now() - start }ms`);
}

await haredo.queue('my-queue')
    .use(timeMessage)
    .subscribe(() => {
        throw new Error('Nack this message for me')
    });

Graceful shutdown

Calling consumer.close() will send cancel to channel and wait for existing messages to be handled before resolving the returned promise.

Calling haredoInstance.close() will gracefully close all of it's consumers

Automatic setup

By default Haredo will automatically assert the queus and exchanges and bind them to each other. This can be disabled by calling .skipSetup()

await haredo.queue('my-queue')
    .skipSetup()
    .subscribe(() => {
        throw new Error('Nack this message for me');
    });

// Only create the queue, don't bind it to any exchanges and don't create any exchanges
await haredo.queue('my-queue')
    .bindExchange('testExchange', '#', 'topic', { durable: false })
    .skipSetup({ skipBoundExchanges: true, skipBindings: true, skipCreate: false });

Extending Haredo

Add new methods to the Haredo instance

example on GitHub

declare module 'haredo/types' {
    interface QueueChain<T> {
        cid(cid: string): this;
    }
}

const haredo = Haredo({
    url: 'amqp://localhost:5672/'
    extensions: [
        {
            name: 'cid',
            queue: (state) => {
                return (cid: string) => ({
                    ...state,
                    headers: {
                        ...state.headers,
                        'x-cid': cid
                    }
                });
            }
        }
    ]
});

await haredo.queue('my-queue')
    .cid('123')
    .publish({ id: 5, status: 'inactive' });

Global middleware

Add a middleware that will be called for every message in every subscriber

example on GitHub

declare module 'haredo/types' {
    interface HaredoMessage<T> {
        cid?: string;
    }
}
const haredo = Haredo({
    url: 'amqp://localhost:5672/'
    globalMiddleware: [
        (message) => {
            message.cid = message.headers?.['x-cid'] as string;
        }
    ]
});
3.0.0-alpha.1

6 months ago

3.0.0-alpha.3

6 months ago

3.0.0-alpha.2

6 months ago

3.0.0-alpha.4

5 months ago

2.11.1

12 months ago

2.11.0

1 year ago

2.10.0

2 years ago

2.8.0

2 years ago

2.9.0

2 years ago

2.7.4

2 years ago

2.7.3

2 years ago

2.7.2

3 years ago

2.6.3

3 years ago

2.6.2

3 years ago

2.7.0

3 years ago

2.7.1

3 years ago

2.6.1

3 years ago

2.6.0

3 years ago

2.5.0

4 years ago

2.4.0

4 years ago

2.3.1

4 years ago

2.3.0

4 years ago

2.2.3

4 years ago

2.2.2

4 years ago

2.2.1

4 years ago

2.2.0

4 years ago

2.1.2

4 years ago

2.1.1

4 years ago

2.1.0

4 years ago

2.0.2

4 years ago

2.0.1

4 years ago

2.0.0-beta.0

4 years ago

2.0.0-alpha.6

4 years ago

2.0.0-alpha.5

4 years ago

2.0.0-alpha.4

4 years ago

2.0.0-alpha.3

4 years ago

2.0.0-alpha.2

4 years ago

1.3.2

4 years ago

2.0.0-alpha.1

4 years ago

2.0.0-alpha.0

4 years ago

1.3.1

4 years ago

1.3.0

5 years ago

1.2.6

5 years ago

1.2.5

5 years ago

1.2.4

5 years ago

1.2.3

5 years ago

1.2.2

5 years ago

1.2.1

5 years ago

1.2.0

5 years ago

1.1.1

5 years ago

1.1.0

5 years ago

1.0.0

5 years ago

0.4.2

5 years ago

0.4.1

5 years ago

0.4.0

5 years ago

0.3.2

5 years ago

0.3.1

5 years ago

0.3.0

5 years ago

0.2.2

5 years ago

0.2.1

5 years ago

0.2.0

5 years ago

0.1.4

5 years ago

0.1.3

5 years ago

0.1.2

5 years ago

0.1.1

5 years ago

0.1.0

5 years ago