{"id":21031288,"url":"https://github.com/workable/rabbit-queue","last_synced_at":"2025-04-07T05:13:30.399Z","repository":{"id":10331333,"uuid":"65309530","full_name":"Workable/rabbit-queue","owner":"Workable","description":"AMQP/RabbitMQ queue management library.","archived":false,"fork":false,"pushed_at":"2025-02-18T00:38:14.000Z","size":967,"stargazers_count":24,"open_issues_count":4,"forks_count":11,"subscribers_count":23,"default_branch":"master","last_synced_at":"2025-03-30T21:09:59.286Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"TypeScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/Workable.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2016-08-09T16:10:40.000Z","updated_at":"2025-01-22T07:24:18.000Z","dependencies_parsed_at":"2024-06-18T18:27:33.916Z","dependency_job_id":"1e664fde-e341-4c34-897c-e6920db8df39","html_url":"https://github.com/Workable/rabbit-queue","commit_stats":{"total_commits":165,"total_committers":13,"mean_commits":"12.692307692307692","dds":"0.21818181818181814","last_synced_commit":"206ab63852dbbe67ca40a63262d0478001cfa057"},"previous_names":[],"tags_count":49,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Workable%2Frabbit-queue","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Workable%2Frabbit-queue/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Workable%2Frabbit-queue/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Workable%2Frabbit-queue/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/Workable","download_url":"https://codeload.github.com/Workable/rabbit-queue/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":247595335,"owners_count":20963943,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-11-19T12:27:05.710Z","updated_at":"2025-04-07T05:13:30.381Z","avatar_url":"https://github.com/Workable.png","language":"TypeScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Rabbit Queue\n\n```\n   ,---.     |    |    o|        ,---.\n   |---',---.|---.|---..|---     |   |.   .,---..   .,---.\n   |  \\ ,---||   ||   |||        |   ||   ||---'|   ||---'\n   `   ``---^`---'`---'``---'    `---\\`---'`---'`---'`---'\n```\n\n## About\n\nThis module is based on [WRabbit](https://github.com/Workable/wrabbit)\nand [Jackrabbit](https://github.com/hunterloftis/jackrabbit).\n\nIt makes [RabbitMQ](http://www.rabbitmq.com/) management and integration easy. Provides an abstraction layer\nabove [amqplib](https://github.com/squaremo/amqp.node).\n\nIt is written in typescript and requires node v10.0.0 or higher.\nMost features will work with nodejs v6.0.0 and higher but using older versions than v10.0.0 is not recommended. RPC streams will not work in older versions.\n\nBy default it works well with jsons. Override properties.contentType to work with strings or binary data.\n\n## Connecting to RabbitMQ\n\n```javascript\nconst { Rabbit } = require('rabbit-queue');\nconst rabbit = new Rabbit(process.env.RABBIT_URL || 'amqp://localhost', {\n  prefetch: 1, //default prefetch from queue\n  replyPattern: true, //if reply pattern is enabled an exclusive queue is created\n  scheduledPublish: false,\n  prefix: '', //prefix all queues with an application name\n  socketOptions: {} // socketOptions will be passed as a second param to amqp.connect and from ther to the socket library (net or tls)\n});\n\nrabbit.on('connected', () =\u003e {\n  //create queues, add halders etc.\n  //this will be called on every reconnection too\n});\n\nrabbit.on('disconnected', (err = new Error('Rabbitmq Disconnected')) =\u003e {\n  //handle disconnections and try to reconnect\n  console.error(err);\n  setTimeout(() =\u003e rabbit.reconnect(), 100);\n});\n```\n\n## Usage\n\n```javascript\nconst { Rabbit } = require('rabbit-queue');\nconst rabbit = new Rabbit(process.env.RABBIT_URL || 'amqp://localhost', { scheduledPublish: true });\n\nrabbit\n  .createQueue('queueName', { durable: false }, (msg, ack) =\u003e {\n    console.log(msg.content.toString());\n    ack(null, 'response');\n  })\n  .then(() =\u003e console.log('queue created'));\n\nrabbit.publish('queueName', { test: 'data' }, { correlationId: '1' }).then(() =\u003e console.log('message published'));\n\n// Please note that a queue will be created for each different expiration used:  prefix_delay_expiration.\nrabbit\n  .publishWithDelay('queueName', { test: 'data' }, { correlationId: '1', expiration: '10000' })\n  .then(() =\u003e console.log('message will be published'));\n\nrabbit\n  .getReply('queueName', { test: 'data' }, { correlationId: '1' })\n  .then((reply) =\u003e console.log('received reply', reply));\n\nrabbit\n  .getReply('queueName', { test: 'data' }, { correlationId: '1' }, '', 100)\n  .then((reply) =\u003e console.log('received reply', reply))\n  .catch((error) =\u003e console.log('Timed out after 100ms'));\n\nrabbit.bindToTopic('queueName', 'routingKey');\nrabbit\n  .getTopicReply('routingKey', { test: 'data' }, { correlationId: '1' }, '', 100)\n  .then((reply) =\u003e console.log('received reply', reply))\n  .catch((error) =\u003e console.log('Timed out after 100ms'));\n```\n\n## Binding to topics\n\n```javascript\nconst { Rabbit } = require('rabbit-queue');\nconst rabbit = new Rabbit('amqp://localhost');\n\nrabbit\n  .createQueue('queueName', { durable: false }, (msg, ack) =\u003e {\n    console.log(msg.content.toString());\n    ack(null, 'response');\n  })\n  .then(() =\u003e console.log('queue created'));\n\nrabbit.bindToExchange('queueName', 'amq.topic', 'routingKey');\n//shortcut rabbit.bindToTopic('queueName', 'routingKey');\n\nrabbit\n  .publishExchange('amq.topic', 'routingKey', { test: 'data' }, { correlationId: '1' })\n  .then(() =\u003e console.log('message published'));\n//shortcut rabbit.publishTopic( 'routingKey', { test: 'data' }, { correlationId: '1' });\n```\n\n## Advanced Usage with BaseQueueHandler (will add to dlq after 3 failed retries)\n\n```javascript\nconst rabbit = new Rabbit('amqp://localhost');\nclass DemoHandler extends BaseQueueHandler {\n  handle({ msg, event, correlationId, startTime }) {\n    console.log('Received: ', event);\n    console.log('With correlation id: ' + correlationId);\n  }\n  afterDlq({ msg, event }) {\n    // Something to do after added to dlq\n  }\n}\n\nnew DemoHandler('demoQueue', rabbit, {\n  retries: 3,\n  retryDelay: 1000,\n  logEnabled: true, //log queue processing time\n  scope: 'SINGLETON', //can also be 'PROTOTYPE' to create a new instance every time\n  createAndSubscribeToQueue: true // used internally no need to overwriteÏÏ\n});\n\nrabbit.publish('demoQueue', { test: 'data' }, { correlationId: '4' });\n```\n\n### Get reply pattern implemented with a QueueHandler\n\n```javascript\nconst rabbit = new Rabbit(process.env.RABBIT_URL || 'amqp://localhost');\nclass DemoHandler extends BaseQueueHandler {\n  handle({ msg, event, correlationId, startTime }) {\n    console.log('Received: ', event);\n    console.log('With correlation id: ' + correlationId);\n    return Promise.resolve('reply'); // could be return 'reply';\n  }\n\n  afterDlq({ msg, event }) {\n    // Something to do after added to dlq\n  }\n}\n\nnew DemoHandler('demoQueue', rabbit, {\n  retries: 3,\n  retryDelay: 1000,\n  logEnabled: true,\n  scope: 'SINGLETON'\n});\n\nrabbit\n  .getReply('demoQueue', { test: 'data' }, { correlationId: '5' })\n  .then((reply) =\u003e console.log('received reply', reply));\n```\n\n### Example where handle throws an error\n\n```javascript\n  const rabbit = new Rabbit(process.env.RABBIT_URL || 'amqp://localhost');\n  class DemoHandler extends BaseQueueHandler {\n    handle({msg, event, correlationId, startTime}) {\n      return Promise.reject(new Error('test Error'));  // could be synchronous: throw new Error('test error');\n    }\n\n    afterDlq({msg, event}) {\n      console.log('added to dlq');\n    }\n  }\n\n  new DemoHandler('demoQueue', rabbit,\n    {\n      retries: 3,\n      retryDelay: 1,\n      logEnabled: true\n    });\n\n  rabbit.getReply('demoQueue', { test: 'data' }, { correlationId: '5' })\n    .then(reply =\u003e console.log('received reply', reply));\n    .catch(error =\u003e console.log('error', error)); //error will be 'test Error';\n```\n\n### Example with Stream rpc (works if both consumer and producer use rabbit-queue)\n\n```javascript\nconst rabbit = new Rabbit(process.env.RABBIT_URL || 'amqp://localhost');\nclass DemoHandler extends BaseQueueHandler {\n  handle({ msg, event, correlationId, startTime }) {\n    const stream = new Readable({ read() {} });\n    stream.push('streaming');\n    stream.push('events');\n    stream.push(null); //the end\n    return stream;\n  }\n}\n\nnew DemoHandler('demoQueue', rabbit, {\n  retries: 3,\n  retryDelay: 1,\n  logEnabled: true\n});\n\nconst reply = await rabbit.getReply('demoQueue', { test: 'data' }, { correlationId: '5' });\nfor await (const chunk of reply) {\n  console.log(`Received chunk: ${chunk.toString()}`);\n}\n```\n\n### Logging\n\nRabbit-queue logs to console by default.\nIt also emits events for each log so that you can use your own logger\n\neg:\n\n```javascript\nrabbit.on('log', (component, level, ...args) =\u003e console.log(`[${level}] ${component}`, ...args));\n```\n\n### Changelog\n\n### New in v5.4.x\n\n- Support for stream RPC to pass options for the Readable stream created in the caller.\n  e.g.\n\n```js\nconst reply = await rabbit.getReply(\n  'demoQueue',\n  { test: 'data' },\n  { headers: { test: 1, backpressure: true, options: { highWaterMark: 1 } }, correlationId: '1' }\n);\nfor await (const chunk of reply) {\n  console.log(`Received chunk: ${chunk.toString()}`);\n  if ('sufficient_data_received') {\n    reply.emit(Queue.STOP_STREAM);\n  }\n}\n```\n\n### v4.x.x to v5.x.x\n\n- Support for Node LTS v6 and v8 was dropped. You should use Node v10 and higher.\n- Two connections are created by default to rabbitMQ. One for consuming and one for producing messages.\n- Dependency to log4js was dropped. Console is used by default but you can easily plug your own logger.\n\n### New in v4.7.x\n\nWhen declaring queues with handlers you can define a prefetch count different from the global one\n\n```js\nrabbit.createQueue('queueName', { durable: false, prefetch: 100 }, (msg, ack) =\u003e {\n  console.log(msg.content.toString());\n  ack(null, 'response');\n});\n\n// or\n\nclass DemoHandler extends BaseQueueHandler {\n  // ...\n}\n\nnew DemoHandler('demoQueue', rabbit, { prefetch: 100 });\n```\n\nNote that the prefetch value is set to RabbitMQ and if you have other ways of creating consumers eg. by calling queue.subscribe directly you might end up with consumers being created with different prefetch from the global.\nThe two ways mentioned above are handled by rabbit-queue and they are **synchronized** so that no other queue consumer might be affected.\n\nAlso note that if you use rabbit prior to 3.3.0 the behavior might be different.\nSee https://www.rabbitmq.com/consumer-prefetch.html for more details\n\n### v4.2.x to \u003e v4.4.x\n\nRPC stream enhancement: When backpressure is enabled, the consumer can stop communication, when data received is sufficient\n\ne.g.\n\n```js\nconst reply = await rabbit.getReply(\n  'demoQueue',\n  { test: 'data' },\n  { headers: { test: 1, backpressure: true }, correlationId: '1' }\n);\nfor await (const chunk of reply) {\n  console.log(`Received chunk: ${chunk.toString()}`);\n  if ('sufficient_data_received') {\n    reply.emit(Queue.STOP_STREAM);\n  }\n}\n```\n\n### v4.x.x to \u003e v4.2.x\n\nGet reply as a stream supports two more optional headers inside properties:\n\nbackpressure (by default false),\ntimeout\n\neg:\n\n```js\nawait rabbit.getReply(\n  'demoQueue',\n  { test: 'data' },\n  { correlationId: '5', headers: { backpressure: true, timeout: 10000 } }\n);\n```\n\nIf used the rpc that responds to this request will stop sending messages until the receiving stream has consumed those messages or has buffered them (By default nodejs stream buffer for json streams is 16 objects). If this does not happen within the timeout the process will stop.\n\nUse this feature only if both requesting and receiving part have rabbit-queue \u003e 4.2.0\n\n### v3.x.x to v4.x.x\n\nCode was reorganized.\nRPC stream was introduced. Drops support for node older than v10.0.0 due to the usage of async generators\nSupports contentType. By default it is application/json for backwards compatibility.\n\n### v2.x.x to v3.x.x\n\nLog4js was removed. You can no longer pass your own logger in Rabbit constructor. Instead log4js-api is used and your log4js configuration is used by default if present. Logger name is rabbit-queue.\n\n#### v1.x.x to V2.x.x\n\nQueue subscribe 2ond param `ack` was updated. Now it accepts as 1st param an error and as a second the response. Rpc calls will throw an error if the first param is defined.\n\n## Tests\n\nThe tests are set up with Docker + Docker-Compose,\nso you don't need to install rabbitmq (or even node) to run them:\n\n`npm run test-docker`\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fworkable%2Frabbit-queue","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fworkable%2Frabbit-queue","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fworkable%2Frabbit-queue/lists"}