{"id":22473114,"url":"https://github.com/bas080/amqplib-stream","last_synced_at":"2025-08-02T10:30:58.451Z","repository":{"id":53055517,"uuid":"135619626","full_name":"bas080/amqplib-stream","owner":"bas080","description":"Stream interface for amqplib","archived":false,"fork":false,"pushed_at":"2021-09-30T17:11:00.000Z","size":55,"stargazers_count":3,"open_issues_count":1,"forks_count":1,"subscribers_count":3,"default_branch":"master","last_synced_at":"2024-11-29T03:42:42.033Z","etag":null,"topics":["amqplib","nodejs","stream"],"latest_commit_sha":null,"homepage":"https://www.npmjs.com/package/amqplib-stream","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/bas080.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":"2018-05-31T18:12:49.000Z","updated_at":"2022-07-05T15:17:03.000Z","dependencies_parsed_at":"2022-08-24T11:40:59.947Z","dependency_job_id":null,"html_url":"https://github.com/bas080/amqplib-stream","commit_stats":null,"previous_names":[],"tags_count":4,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/bas080%2Famqplib-stream","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/bas080%2Famqplib-stream/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/bas080%2Famqplib-stream/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/bas080%2Famqplib-stream/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/bas080","download_url":"https://codeload.github.com/bas080/amqplib-stream/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":228461622,"owners_count":17923830,"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":["amqplib","nodejs","stream"],"created_at":"2024-12-06T12:19:02.186Z","updated_at":"2024-12-06T12:19:04.497Z","avatar_url":"https://github.com/bas080.png","language":"JavaScript","readme":"# amqplib-stream\n\nA stream interface for amqplib\n\n- [`createReadStream`](#createreadstream)\n  * [`fromQueue`](#fromqueue)\n  * [`contentFromQueue`](#contentfromqueue)\n  * [Custom read configuration](#custom-read-configuration)\n- [`createWriteStream`](#createwritestream)\n  * [`toQueue`](#toqueue)\n  * [`toExchange`](#toexchange)\n  * [Custom write configuration](#custom-write-configuration)\n\n## Reasoning\n\nHaving an interface that uses the nodejs Stream api makes sense when working\nwith fifo queues. The goal is to have the domain of queues be represented by\nstreams.\n\n## API\n\nThe whole api consist out of two main functions named `createReadStream` and\n`createWriteStream`. These can be configured to use a specific connection and/or\nchannel.\n\n### `createReadStream`\n\nThe createReadStream function creates a stream that consumes messages from a\nqueue. This can be configured with the `fromQueue` configuration helper.\n\nThere are also other things that have to do with reading from a queue. One of\nthem is acking or nacking message(s).\n\nThere are two configuration helpers that should make it easy to configure\na read stream.\n\n```js\nconst amqplib = require('amqplib')\nconst channel = amqplib.connect('amqp://localhost').then(conn =\u003e conn.createChannel())\nconst {createReadStream, fromQueue} = require('amqplib-stream')\n\ncreateReadStream({\n  channel,\n  read: fromQueue('my-queue')\n})\n```\n\n#### `fromQueue`\n\nResults in a stream of amqplib messages. These can be used with\n`channel.(ack|nack)` or other `amqplib` library features.\n\n```js\nconst amqplib = require('amqplib')\nconst {fromQueue, createReadStream} = require('amqplib-stream')\nconst {PassThrough} = require('stream')\nconst channelPromise = amqplib.connect('amqp://localhost').then(conn =\u003e conn.createChannel())\n\nconst ackAll = new PassThrough({objectMode: true})\n\nchannelPromise\n  .then(channel =\u003e ackAll.on('data', message =\u003e channel.ack(message)))\n\ncreateReadStream({\n  channel: channelPromise,\n  read: fromQueue('my-queue', {noAck: true})\n}).pipe(ackAll)\n```\n\n#### `contentFromQueue`\n\nWon't require acking manually. Acking is done as soon as the message is written\nto the stream. If you need to manually ack then use `fromQueue` instead.\n\nReason why is because acking requires a message object. This config function\nresults in a stream where only the message's content is part of the stream.\n\n#### Custom read configuration\n\nIt is also possible to write your own read function.\n\nLet's look at how `fromQueue` is implemented to get a better idea of how to\nwrite your own.\n\n```js\n'use strict'\n\nmodule.exports = function fromQueue(name, options = {}) {\n  return (channel, write) =\u003e channel.consume(name, write, options)\n}\n```\n\nThe `fromQueue` returns a function that complies with the read function\nsignature that the readable stream expects.\n\nFor lack of a better example I could write a configuration function that\nbasicly consumes and then nacks straight away.\n\n```js\nmodule.exports = function fromQueueAndNack(name, options) {\n  return (channel, read) =\u003e {\n    return channel.consume(name, msg =\u003e {\n      channel.nack(msg)\n\n      read(msg)\n    }, options)\n  }\n}\n```\n\nThe [write streams](##createWriteStream) also allow you to define [custom write behavior](###Custom write configuration).\n\n### `createWriteStream`\n\nThe createWriteStream function creates a stream that publishes to either queues\nor exchanges. This can be configured using some predefined configuration\nhelpers, or by writing your own publish configuration.\n\nAmqplib allows one to publish to exchanges and to send messages to queues.\n\n```js\nconst amqplib = require('amqplib')\nconst channel = amqplib.connect('amqp://localhost').createChannel()\nconst {createWriteStream, toExchange, toQueue} = require('amqplib-stream')\n\n// Publish messages to an exchange\ncreateWriteStream({\n  channel,\n  publish: toExchange('my-exchange')\n})\n\n// Send messages to a queue\ncreateWriteStream({\n  channel,\n  publish: toQueue('my-queue')\n})\n```\n\n#### `toQueue`\n\n```js\nconst amqplib = require('amqplib')\nconst channelPromise = amqplib.connect('amqp://localhost').then(conn =\u003e conn.createChannel())\nconst {toQueue, createWriteStream} = require('amqplib-stream')\n\nchannelPromise.then(() =\u003e {\n  const myQueueStream = createWriteStream({\n    channel: channelPromise,\n    write: toQueue('my-queue')\n  })\n\n  myQueueStream.write('jenny')\n  myQueueStream.write('jake')\n  myQueueStream.write('josh')\n})\n\n```\n\n#### `toExchange`\n\n```js\nconst amqplib = require('amqplib')\nconst {toExchange, createWriteStream} = require('amqplib-stream')\nconst channelPromise = amqplib.connect('amqp://localhost').then(conn =\u003e conn.createChannel())\n\nchannelPromise.then(() =\u003e {\n  const myExchangeStream = createWriteStream({\n    channel: channelPromise,\n    write: toExchange('my-exchange', {routingKey: () =\u003e 'my-routing-key'})\n  })\n\n  myExchangeStream.write('jenny')\n  myExchangeStream.write('jake')\n  myExchangeStream.write('josh')\n})\n\n```\n\nNotice how similar the code examples are. The main difference is the\nconfiguration function.\n\n#### Custom write configuration\n\nWe can also define our own write configuration. Maybe you want to log whenever\na message is being written with succes.\n\n```js\n/*eslint no-console: \"off\"*/\n\nmodule.exports = function fromQueueAndNack(name, options = {}) {\n  return (channel, write) =\u003e {\n    return channel.consume(name, msg =\u003e {\n      channel.nack(msg)\n\n      write(msg)\n        .then(() =\u003e console.info('success', msg))\n        .catch(console.error)\n    }, options)\n  }\n}\n```\n\n# Tests\n\nThe tests require you to have RabbitMQ installed. Consider following the\ninstallation instructions located at\n[RabbitMQ](https://www.rabbitmq.com/download.html)\n\nAfter installing RabbitMQ you can run the tests with `npm test`.\n\n## Coverage\n\n```\n-------------------------|----------|----------|----------|----------|-------------------|\nFile                     |  % Stmts | % Branch |  % Funcs |  % Lines | Uncovered Line #s |\n-------------------------|----------|----------|----------|----------|-------------------|\nAll files                |    95.71 |    76.92 |    94.34 |    96.85 |                   |\n lib                     |    94.92 |       80 |    93.18 |    96.26 |                   |\n  create-read-stream.js  |    81.25 |      100 |    71.43 |    85.71 |             19,21 |\n  create-write-stream.js |    90.91 |    66.67 |     87.5 |    94.44 |                19 |\n  helpers.js             |      100 |       50 |      100 |      100 |                 4 |\n  helpers.spec.js        |      100 |      100 |      100 |      100 |                   |\n  index.js               |      100 |      100 |      100 |      100 |                   |\n  index.spec.js          |    98.41 |      100 |      100 |    98.28 |               124 |\n lib/configuration       |      100 |    66.67 |      100 |      100 |                   |\n  content-from-queue.js  |      100 |      100 |      100 |      100 |                   |\n  from-queue.js          |      100 |      100 |      100 |      100 |                   |\n  index.js               |      100 |      100 |      100 |      100 |                   |\n  to-exchange.js         |      100 |        0 |      100 |      100 |                 9 |\n  to-queue.js            |      100 |      100 |      100 |      100 |                   |\n-------------------------|----------|----------|----------|----------|-------------------|\n```\n\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fbas080%2Famqplib-stream","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fbas080%2Famqplib-stream","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fbas080%2Famqplib-stream/lists"}