{"id":18657781,"url":"https://github.com/lightsofapollo/amqpworkers","last_synced_at":"2025-11-05T22:30:21.632Z","repository":{"id":12626232,"uuid":"15297496","full_name":"lightsofapollo/amqpworkers","owner":"lightsofapollo","description":null,"archived":false,"fork":false,"pushed_at":"2014-01-28T20:00:21.000Z","size":1452,"stargazers_count":1,"open_issues_count":1,"forks_count":0,"subscribers_count":0,"default_branch":"master","last_synced_at":"2025-02-06T07:37:23.664Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/lightsofapollo.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2013-12-19T00:04:28.000Z","updated_at":"2017-04-15T19:47:14.000Z","dependencies_parsed_at":"2022-09-26T18:40:30.623Z","dependency_job_id":null,"html_url":"https://github.com/lightsofapollo/amqpworkers","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/lightsofapollo%2Famqpworkers","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/lightsofapollo%2Famqpworkers/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/lightsofapollo%2Famqpworkers/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/lightsofapollo%2Famqpworkers/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/lightsofapollo","download_url":"https://codeload.github.com/lightsofapollo/amqpworkers/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":239225442,"owners_count":19603155,"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-07T07:29:51.815Z","updated_at":"2025-11-05T22:30:21.381Z","avatar_url":"https://github.com/lightsofapollo.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# AMQP Workers\n\n[![Build Status](https://travis-ci.org/lightsofapollo/amqpworkers.png)](https://travis-ci.org/lightsofapollo/amqpworkers)\n\nAMQP Workers is an opinioned library which codifies a lot of my personal\ntastes while working with AMQP in JavaScript (node). It also embraces a\nPromise _only_ api (everything should return a promise) and requires you\nto setup / manage your own amqp connection (through [amqplib](https://npmjs.org/package/amqplib)).\n\nThe primary export is build out of four smaller modules (and while you\ncan use the top level export using the longer form is probably what you\nwant).\n\n- [Schema](#schema)\n- [Consumer](#consumer)\n- [Message](#message)\n- [Publisher](#publisher)\n\n\n## Schema\n\nA \"Schema\" is a blueprint to build all queues, exchanges and bindings\nbetween them. Generally you always need to define a schema and its a\ngood idea to try to build it before publishing new messages or consuming\na queue. \n\n```js\n\n// your_schema.js\n\nvar Schema = require('amqpworkers/schema');\n\nmodule.exports = new Schema({\n  // see examples/schema_config.json\n});\n\n```\n\nNow that the schema is defined we can use it to define, purge and\ndestroy it. In RabbitMQ 3.2 and greater all are idempotent but deletes\nwill fail if the queue/exchange does not exist in earlier versions.\n\n```js\n// Define the schema\n\nvar AMQPSchema = require('./my_schema');\n\n// this is the result from amqplib.connect\nvar connection;\n\nAMQPSchema.define(connection).then(\n //\n);\n\n// Destroy the schema (delete queues and exchanges)\n\nAMQPSchema.destroy(connection).then(\n // messages and queues are gone! Good for testing\n);\n\n// Purge the messages but leave the exchanges, queues and bindings alone\n\nAMQPSchema.purge(connection).then(\n // messages and queues are gone! Good for testing\n);\n```\n\n## Consumer\n\nA consumer is an object oriented approach to consuming queues. They can\nbe used directly by instantiating Consumer or via inheritance.\n\n```js\nvar Consumer = require('amqpworkers/consumer');\n\n// this is the result from amqplib.connect\nvar connection;\n\nvar consumer = new Consumer(connection);\n\n// Read will be called when an item is being consumed from the queue.\nconsumer.read = function(content, message) {\n  // content is the parsed content of the message\n  // and message is un mutated value from amqplib\n\n\n  // the promise is optional but highly recommended if you care about\n  // ack / nack. When this promise is accepted an ack will be sent\n  // (and you guessed! nack when rejected).\n  return new Promise(function(accept, reject) {\n\n  });\n}\nconsumer.consume('the queue name', {\n  // optional prefetch option\n  prefetch: 1\n});\n\n// cleanup when your done\nconsumer.close();\n```\n\nThe consumer has the `parseMessage` method which will be called prior\nto passing the result of that function and the message along to the\n.read method. This can be used as a hook for implementing other\nde-serializing protocols.\n\n## Message\n\nA message is a simple representation of an _outbound_ (to be published)\nmessage.\n\nMessages are simply any object with a `.buffer [Buffer]` property and an `.options [Object]` property.\nThe provided object will parse objects into json blobs\n`(new Buffer(JSON.stringify(obj))` and stamp them with `contentType`\n`application/json`\n\n```js\nvar Message = require('amqp/message');\n\nvar myMsg = new Message(\n  // will be converted into a buffer\n  { woot: true },\n\n  // see amqplib #publish options\n  { persistent: true }\n);\n\n// json blob\nmyMsg.buffer;\n\n// application/json\nmyMsg.options.contentType;\n```\n\nMessages are only useful in conjunction with [Publisher's](#publisher)\n`#publish` method.\n\n## Publisher\n\nPublishers are fairly simple wrappers around #publish and confirm\nchannels. The assumption is that every message is critical and slowing\ndown the publishing process to confirm writes is more important then\nraw speed.\n\n```js\nvar Publisher = require('amqpworkers/publisher'),\n    Message = require('amqpworkers/message');\n\n// from amqplib #connect\nvar connection;\n\nvar tasks = new Publisher(connection);\n\n// publish something to the task exchange\n\ntasks.publish(\n  'tasks', // exchange name\n  'request', // routing key\n  new Message({ woot: true }, { persistent: true })\n).then(\n  function() {\n    // confirmed  \n  },\n  function() {\n    // rejected\n  }\n);\n\n// cleanup when your done\ntasks.close();\n```\n\n## License\n\nThe MIT License (MIT)\n\nCopyright (c) 2013 Sahaja James Lal\n\nPermission is hereby granted, free of charge, to any person obtaining a copy\nof this software and associated documentation files (the \"Software\"), to deal\nin the Software without restriction, including without limitation the rights\nto use, copy, modify, merge, publish, distribute, sublicense, and/or sell\ncopies of the Software, and to permit persons to whom the Software is\nfurnished to do so, subject to the following conditions:\n\nThe above copyright notice and this permission notice shall be included in\nall copies or substantial portions of the Software.\n\nTHE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR\nIMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,\nFITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE\nAUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER\nLIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,\nOUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN\nTHE SOFTWARE.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Flightsofapollo%2Famqpworkers","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Flightsofapollo%2Famqpworkers","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Flightsofapollo%2Famqpworkers/lists"}