Menu

1 / 5

Dead letters in Node.js: amqplib and NestJS on RabbitMQ

DEV Community: node·Viktor·3 days ago
#376htxrG
#devtonodejs#devto#dev#message#orders
Long article (1594 words) — no AI summary yet.
Reading 0:00
15s threshold

Originally published at warrenops.io .

In Node the failure modes of a RabbitMQ consumer are quieter than elsewhere. An async handler that throws does not crash anything: the promise rejects, nobody awaits it, and the message stays unacknowledged until the channel closes. NestJS, with its defaults, acknowledges the message before your handler runs, so a failure simply loses it. And a nack with requeue puts the message straight back at the head of the queue. Here is how to make failed messages land in a dead-letter queue with amqplib and with NestJS, and how to send them back once the bug is fixed.

amqplib: catch it, then nack without requeue

const amqp = require ( 'amqplib' ) ;

const conn = await amqp . connect ( process . env . AMQP_URL ) ; const ch = await conn . createChannel ( ) ;

// the dead-letter route await ch . assertExchange ( 'orders.dlx' , 'direct' , { durable : true } ) ; await ch . assertQueue ( 'orders.dlq' , { durable : true } ) ; await ch .…

Continue reading — create a free account

Join HashtagPLUS to read full articles, follow hashtags, vote, and join the conversation.