{"id":15775,"library":"rabbitmq-client","title":"RabbitMQ Client for Node.js","description":"rabbitmq-client is a robust and typed Node.js client library for RabbitMQ (AMQP 0-9-1), designed as an alternative to `amqplib`. It is currently in version 5.0.8, actively maintained with regular updates and bug fixes as indicated by recent commits and version bumps. Key differentiators include automatic re-connection, re-subscription, and message retry mechanisms, offering higher resilience out of the box. It provides a higher-level API through `Consumer` and `Publisher` abstractions, simplifying common use cases, alongside a lower-level `Connection` for direct AMQP operations and an `RPCClient` for request-response patterns. The library is written in TypeScript, ships with comprehensive type definitions, and explicitly avoids external dependencies, contributing to a smaller footprint and potentially fewer supply chain risks. Performance is comparable to `amqplib`, as demonstrated by included benchmarks.","status":"active","version":"5.0.8","language":"javascript","source_language":"en","source_url":"https://github.com/cody-greene/node-rabbitmq-client","tags":["javascript","amqp","rabbitmq","reconnect","0-9-1","typescript"],"install":[{"cmd":"npm install rabbitmq-client","lang":"bash","label":"npm"},{"cmd":"yarn add rabbitmq-client","lang":"bash","label":"yarn"},{"cmd":"pnpm add rabbitmq-client","lang":"bash","label":"pnpm"}],"dependencies":[],"imports":[{"note":"The library primarily uses ES modules. While CommonJS might work with transpilation, direct require() is not the idiomatic approach for current Node.js versions.","wrong":"const { Connection } = require('rabbitmq-client')","symbol":"Connection","correct":"import { Connection } from 'rabbitmq-client'"},{"note":"Consumer is a type returned by `rabbit.createConsumer()`, not a class to be instantiated directly. The value is an instance created by the connection. For type-only imports, use `type Consumer`.","wrong":"import { createConsumer } from 'rabbitmq-client'","symbol":"Consumer","correct":"import { Connection, type Consumer } from 'rabbitmq-client'"},{"note":"Publisher is a type returned by `rabbit.createPublisher()`, not a class to be instantiated directly. Similar to Consumer, it's an instance created by the connection. Use `type Publisher` for type-only imports.","wrong":"import { createPublisher } from 'rabbitmq-client'","symbol":"Publisher","correct":"import { Connection, type Publisher } from 'rabbitmq-client'"}],"quickstart":{"code":"import { Connection } from 'rabbitmq-client'\n\n// Initialize: Connect to RabbitMQ\nconst rabbit = new Connection('amqp://guest:guest@localhost:5672')\nrabbit.on('error', (err) => {\n  console.error('RabbitMQ connection error:', err)\n})\nrabbit.on('connection', () => {\n  console.log('Connection successfully (re)established to RabbitMQ')\n})\n\nasync function setupAndRun() {\n  // Consume messages from a queue\n  const consumer = rabbit.createConsumer({\n    queue: 'user-events-queue',\n    queueOptions: { durable: true },\n    qos: { prefetchCount: 2 }, // Handle 2 messages concurrently\n    exchanges: [{ exchange: 'my-events-exchange', type: 'topic' }],\n    queueBindings: [{ exchange: 'my-events-exchange', routingKey: 'users.*' }]\n  }, async (msg) => {\n    try {\n      console.log('Received message:', msg.body)\n      // Process message here. Auto-acknowledges on success.\n      // Throws error to nack and potentially requeue or dead-letter.\n      await new Promise(resolve => setTimeout(resolve, 50)); // Simulate async work\n    } catch (e) {\n      console.error('Error processing message:', e)\n      // Returning a specific status can control nack/requeue behavior\n      return 3; // Nack, don't requeue\n    }\n  })\n\n  consumer.on('error', (err) => {\n    console.error('Consumer error (user-events-queue):', err)\n  })\n\n  // Declare a publisher\n  const publisher = rabbit.createPublisher({\n    confirm: true, // Enable publish confirmations\n    maxAttempts: 2, // Enable retries on publish failure\n    exchanges: [{ exchange: 'my-events-exchange', type: 'topic' }]\n  })\n\n  // Publish a message to an exchange\n  try {\n    await publisher.send(\n      { exchange: 'my-events-exchange', routingKey: 'users.visit' },\n      { id: Date.now(), name: 'Alice', action: 'visit' }\n    )\n    console.log('Published user.visit message.')\n  } catch (e) {\n    console.error('Failed to publish message:', e)\n  }\n\n  // Publish directly to a queue (less common with exchanges)\n  try {\n    await publisher.send(\n      { queue: 'direct-queue' },\n      { message: 'This goes directly to a queue' }\n    )\n    console.log('Published direct message to queue.')\n  } catch (e) {\n    console.error('Failed to publish direct message:', e)\n  }\n\n  // Keep the process alive for a bit to receive messages\n  // setTimeout(() => {\n  //   consumer.close()\n  //   rabbit.close()\n  //   console.log('Closed consumer and connection.')\n  // }, 10000)\n}\n\nsetupAndRun();","lang":"typescript","description":"This quickstart demonstrates how to establish a connection to RabbitMQ, set up a consumer to process messages from a queue, and create a publisher to send messages to an exchange or directly to a queue, including error handling and retry mechanisms."},"warnings":[{"fix":"Upgrade `rabbitmq-client` to version 5.0.3 or higher using `npm install rabbitmq-client@latest` or `yarn add rabbitmq-client@latest`.","message":"To connect to RabbitMQ version 4.1.x or higher, you must use version 5.0.3 or higher of this library. Older versions of `rabbitmq-client` will not be compatible.","severity":"breaking","affected_versions":"<5.0.3"},{"fix":"Ensure your Node.js environment meets the `>=16` engine requirement and provides necessary global objects/APIs if you're deploying to an unconventional runtime.","message":"The library explicitly states it has 'No dependencies'. While this is generally positive for a small footprint, it means users are responsible for handling any environment-specific requirements or polyfills if running in non-standard Node.js environments.","severity":"gotcha","affected_versions":">=1.0.0"},{"fix":"Review consumer callback logic to ensure correct error handling and consider returning integer status codes (e.g., `return 3` for Nack without requeue) for fine-grained control over message disposition in failure scenarios.","message":"Message acknowledgment behavior is automatic. If a consumer callback resolves successfully, the message is acknowledged (`BasicAck`). If it throws an error, the message is rejected (`BasicNack`) and potentially requeued or sent to a dead-letter exchange, depending on configuration. Explicit control requires returning a specific status code from the callback.","severity":"gotcha","affected_versions":">=1.0.0"}],"env_vars":null,"search_vec":"'-1':21,148 '-9':20,147 '0':19,146 '5.0.8':33 'abstract':82 'activ':34 'alongsid':87 'altern':25 'amqp':18,95,143 'amqplib':27,136 'api':77 'automat':53 'avoid':118 'benchmark':141 'box':70 'bug':40 'bump':49 'case':86 'chain':130 'client':2,7,14 'commit':46 'common':84 'compar':134 'comprehens':113 'connect':56,92 'consum':79 'contribut':121 'current':30 'definit':115 'demonstr':138 'depend':120 'design':22 'differenti':51 'direct':94 'explicit':117 'extern':119 'fewer':128 'fix':41 'footprint':125 'higher':65,75 'higher-level':74 'includ':52,140 'indic':43 'javascript':142 'key':50 'level':76,91 'librari':15,106 'lower':90 'lower-level':89 'maintain':35 'mechan':63 'messag':61 'node.js':4,13 'offer':64 'oper':96 'pattern':104 'perform':132 'potenti':127 'provid':72 'publish':81 'rabbitmq':1,6,17,144 'rabbitmq-cli':5 're':55,58 're-connect':54 're-subscript':57 'recent':45 'reconnect':145 'regular':37 'request':102 'request-respons':101 'resili':66 'respons':103 'retri':62 'risk':131 'robust':10 'rpcclient':99 'ship':111 'simplifi':83 'smaller':124 'subscript':59 'suppli':129 'type':12,114 'typescript':110,149 'updat':38 'use':85 'version':32,48 'written':108","created_at":"2026-04-21T17:59:57.026751+00:00","updated_at":"2026-04-21T17:59:57.026751+00:00","problems":[{"fix":"Ensure RabbitMQ is running and accessible from the machine where your application is hosted. Verify the connection string (e.g., `amqp://guest:guest@localhost:5672`) for correct host, port, and credentials. Check firewall rules if RabbitMQ is on a remote server.","cause":"The RabbitMQ server is not running or is not accessible at the specified host and port.","error":"Error: connect ECONNREFUSED 127.0.0.1:5672"},{"fix":"Ensure `import { Connection } from 'rabbitmq-client'` is correct and `const rabbit = new Connection('amqp://...')` has been executed successfully before calling `rabbit.createConsumer()` or `rabbit.createPublisher()`.","cause":"The `rabbit` variable is not a valid `Connection` instance or has not been properly initialized, or the import path is incorrect.","error":"TypeError: rabbit.createConsumer is not a function"},{"fix":"Either delete the conflicting queue in RabbitMQ management UI/cli, or ensure your `queueOptions` match the existing queue's configuration. This often happens when changing queue properties like `durable: true` to `durable: false` or vice-versa.","cause":"Attempting to declare a queue with parameters (e.g., `durable`, `autoDelete`) that conflict with an existing queue of the same name.","error":"Channel closed due to error: PRECONDITION_FAILED - inequivalent arg 'durable' for queue 'my-queue' in vhost '/'"}],"ecosystem":"npm","meta_description":null,"install_score":null,"quickstart_score":null,"quickstart_tag":null,"pypi_latest":"2.4.0","cli_name":"","cli_version":null,"type":"library","homepage":null,"github":"https://github.com/cody-greene/node-rabbitmq-client","docs":null,"changelog":null,"pypi":null,"npm":"https://www.npmjs.com/package/rabbitmq-client","openapi_spec":null,"status_page":null,"smithery":null,"categories":["database","communication"],"base_url":null,"auth_type":null,"provenance":{"verified_status":null,"verified_at":null,"last_verified":"2026-06-17","next_check":"2026-07-20","install_tag":null}}