{"id":13598869,"url":"https://github.com/nestjsx/nestjs-amqp","last_synced_at":"2025-06-20T07:36:23.690Z","repository":{"id":35100841,"uuid":"139561270","full_name":"nestjsx/nestjs-amqp","owner":"nestjsx","description":"Amqp connections for nestjs :love_letter:","archived":false,"fork":false,"pushed_at":"2023-01-16T09:09:34.000Z","size":841,"stargazers_count":187,"open_issues_count":31,"forks_count":25,"subscribers_count":4,"default_branch":"master","last_synced_at":"2025-06-14T08:07:46.120Z","etag":null,"topics":["amqp","connection","manager","module","nestjs","typescript"],"latest_commit_sha":null,"homepage":"","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/nestjsx.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-07-03T09:38:24.000Z","updated_at":"2025-02-13T09:36:29.000Z","dependencies_parsed_at":"2023-02-10T02:15:58.487Z","dependency_job_id":null,"html_url":"https://github.com/nestjsx/nestjs-amqp","commit_stats":null,"previous_names":["nestjs-community/nestjs-amqp","bashleigh/nestjs-amqp"],"tags_count":1,"template":false,"template_full_name":null,"purl":"pkg:github/nestjsx/nestjs-amqp","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/nestjsx%2Fnestjs-amqp","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/nestjsx%2Fnestjs-amqp/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/nestjsx%2Fnestjs-amqp/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/nestjsx%2Fnestjs-amqp/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/nestjsx","download_url":"https://codeload.github.com/nestjsx/nestjs-amqp/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/nestjsx%2Fnestjs-amqp/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":260073376,"owners_count":22954943,"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":["amqp","connection","manager","module","nestjs","typescript"],"created_at":"2024-08-01T17:00:57.610Z","updated_at":"2025-06-20T07:36:23.652Z","avatar_url":"https://github.com/nestjsx.png","language":"TypeScript","funding_links":[],"categories":["资源","Integrations"],"sub_categories":["集成"],"readme":"\u003cp align=\"center\"\u003e\u003cimg src=\"https://avatars1.githubusercontent.com/u/43827489?s=400\u0026u=45ac0ac47d40b6d8f277c96bdf00244c10508aef\u0026v=4\"/\u003e\u003c/p\u003e\n\u003cp align=\"center\"\u003e\n    \u003ca href=\"https://travis-ci.org/nestjsx/nestjs-amqp\"\u003e\u003cimg src=\"https://travis-ci.org/nestjsx/nestjs-amqp.svg?branch=master\"/\u003e\u003c/a\u003e\n    \u003ca href=\"https://www.npmjs.com/package/nestjs-amqp\"\u003e\u003cimg src=\"https://img.shields.io/npm/v/nestjs-amqp.svg\"/\u003e\u003c/a\u003e\n    \u003ca href=\"https://github.com/nestjsx/nestjs-amqp/blob/master/LICENSE\"\u003e\u003cimg src=\"https://img.shields.io/github/license/nestjsx/nestjs-amqp.svg\"/\u003e\u003c/a\u003e\n    \u003ca href=\"https://coveralls.io/github/nestjsx/nestjs-amqp?branch=master\"\u003e\u003cimg src=\"https://coveralls.io/repos/github/nestjsx/nestjs-amqp/badge.svg?branch=master\"/\u003e\u003c/a\u003e\n    \u003cimg src=\"https://flat.badgen.net/dependabot/nestjsx/nestjs-config?icon=dependabot\" /\u003e\n    \u003cimg src=\"https://camo.githubusercontent.com/a34cfbf37ba6848362bf2bee0f3915c2e38b1cc1/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f5052732d77656c636f6d652d627269676874677265656e2e7376673f7374796c653d666c61742d737175617265\" /\u003e\n    \u003ca href=\"https://github.com/juliandavidmr/awesome-nestjs#components--libraries\"\u003e\u003cimg src=\"https://raw.githubusercontent.com/nestjsx/crud/master/img/awesome-nest.svg?sanitize=true\" alt=\"Awesome Nest\" /\u003e\u003c/a\u003e\n    \u003ca href=\"https://github.com/nestjs/nest\"\u003e\u003cimg src=\"https://raw.githubusercontent.com/nestjsx/crud/master/img/nest-powered.svg?sanitize=true\" alt=\"Nest Powered\" /\u003e\u003c/a\u003e\n\u003c/p\u003e\n\u003ch1 align=\"center\"\u003eNestjs Amqp\u003c/h1\u003e\n\n\u003cp align=\"center\"\u003eAn AMQP connection service for \u003ca href=\"https://github.com/nestjs/nest\"\u003eNestJS\u003c/a\u003e.\u003c/p\u003e\n\n\u003cp\u003eUsing the \u003ca href=\"https://github.com/squaremo/amqp.node\"\u003eAMQPlib\u003c/a\u003e for node package.\u003c/p\u003e\n\nThis package was intented to be used in execution content and provides a basic AMQPlib connection via the providers to allow developers to develop their amqp queue consumers and publishers. For microservice transport; check out \u003ca href=\"https://docs.nestjs.com/microservices/rabbitmq\"\u003ethe docs\u003c/a\u003e for rabbitMQ.\n\n## Install\n\n```bash\n$ yarn add nestjs-amqp\n$ yarn add -D @types/amqplib\n```\n\n## Basic usage \n\n```ts\nimport {Module} from '@nestjs/common';\nimport {AmqpModule} from 'nestjs-amqp';\n\n@Module({\n  imports: [AmqpModule.forRoot({\n    name: 'rabbitmq',\n    hostname: 'localhost',\n    port: 5672,\n    username: 'test',\n    password: 'test',\n  })],\n})\nexport default class AppModule {}\n\n```\n\n## Advanced usage\n### Usage with nestjs-config\n\n```ts\nimport {Module} from '@nestjs/common';\nimport {AmqpModule} from 'nestjs-amqp';\nimport {ConfigModule, ConfigService} from 'nestjs-config';\nimport * as path from 'path';\n\n@Module({\n  imports: [\n    ConfigModule.load(path.resolve(__dirname, 'config', '**/*.ts')),\n    AmqpModule.forRootAsync({\n      useFactory: (config: ConfigService) =\u003e config.get('amqp'),\n      inject: [ConfigService],\n    }),\n  ],\n})\nexport default class AppModule {}\n\n//src/config/amqp.ts\nexport default {\n  name: 'rabbitmq',\n  hostname: process.env.AMQP_HOST,\n  port: process.env.AMQP_PORT,\n  username: process.env.USERNAME,\n  password: process.env.PASSWORD,\n} \n```\n\u003e Unfortunately multiple connections are unavailable when using the `forRootAsync` method.\n\n### Usage in custom provider\nIt is possible to inject the AmqpConnection in a factory of a custom provider if one needs such capability. \n\n\n```ts\nimport { Connection as AmqpConnection } from 'amqplib';\nimport {ConfigService} from 'nestjs-config';\nimport {createConnectionToken} from 'nestjs-amqp/utils';\n\nexport const queueServiceProvider = {\n    provider: 'QueueService',\n    useFactory: (amqp: AmqpConnection, configService: ConfigService) =\u003e new QueueService(amqp, config.get('queue')),\n    inject: [createConnectionToken('default'), ConfigService],\n}\n```\n\nIt's also possible to give your connections names, if you have done so then use the name of your connection instead of ``default``.\n### Connection Decorators\n\n```ts\nimport {Module} from '@nestjs/common';\nimport {AmqpModule} from 'nestjs-amqp';\n\n@Module({\n  imports: [AmqpModule.forRoot([\n    {\n      hostname: 'test:test@localhost',\n    }, \n    {\n      username: 'test',\n      password: 'test',\n      hostname: 'localhost',\n      port: 5672,\n      protocol: 'amqps',\n      name: 'test',\n    }\n  ])],\n})\nexport default class ExecutionModule {\n}\n```\n\n```ts\nimport {Injectable} from '@nestjs/common';\nimport {InjectAmqpConnection} from 'nestjs-amqp';\nimport {Connection} from 'amqplib';\n\n@Injectable()\nexport default class TestService {\n  constructor(\n    @InjectAmqpConnection('test')\n    private readonly connectionTest: Connection, //gets connection with name 'test' defined in module\n    @InjectAmqpConnection(0)\n    private readonly connection0: Connection, //gets first defined connection without a name\n  ) {}\n}\n```\n\u003e Use InjectAmqpConnection without a parameter for default connection\n\n### Example publish \n\n```ts\nimport {Injectable, Logger} from '@nestjs/common';\nimport {InjectAmqpConnection} from 'nestjs-amqp';\nimport {Connection} from 'amqplib';\n\n@Injectable()\nexport default class TestProvider {\n  constructor(\n    @InjectAmqpConnection()\n    private readonly amqp: Connection,\n  ) {}\n  async publish(message: string)  {\n    await this.amqp.createChannel((err, channel) =\u003e {\n      if (err != null) {\n        Logger.alert(err, 'TestProvider');\n      }\n      channel.assertQueue('test_queue_name');\n      channel.sendToQueue('test_queue_name', message);\n    });\n  }\n}\n```\nMore information and examples about amqplib can be found [here](https://www.npmjs.com/package/amqplib).\n\n## Available Options\n\nName | For | Default\n--- | --- | ---\nhostname | The host url for the connection | `localhost`\nport | The port of the amqp host | `5672`\nname | The name of the connection | `default` or the array key index `[0]`\nretrys | The amount of retry attempts before surrender | 3\nretryDelay | The amount of milliseconds to wait before attempting retry | 3000\nprotocol | The protocol for the connection | `amqp`\nusername | The username for the connection | \npassword | The password for the connection |\nlocale | The desired locale for error messages | `en_US`\nframeMax | The size in bytes of the maximum frame allowed over the connection | 0\nheartbeat | The period of the connection heartbeat in seconds | 0\nvhost | What VHost shall be used | `/`\n\n## Testing this package\n\nIn order to test first you need to start the rabbitmq container. We've provided a `docker-compose` file to make this easier.\n\n```bash\n$ docker-compose up -d \n$ yarn test\n```\n\u003e Navigate to localhost:15672 for rabbitmq manager, username and password are both `guest`\n\n\u003e If you're using docker-machine or a VM then change the env for `HOST` in the `.env` file or create one using the provided `.env.dist` file.\n\n## Future implementation\n\n\u003e WARNING: The below examples have not been implemented \n\nSo far this package manages multiple AMQP connections using the nestjs container and injects them into other providers.  \nAlternatively I'd like to implement something like this:\n\n```ts\nimport {Injectable} from '@nestjs/common';\nimport {\n  AmqpConnection,\n  Consume,\n  Publish,\n  Message,\n} from 'nestjs-amqp';\n\n@Injectable()\n@AmqpConnection()\nexport default class MyAmqpService {\n   \n  @Consume(\"queue_name\", {\n    noAck: true,\n  })\n  async listen(@Message message) {\n    console.log('Message received', message);\n    \n    //send a message back\n    this.publish();\n  }\n\n  @Publish(\"queue_name\")\n  async publish() {\n    return \"Send this to 'queue queue_name'\";\n  }\n}\n```\n\nThen using executable context \n\n```ts \n\nimport { NestFactory } from '@nestjs/core';\nimport QueueModule, { MyAmqpService } from './queue';\n\nasync function bootstrap() {\n  const app = await NestFactory.create(QueueModule);\n  const event = app.get(MyAmqpService);\n\n  await event.listen();\n\n}\nbootstrap();\n\nprocess.stdin.resume();\n```\n\nOr something similar to the above is what I'd like to implement\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fnestjsx%2Fnestjs-amqp","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fnestjsx%2Fnestjs-amqp","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fnestjsx%2Fnestjs-amqp/lists"}