Skip to main content

@rtorcato/api-amqp v1.0.0

Interfaces

AmqpChannel

Defined in: index.ts:14

Properties

connection

connection: ChannelModel

Defined in: index.ts:15

channel

channel: Channel

Defined in: index.ts:16


PublisherOptions

Defined in: index.ts:29

Properties

exchange

exchange: string

Defined in: index.ts:30

exchangeType?

optional exchangeType?: "direct" | "topic" | "fanout" | "headers"

Defined in: index.ts:32

Exchange type. Default: 'topic'.

durable?

optional durable?: boolean

Defined in: index.ts:34

Survive a broker restart. Default: true.


ConsumerOptions

Defined in: index.ts:61

Properties

queue

queue: string

Defined in: index.ts:62

durable?

optional durable?: boolean

Defined in: index.ts:64

Survive a broker restart. Default: true.

prefetch?

optional prefetch?: number

Defined in: index.ts:66

Max unacknowledged messages in flight. Default: 10.

requeueOnError?

optional requeueOnError?: boolean

Defined in: index.ts:68

Requeue a message when the handler throws (risks a poison-message loop). Default: false.

Type Aliases

AmqpConnection

AmqpConnection = Awaited<ReturnType<typeof amqp.connect>>

Defined in: index.ts:12

An open connection + channel. Close the channel then the connection when done.

Functions

connect()

connect(url, socketOptions?): Promise<AmqpChannel>

Defined in: index.ts:20

Open a connection and a channel to the broker (e.g. amqp://localhost).

Parameters

url

string

socketOptions?

any

Returns

Promise<AmqpChannel>


createPublisher()

createPublisher<T>(channel, options): Promise<(routingKey, message, publishOptions?) => boolean>

Defined in: index.ts:46

Assert the exchange and return a typed publish function that JSON-encodes messages.

Type Parameters

T

T = unknown

Parameters

channel

Channel

options

PublisherOptions

Returns

Promise<(routingKey, message, publishOptions?) => boolean>

Example

const publish = await createPublisher(channel, { exchange: 'orders' })
publish('order.created', { id: '1', total: 42 })

createConsumer()

createConsumer<T>(channel, options, handler): Promise<void>

Defined in: index.ts:82

Assert the queue and consume JSON messages. The message is acked when the handler resolves, and nacked when it throws (requeued only if requeueOnError).

Type Parameters

T

T = unknown

Parameters

channel

Channel

options

ConsumerOptions

handler

(message, raw) => void | Promise<void>

Returns

Promise<void>

Example

await createConsumer<OrderCreated>(channel, { queue: 'orders' }, async (order) => {
await process(order)
})