{"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.","language":"javascript","status":"active","last_verified":"Tue Apr 21","install":{"commands":["npm install rabbitmq-client"],"cli":null},"imports":["import { Connection } from 'rabbitmq-client'","import { Connection, type Consumer } from 'rabbitmq-client'","import { Connection, type Publisher } from 'rabbitmq-client'"],"auth":{"required":false,"env_vars":[]},"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.","tag":null,"tag_description":null,"last_tested":null,"results":[]},"compatibility":null}