{"id":22894248,"url":"https://github.com/clearcodehq/npm-rabbitmq-simplified","last_synced_at":"2025-03-31T22:38:30.787Z","repository":{"id":57102479,"uuid":"124525574","full_name":"ClearcodeHQ/npm-rabbitmq-simplified","owner":"ClearcodeHQ","description":null,"archived":false,"fork":false,"pushed_at":"2019-02-12T11:51:51.000Z","size":31,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":7,"default_branch":"master","last_synced_at":"2025-03-30T13:37:09.519Z","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":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/ClearcodeHQ.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}},"created_at":"2018-03-09T10:26:11.000Z","updated_at":"2019-02-12T11:51:53.000Z","dependencies_parsed_at":"2022-08-20T18:40:37.359Z","dependency_job_id":null,"html_url":"https://github.com/ClearcodeHQ/npm-rabbitmq-simplified","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/ClearcodeHQ%2Fnpm-rabbitmq-simplified","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ClearcodeHQ%2Fnpm-rabbitmq-simplified/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ClearcodeHQ%2Fnpm-rabbitmq-simplified/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ClearcodeHQ%2Fnpm-rabbitmq-simplified/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/ClearcodeHQ","download_url":"https://codeload.github.com/ClearcodeHQ/npm-rabbitmq-simplified/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":246552972,"owners_count":20795835,"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-12-13T23:17:19.038Z","updated_at":"2025-03-31T22:38:30.768Z","avatar_url":"https://github.com/ClearcodeHQ.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# RabbitMQ simplified\n\nSimplifies asynchronous connection to RabbitMq with connection retrying, as well as providing promise-based helper methods for creating channels, exchanges and queues, binding queue with exchange and consuming with given prefetch, all with error handling.\n\nIf connection cannot be established, a new attempt will be made after a delay set in config (5 seconds by default). Every subsequent retry will occur after two times as long (so by default 10, 20, 40, 80 etc seconds). You can set how many times connection will be retried (10 by default).\n\n## Installation\n\nAdd to your dependencies:\n\n```\n\"dependencies\": {\n    \"rabbitmq-simplified\": \"https://github.com/ClearcodeHQ/npm-rabbitmq-simplified\"\n}\n```\n\n## Usage\n\n```\nconst RabbitMQ = require(\"rabbitmq-simplified\");\n\n// You can, but don't have to pass the config array or any of its values\nconst config = {\n    hostname: 'rabbit', // by default process.env.RABBITMQ_HOST\n    username: 'user',  // by default process.env.RABBITMQ_USER\n    password: 'pass', // by default process.env.RABBITMQ_PASS\n    port: 5959, // by default process.env.RABBITMQ_PORT\n    maxRabbitConnectionRetries: 5, // by default 10\n    retryAfter: 3000 // in miliseconds, by default 5000\n    retryAfterMultiplier: 1 // by default 2\n}\n\nconst Connector = new RabbitMQ(config);\n```\n\n### Obtaining connection and channel\n\n```\nlet channel = await Connector.connectToRabbit().then(Connector.createChannel);\n```\n\n### Asserting exchange existence\n\nIf exchange with given name doesn't exist, it will be created with given options and channel returned back.\nIf exchange with given name exists with the same options and type, we're ok and channel will be returned back.\nIf exchange with given name exists with different options or type, an error will occur and null will be returned.\n\n```\nConnector.assertExchange(\n    channel, // channel obtained from createChannel method on which the assertion should be sent to RabbitMQ\n    exchangeName, // string with exchange name\n    exchangeType, // string with exchange type; optional, direct by default\n    exchangeOptions // object with exchange options; optional {durable:true} by default\n);\n```\n\nExample:\n\n```\nif (Connector.assertExchange(channel, \"exchange\", \"fanout\")) {\n    // Now you know that exchange is available, so you can send messages there\n} else {\n    // Something went wrong, try again or leave\n}\n```\n\n### Asserting queue existence\n\nIf queue with given name doesn't exist, it will be created with given options and channel returned back.\nIf queue with given name exists with the same options, we're ok and channel will be returned back.\nIf queue with given name exists with different options, an error will occur and null will be returned.\n\n\n```\nConnector.assertQueue(\n    channel, // channel obtained from createChannel method on which the assertion should be sent to RabbitMQ\n    queueName, // string with queue name\n    queueOptions // object with exchange options; optional {durable:true} by default\n);\n```\n\nExample:\n\n```\nif (Connector.assertQueue(channel, \"queue\", {durable:false})) {\n    // Queue is available, you can consume from it\n} else {\n    // Something went wrong, try again or leave\n}\n```\n\n### Binding queue to exchange\n\n```\nConnector.bindQueueToExchange(\n    channel, // channel obtained from createChannel method on which the assertion should be sent to RabbitMQ\n    queueName, // string with queue name, must be asserted beforehand\n    exchangeName, // string with exchange name, must be asserted beforehand\n    routingKey // routing key on which the exchange and queue will be bound; empty by default\n);\n```\n\nExample:\n\n```\nif (Connector.bindQueueToExchange(channel, \"queue\", \"exchange\", \"key\")) {\n    // Queue is bound to exchange, messages with given routing key will be delivered there\n} else {\n    // Something went wrong, try again or leave\n}\n```\n\n### Asserting and consuming queue\n\nA shorthand for calling `assertQueue`, setting prefetch and starting consuming messages. Returns true if queue was asserted and consumption can start, false otherwise.\n\n```\nConnector.assertAndConsumeQueue(\n    channel, // channel obtained from createChannel method on which the assertion should be sent to RabbitMQ\n    queueName, // string with queue name, must be asserted beforehand\n    callback, // a function that will manage the received messages (passed as the only parameter)\n    prefetch, // how many messages should be prefetched, one by default\n    queueOptions // optional queue options, defaults are the same as in assertQueue\n);\n```\n\n### Publishing helper\n\nCalls `channel.publish` with empty routing key by default and options that make the message persistent (which means it will be stored in case of RabbitMQ shutdown) and it's delivery mandatory (which means that exchange must have a way to route it to a queue, otherwise it will fail and return false).\n\nIt is a synchronous method and returns true on successful publish or false if something went wrong.\n\n```\nConnector.publishMessage(\n    channel, // channel obtained from createChannel method on which the message should be sent to RabbitMQ\n    exchange, // string with exchange name, must be asserted beforehand\n    message, // string with the message to publish\n    routingKey, // optional routing key, empty by default\n    options // optional message options, by default the message is persistent and it's delivery is mandatory\n);\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fclearcodehq%2Fnpm-rabbitmq-simplified","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fclearcodehq%2Fnpm-rabbitmq-simplified","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fclearcodehq%2Fnpm-rabbitmq-simplified/lists"}