{"id":22737256,"url":"https://github.com/noblesamurai/node-simple-amqplib-rpc","last_synced_at":"2025-03-30T03:15:41.997Z","repository":{"id":48760633,"uuid":"174252844","full_name":"noblesamurai/node-simple-amqplib-rpc","owner":"noblesamurai","description":"simple amqp rpc interface","archived":false,"fork":false,"pushed_at":"2021-07-13T03:00:17.000Z","size":24,"stargazers_count":1,"open_issues_count":0,"forks_count":0,"subscribers_count":5,"default_branch":"master","last_synced_at":"2025-03-07T04:37:52.451Z","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":"other","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/noblesamurai.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":"2019-03-07T01:59:57.000Z","updated_at":"2023-01-18T08:57:44.000Z","dependencies_parsed_at":"2022-08-27T00:01:09.732Z","dependency_job_id":null,"html_url":"https://github.com/noblesamurai/node-simple-amqplib-rpc","commit_stats":null,"previous_names":[],"tags_count":5,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/noblesamurai%2Fnode-simple-amqplib-rpc","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/noblesamurai%2Fnode-simple-amqplib-rpc/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/noblesamurai%2Fnode-simple-amqplib-rpc/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/noblesamurai%2Fnode-simple-amqplib-rpc/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/noblesamurai","download_url":"https://codeload.github.com/noblesamurai/node-simple-amqplib-rpc/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":246269933,"owners_count":20750321,"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-10T22:10:50.262Z","updated_at":"2025-03-30T03:15:41.961Z","avatar_url":"https://github.com/noblesamurai.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Simple amqplib RPC\n\n\u003e Simple RPC interface for [amqplib](https://github.com/squaremo/amqp.node)\n\n## Installation\n\nThis module is installed via npm:\n\n``` bash\n$ npm install simple-amqplib-rpc\n```\n\n## Example Usage\n\nClient side:\n```js\nconst amqplib = require('amqplib');\nconst { request } = require('simple-amqplib-rpc');\nconst config = {\n  url: 'amqp://...',\n  exchange: 'exchange',\n  routingKey: 'sum'\n};\n\nconst connection = await amqplib.connect(config.url);\ntry {\n  const content = [ 1, 2, 3 ];\n  const opts = { exchange: config.exchange, timeout: 5000 };\n  const resp = await request(connection, config.routingKey, content, opts);\n  // resp = 6\n} catch (err) {\n  switch (err.name) {\n    case 'ChannelClosedError': // the connection was closed unexpectedly.\n    case 'NoRouteError': // the specified routing key goes nowhere (server needs to bindQueue).\n    case 'ResponseError': // the request returned an error.\n    case 'TimeoutError': // the request took more than 5 seconds.\n  }\n}\n```\n\nServer side:\n```js\nconst amqplib = require('amqplib');\nconst { checkReplyQueue, error, reply } = require('simple-amqplib-rpc');\nconst config = {\n  url: 'amqp://...',\n  exchange: 'exchange',\n  queue: 'sum',\n  routingKey: 'sum'\n};\n\nconst connection = await amqplib.connect(config.url);\nconst consumeChannel = await connection.createChannel();\nawait consumeChannel.assertQueue(config.queue);\nawait consumeChannel.bindQueue(config.queue, config.exchange, config.routingKey);\nconst publishChannel = await connection.createChannel();\nconsumeChannel.consume(config.queue, async message =\u003e {\n  if (!await checkReplyQueue(connection, message)) { // the consumer doesn't exist anymore\n    return consumeChannel.reject(message, false); // reject and don't requeue\n  }\n  try {\n    const numbers = JSON.parse(message.content);\n    const sum = numbers.reduce((acc, n) =\u003e acc + n, 0);\n    reply(consumeChannel, message, sum, publishChannel);\n  } catch (err) {\n    // if something went wrong, return an error to the client\n    error(consumeChannel, message, err, publishChannel);\n  }\n});\n```\n\n## API\n\n\u003cdl\u003e\n\u003cdt\u003e\u003ca href=\"#checkReplyQueue\"\u003echeckReplyQueue(connection, message)\u003c/a\u003e ⇒ \u003ccode\u003eboolean\u003c/code\u003e\u003c/dt\u003e\n\u003cdd\u003e\u003cp\u003eCheck if a \u0026quot;reply to\u0026quot; queue exists or not. Will create a separate channel so that it doesn\u0026#39;t\nkill an existing one if the queue check fails.\u003c/p\u003e\n\u003c/dd\u003e\n\u003cdt\u003e\u003ca href=\"#error\"\u003eerror(channel, message, error, publisherChannel)\u003c/a\u003e\u003c/dt\u003e\n\u003cdd\u003e\u003cp\u003eReply to an rpc request with an error. Will automatically nack and not requeue the message after\nthe error response has been sent.\u003c/p\u003e\n\u003c/dd\u003e\n\u003cdt\u003e\u003ca href=\"#reply\"\u003ereply(channel, message, content, publisherChannel)\u003c/a\u003e\u003c/dt\u003e\n\u003cdd\u003e\u003cp\u003eReply to an rpc request. Will automatically ack the message after the response has been sent.\u003c/p\u003e\n\u003c/dd\u003e\n\u003cdt\u003e\u003ca href=\"#request\"\u003erequest(connection, key, content)\u003c/a\u003e ⇒ \u003ccode\u003e*\u003c/code\u003e\u003c/dt\u003e\n\u003cdd\u003e\u003cp\u003eMake an rpc request. Each request will have its own channel.\u003c/p\u003e\n\u003c/dd\u003e\n\u003c/dl\u003e\n\n\u003ca name=\"checkReplyQueue\"\u003e\u003c/a\u003e\n\n## checkReplyQueue(connection, message) ⇒ \u003ccode\u003eboolean\u003c/code\u003e\nCheck if a \"reply to\" queue exists or not. Will create a separate channel so that it doesn't\nkill an existing one if the queue check fails.\n\n**Kind**: global function\n**Returns**: \u003ccode\u003eboolean\u003c/code\u003e - whether the reply queue exists or not\n\n| Param | Type | Description |\n| --- | --- | --- |\n| connection | \u003ccode\u003eamqplibConnection\u003c/code\u003e | amqplib connection |\n| message | \u003ccode\u003eobject\u003c/code\u003e | incomming message |\n\n\u003ca name=\"error\"\u003e\u003c/a\u003e\n\n## error(channel, message, error, publisherChannel)\nReply to an rpc request with an error. Will automatically nack and not requeue the message after\nthe error response has been sent.\n\n**Kind**: global function\n\n| Param | Type | Description |\n| --- | --- | --- |\n| channel | \u003ccode\u003eAmqplibChannel\u003c/code\u003e | the amqplib channel on which the message was received |\n| message | \u003ccode\u003eobject\u003c/code\u003e | incomming message |\n| error | \u003ccode\u003eError\u003c/code\u003e | an error object. `{ message, code }` will be returned to the client. |\n| publisherChannel | \u003ccode\u003eAmqplibChannel\u003c/code\u003e | optional separate channel to publish response on |\n\n\u003ca name=\"reply\"\u003e\u003c/a\u003e\n\n## reply(channel, message, content, publisherChannel)\nReply to an rpc request. Will automatically ack the message after the response has been sent.\n\n**Kind**: global function\n\n| Param | Type | Description |\n| --- | --- | --- |\n| channel | \u003ccode\u003eAmqplibChannel\u003c/code\u003e | on which the message was received |\n| message | \u003ccode\u003eobject\u003c/code\u003e | incomming message |\n| content | \u003ccode\u003e\\*\u003c/code\u003e | response message |\n| publisherChannel | \u003ccode\u003eAmqplibChannel\u003c/code\u003e | optional separate channel to publish response on |\n\n\u003ca name=\"request\"\u003e\u003c/a\u003e\n\n## request(connection, key, content, opts) ⇒ \u003ccode\u003e\\*\u003c/code\u003e\nMake an rpc request. Each request will have its own channel.\n\n**Kind**: global function\n**Returns**: \u003ccode\u003e\\*\u003c/code\u003e - json decoded response\n**Throws**:\n\n- \u003ccode\u003eChannelClosedError\u003c/code\u003e when the channel is closed\n- \u003ccode\u003eNoRouteError\u003c/code\u003e if a published message has nowhere to go\n- \u003ccode\u003eResponseError\u003c/code\u003e if the request returned an error\n- \u003ccode\u003eTimeoutError\u003c/code\u003e after the specified timeout period\n\n\n| Param | Type | Description |\n| --- | --- | --- |\n| connection | \u003ccode\u003eamqplibConnection\u003c/code\u003e | amqplib connection |\n| key | \u003ccode\u003estring\u003c/code\u003e | the routing key for the rpc service |\n| content | \u003ccode\u003e\\*\u003c/code\u003e | must be json serialisable |\n| opts | \u003ccode\u003eobject\u003c/code\u003e |  |\n| opts.exchange | \u003ccode\u003estring\u003c/code\u003e | the amqp exchange to publish to (defaults to '') |\n| opts.timeout | \u003ccode\u003enumber\u003c/code\u003e | optional max time to wait for a response |\n\nNote: To regenerate this section from the jsdoc run `npm run docs` and paste\nthe output above.\n\n## Contributing\n\n### Prerequisites\n\n```\n$ pip install pre-commit\n```\n\n### Installation\n\n```\n$ pre-commit install --install-hooks\n```\n\n## License\n\nThe BSD License\n\nCopyright (c) 2019, Andrew Harris\n\nAll rights reserved.\n\nRedistribution and use in source and binary forms, with or without modification,\nare permitted provided that the following conditions are met:\n\n* Redistributions of source code must retain the above copyright notice, this\n  list of conditions and the following disclaimer.\n\n* Redistributions in binary form must reproduce the above copyright notice, this\n  list of conditions and the following disclaimer in the documentation and/or\n  other materials provided with the distribution.\n\n* Neither the name of the Andrew Harris nor the names of its\n  contributors may be used to endorse or promote products derived from\n  this software without specific prior written permission.\n\nTHIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS \"AS IS\" AND\nANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED\nWARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE\nDISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR\nANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES\n(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;\nLOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON\nANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT\n(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS\nSOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fnoblesamurai%2Fnode-simple-amqplib-rpc","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fnoblesamurai%2Fnode-simple-amqplib-rpc","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fnoblesamurai%2Fnode-simple-amqplib-rpc/lists"}