├── .gitignore ├── README.md ├── docker-compose.yml ├── golevelup-nextjs ├── .eslintrc.js ├── .prettierrc ├── README.md ├── nest-cli.json ├── package-lock.json ├── package.json ├── src │ ├── app.controller.ts │ ├── app.module.ts │ ├── app.service.ts │ ├── constants.ts │ ├── eventdata.interface.ts │ └── main.ts ├── tsconfig.build.json └── tsconfig.json ├── microservice ├── .eslintrc.js ├── .prettierrc ├── README.md ├── nest-cli.json ├── package-lock.json ├── package.json ├── src │ ├── EventData.interface.ts │ ├── Task.service.ts │ ├── app.controller.spec.ts │ ├── app.controller.ts │ ├── app.module.ts │ ├── constants.ts │ └── main.ts ├── test │ ├── app.e2e-spec.ts │ └── jest-e2e.json ├── tsconfig.build.json └── tsconfig.json ├── nestjs-amqp ├── .eslintrc.js ├── .prettierrc ├── README.md ├── nest-cli.json ├── package-lock.json ├── package.json ├── src │ ├── app.module.ts │ ├── app.service.ts │ ├── constants.ts │ ├── eventdata.interface.ts │ └── main.ts ├── test │ ├── app.e2e-spec.ts │ └── jest-e2e.json ├── tsconfig.build.json └── tsconfig.json └── nestx-amqp ├── .eslintrc.js ├── .prettierrc ├── README.md ├── nest-cli.json ├── package-lock.json ├── package.json ├── src ├── app.module.ts ├── app.service.ts ├── constants.ts ├── eventdata.interface.ts └── main.ts ├── tsconfig.build.json └── tsconfig.json /.gitignore: -------------------------------------------------------------------------------- 1 | # compiled output 2 | dist 3 | node_modules 4 | 5 | # Logs 6 | logs 7 | *.log 8 | npm-debug.log* 9 | yarn-debug.log* 10 | yarn-error.log* 11 | lerna-debug.log* 12 | 13 | # OS 14 | .DS_Store 15 | 16 | # Tests 17 | /coverage 18 | /.nyc_output 19 | 20 | # IDEs and editors 21 | /.idea 22 | .project 23 | .classpath 24 | .c9/ 25 | *.launch 26 | .settings/ 27 | *.sublime-workspace 28 | 29 | # IDE - VSCode 30 | .vscode/* 31 | !.vscode/settings.json 32 | !.vscode/tasks.json 33 | !.vscode/launch.json 34 | !.vscode/extensions.json 35 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | ## rabbitmq-nestjs 2 | 3 | Different approaches to use RabbitMQ with NestJS. 4 | -------------------------------------------------------------------------------- /docker-compose.yml: -------------------------------------------------------------------------------- 1 | version: "3" 2 | 3 | services: 4 | rabbitmq: 5 | image: rabbitmq:3-management 6 | command: rabbitmq-server 7 | ports: 8 | - 5672:5672 9 | - 15672:15672 10 | expose: 11 | - 5672 12 | - 15672 13 | -------------------------------------------------------------------------------- /golevelup-nextjs/.eslintrc.js: -------------------------------------------------------------------------------- 1 | module.exports = { 2 | parser: '@typescript-eslint/parser', 3 | parserOptions: { 4 | project: 'tsconfig.json', 5 | sourceType: 'module', 6 | }, 7 | plugins: ['@typescript-eslint/eslint-plugin'], 8 | extends: [ 9 | 'plugin:@typescript-eslint/eslint-recommended', 10 | 'plugin:@typescript-eslint/recommended', 11 | 'prettier', 12 | 'prettier/@typescript-eslint', 13 | ], 14 | root: true, 15 | env: { 16 | node: true, 17 | jest: true, 18 | }, 19 | rules: { 20 | '@typescript-eslint/interface-name-prefix': 'off', 21 | '@typescript-eslint/explicit-function-return-type': 'off', 22 | '@typescript-eslint/no-explicit-any': 'off', 23 | }, 24 | }; 25 | -------------------------------------------------------------------------------- /golevelup-nextjs/.prettierrc: -------------------------------------------------------------------------------- 1 | { 2 | "singleQuote": true, 3 | "trailingComma": "all" 4 | } -------------------------------------------------------------------------------- /golevelup-nextjs/README.md: -------------------------------------------------------------------------------- 1 | ## nestjs-amqp 2 | 3 | A simple example app using [this library](https://github.com/golevelup/nestjs/tree/master/packages/rabbitmq) 4 | 5 | ### Usage 6 | 7 | * `npm install` 8 | * `docker-compose up -d`. Start RabbitMQ container from root directory 9 | * `npm run start` 10 | -------------------------------------------------------------------------------- /golevelup-nextjs/nest-cli.json: -------------------------------------------------------------------------------- 1 | { 2 | "collection": "@nestjs/schematics", 3 | "sourceRoot": "src" 4 | } 5 | -------------------------------------------------------------------------------- /golevelup-nextjs/package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "golevelup-nextjs", 3 | "version": "0.0.1", 4 | "description": "", 5 | "author": "", 6 | "private": true, 7 | "license": "UNLICENSED", 8 | "scripts": { 9 | "prebuild": "rimraf dist", 10 | "build": "nest build", 11 | "format": "prettier --write \"src/**/*.ts\" \"test/**/*.ts\"", 12 | "start": "nest start", 13 | "start:dev": "nest start --watch", 14 | "start:debug": "nest start --debug --watch", 15 | "start:prod": "node dist/main", 16 | "lint": "eslint \"{src,apps,libs,test}/**/*.ts\" --fix", 17 | "test": "jest", 18 | "test:watch": "jest --watch", 19 | "test:cov": "jest --coverage", 20 | "test:debug": "node --inspect-brk -r tsconfig-paths/register -r ts-node/register node_modules/.bin/jest --runInBand", 21 | "test:e2e": "jest --config ./test/jest-e2e.json" 22 | }, 23 | "dependencies": { 24 | "@golevelup/nestjs-rabbitmq": "^1.15.0", 25 | "@nestjs/common": "^7.0.0", 26 | "@nestjs/core": "^7.0.0", 27 | "@nestjs/platform-express": "^7.0.0", 28 | "@nestjs/schedule": "^0.3.1", 29 | "@types/amqplib": "^0.5.13", 30 | "reflect-metadata": "^0.1.13", 31 | "rimraf": "^3.0.2", 32 | "rxjs": "^6.5.4" 33 | }, 34 | "devDependencies": { 35 | "@nestjs/cli": "^7.0.0", 36 | "@nestjs/schematics": "^7.0.0", 37 | "@nestjs/testing": "^7.0.0", 38 | "@types/express": "^4.17.3", 39 | "@types/jest": "25.1.4", 40 | "@types/node": "^13.9.1", 41 | "@types/supertest": "^2.0.8", 42 | "@typescript-eslint/eslint-plugin": "^2.23.0", 43 | "@typescript-eslint/parser": "^2.23.0", 44 | "eslint": "^6.8.0", 45 | "eslint-config-prettier": "^6.10.0", 46 | "eslint-plugin-import": "^2.20.1", 47 | "jest": "^25.1.0", 48 | "prettier": "^1.19.1", 49 | "supertest": "^4.0.2", 50 | "ts-jest": "25.2.1", 51 | "ts-loader": "^6.2.1", 52 | "ts-node": "^8.6.2", 53 | "tsconfig-paths": "^3.9.0", 54 | "typescript": "^3.7.4" 55 | }, 56 | "jest": { 57 | "moduleFileExtensions": [ 58 | "js", 59 | "json", 60 | "ts" 61 | ], 62 | "rootDir": "src", 63 | "testRegex": ".spec.ts$", 64 | "transform": { 65 | "^.+\\.(t|j)s$": "ts-jest" 66 | }, 67 | "coverageDirectory": "../coverage", 68 | "testEnvironment": "node" 69 | } 70 | } 71 | -------------------------------------------------------------------------------- /golevelup-nextjs/src/app.controller.ts: -------------------------------------------------------------------------------- 1 | import { Controller } from '@nestjs/common'; 2 | 3 | @Controller() 4 | export class AppController { 5 | } 6 | -------------------------------------------------------------------------------- /golevelup-nextjs/src/app.module.ts: -------------------------------------------------------------------------------- 1 | import { Module } from '@nestjs/common'; 2 | import { AppController } from './app.controller'; 3 | import { AppService } from './app.service'; 4 | import { RabbitMQModule } from '@golevelup/nestjs-rabbitmq'; 5 | import { ScheduleModule } from '@nestjs/schedule'; 6 | import { EXCHANGE_NAME, RABBITMQ_URL } from './constants'; 7 | 8 | @Module({ 9 | imports: [ 10 | ScheduleModule.forRoot(), 11 | RabbitMQModule.forRoot(RabbitMQModule, { 12 | exchanges: [ 13 | { 14 | name: EXCHANGE_NAME, 15 | type: 'topic', 16 | }, 17 | ], 18 | uri: RABBITMQ_URL, 19 | }), 20 | ], 21 | controllers: [AppController], 22 | providers: [AppService], 23 | }) 24 | export class AppModule {} 25 | -------------------------------------------------------------------------------- /golevelup-nextjs/src/app.service.ts: -------------------------------------------------------------------------------- 1 | import { Injectable } from '@nestjs/common'; 2 | import { AmqpConnection, RabbitSubscribe } from '@golevelup/nestjs-rabbitmq'; 3 | import { Cron, CronExpression } from '@nestjs/schedule'; 4 | import { EXCHANGE_NAME, QUEUE_NAME, ROUTING_KEY } from './constants'; 5 | import { EventData } from './eventdata.interface'; 6 | 7 | @Injectable() 8 | export class AppService { 9 | 10 | private count: number 11 | 12 | constructor(private readonly amqpConnection: AmqpConnection) { 13 | this.count = 0 14 | } 15 | 16 | @RabbitSubscribe({ 17 | exchange: EXCHANGE_NAME, 18 | routingKey: ROUTING_KEY, 19 | queue: QUEUE_NAME, 20 | }) 21 | public async pubSubHandler(msg: EventData) { 22 | console.log(`Received message: ${JSON.stringify(msg)}`); 23 | } 24 | 25 | @Cron(CronExpression.EVERY_SECOND) 26 | async cron() { 27 | const message: EventData = {data: {counter: this.count++}} 28 | console.log(`sending message: ${JSON.stringify(message)}`) 29 | await this.amqpConnection.publish(EXCHANGE_NAME, ROUTING_KEY, message) 30 | } 31 | 32 | } 33 | -------------------------------------------------------------------------------- /golevelup-nextjs/src/constants.ts: -------------------------------------------------------------------------------- 1 | export const RABBITMQ_URL = "amqp://localhost:5672" 2 | export const RABBITMQ_QUEUE_NAME = "test-queue" 3 | export const EXCHANGE_NAME = "test-exchange" 4 | export const ROUTING_KEY = "test-routing-key" 5 | export const QUEUE_NAME = "test-queue-golevelup" 6 | -------------------------------------------------------------------------------- /golevelup-nextjs/src/eventdata.interface.ts: -------------------------------------------------------------------------------- 1 | export interface EventData { 2 | // pattern: string, 3 | data: { 4 | counter: number 5 | }; 6 | } 7 | -------------------------------------------------------------------------------- /golevelup-nextjs/src/main.ts: -------------------------------------------------------------------------------- 1 | import { NestFactory } from '@nestjs/core'; 2 | import { AppModule } from './app.module'; 3 | 4 | async function bootstrap() { 5 | const app = await NestFactory.create(AppModule); 6 | await app.listen(3000); 7 | } 8 | bootstrap(); 9 | -------------------------------------------------------------------------------- /golevelup-nextjs/tsconfig.build.json: -------------------------------------------------------------------------------- 1 | { 2 | "extends": "./tsconfig.json", 3 | "exclude": ["node_modules", "test", "dist", "**/*spec.ts"] 4 | } 5 | -------------------------------------------------------------------------------- /golevelup-nextjs/tsconfig.json: -------------------------------------------------------------------------------- 1 | { 2 | "compilerOptions": { 3 | "module": "commonjs", 4 | "declaration": true, 5 | "removeComments": true, 6 | "emitDecoratorMetadata": true, 7 | "experimentalDecorators": true, 8 | "target": "es2017", 9 | "sourceMap": true, 10 | "outDir": "./dist", 11 | "baseUrl": "./", 12 | "incremental": true 13 | }, 14 | "exclude": ["node_modules", "dist"] 15 | } 16 | -------------------------------------------------------------------------------- /microservice/.eslintrc.js: -------------------------------------------------------------------------------- 1 | module.exports = { 2 | parser: '@typescript-eslint/parser', 3 | parserOptions: { 4 | project: 'tsconfig.json', 5 | sourceType: 'module', 6 | }, 7 | plugins: ['@typescript-eslint/eslint-plugin'], 8 | extends: [ 9 | 'plugin:@typescript-eslint/eslint-recommended', 10 | 'plugin:@typescript-eslint/recommended', 11 | 'prettier', 12 | 'prettier/@typescript-eslint', 13 | ], 14 | root: true, 15 | env: { 16 | node: true, 17 | jest: true, 18 | }, 19 | rules: { 20 | '@typescript-eslint/interface-name-prefix': 'off', 21 | '@typescript-eslint/explicit-function-return-type': 'off', 22 | '@typescript-eslint/no-explicit-any': 'off', 23 | }, 24 | }; 25 | -------------------------------------------------------------------------------- /microservice/.prettierrc: -------------------------------------------------------------------------------- 1 | { 2 | "singleQuote": true, 3 | "trailingComma": "all" 4 | } -------------------------------------------------------------------------------- /microservice/README.md: -------------------------------------------------------------------------------- 1 | ## microservices 2 | 3 | A simple hybrid example app with [RabbitMQ based microservice](https://docs.nestjs.com/microservices/rabbitmq) in Nestjs 4 | 5 | ### Usage 6 | 7 | * `npm install` 8 | * `docker-compose up -d`. Start RabbitMQ container from root directory 9 | * `npm run start` 10 | 11 | -------------------------------------------------------------------------------- /microservice/nest-cli.json: -------------------------------------------------------------------------------- 1 | { 2 | "collection": "@nestjs/schematics", 3 | "sourceRoot": "src" 4 | } 5 | -------------------------------------------------------------------------------- /microservice/package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "rabbitmq-nestjs", 3 | "version": "0.0.1", 4 | "description": "", 5 | "author": "", 6 | "private": true, 7 | "license": "UNLICENSED", 8 | "scripts": { 9 | "prebuild": "rimraf dist", 10 | "build": "nest build", 11 | "format": "prettier --write \"src/**/*.ts\" \"test/**/*.ts\"", 12 | "start": "nest start", 13 | "start:dev": "nest start --watch", 14 | "start:debug": "nest start --debug --watch", 15 | "start:prod": "node dist/main", 16 | "lint": "eslint \"{src,apps,libs,test}/**/*.ts\" --fix", 17 | "test": "jest", 18 | "test:watch": "jest --watch", 19 | "test:cov": "jest --coverage", 20 | "test:debug": "node --inspect-brk -r tsconfig-paths/register -r ts-node/register node_modules/.bin/jest --runInBand", 21 | "test:e2e": "jest --config ./test/jest-e2e.json" 22 | }, 23 | "dependencies": { 24 | "@nestjs/common": "^7.0.0", 25 | "@nestjs/core": "^7.0.0", 26 | "@nestjs/microservices": "^7.0.8", 27 | "@nestjs/platform-express": "^7.0.0", 28 | "@nestjs/schedule": "^0.3.0", 29 | "amqp-connection-manager": "^3.2.0", 30 | "amqplib": "^0.5.5", 31 | "reflect-metadata": "^0.1.13", 32 | "rimraf": "^3.0.2", 33 | "rxjs": "^6.5.4" 34 | }, 35 | "devDependencies": { 36 | "@nestjs/cli": "^7.0.0", 37 | "@nestjs/schematics": "^7.0.0", 38 | "@nestjs/testing": "^7.0.0", 39 | "@types/express": "^4.17.3", 40 | "@types/jest": "25.1.4", 41 | "@types/node": "^13.9.1", 42 | "@types/supertest": "^2.0.8", 43 | "@typescript-eslint/eslint-plugin": "^2.23.0", 44 | "@typescript-eslint/parser": "^2.23.0", 45 | "eslint": "^6.8.0", 46 | "eslint-config-prettier": "^6.10.0", 47 | "eslint-plugin-import": "^2.20.1", 48 | "jest": "^25.1.0", 49 | "prettier": "^1.19.1", 50 | "supertest": "^4.0.2", 51 | "ts-jest": "25.2.1", 52 | "ts-loader": "^6.2.1", 53 | "ts-node": "^8.6.2", 54 | "tsconfig-paths": "^3.9.0", 55 | "typescript": "^3.7.4" 56 | }, 57 | "jest": { 58 | "moduleFileExtensions": [ 59 | "js", 60 | "json", 61 | "ts" 62 | ], 63 | "rootDir": "src", 64 | "testRegex": ".spec.ts$", 65 | "transform": { 66 | "^.+\\.(t|j)s$": "ts-jest" 67 | }, 68 | "coverageDirectory": "../coverage", 69 | "testEnvironment": "node" 70 | } 71 | } 72 | -------------------------------------------------------------------------------- /microservice/src/EventData.interface.ts: -------------------------------------------------------------------------------- 1 | export interface EventData { 2 | // pattern: string, 3 | data: { 4 | counter: number 5 | }; 6 | } 7 | -------------------------------------------------------------------------------- /microservice/src/Task.service.ts: -------------------------------------------------------------------------------- 1 | import { Inject, Injectable } from '@nestjs/common'; 2 | import { ClientProxy } from '@nestjs/microservices'; 3 | import { Cron, CronExpression } from '@nestjs/schedule'; 4 | import { EventData } from './EventData.interface'; 5 | import { RABBIT_TEST_PATTERN } from './constants'; 6 | 7 | @Injectable() 8 | export class TaskService { 9 | 10 | private count: number; 11 | 12 | constructor(@Inject("RabbitClient") private rabbitClient: ClientProxy) { 13 | this.count = 0; 14 | } 15 | 16 | @Cron(CronExpression.EVERY_SECOND) 17 | cron() { 18 | const data: EventData = {data: {counter: this.count++}}; 19 | console.log(`sending count: ${data.data.counter}`) 20 | return this.rabbitClient.emit(RABBIT_TEST_PATTERN, data); 21 | } 22 | 23 | } 24 | -------------------------------------------------------------------------------- /microservice/src/app.controller.spec.ts: -------------------------------------------------------------------------------- 1 | import { Test, TestingModule } from '@nestjs/testing'; 2 | import { AppController } from './app.controller'; 3 | import { TaskService } from './Task.service'; 4 | 5 | describe('AppController', () => { 6 | let appController: AppController; 7 | 8 | beforeEach(async () => { 9 | const app: TestingModule = await Test.createTestingModule({ 10 | controllers: [AppController], 11 | providers: [TaskService], 12 | }).compile(); 13 | 14 | appController = app.get(AppController); 15 | }); 16 | 17 | describe('root', () => { 18 | it('should return "Hello World!"', () => { 19 | expect(appController.getHello()).toBe('Hello World!'); 20 | }); 21 | }); 22 | }); 23 | -------------------------------------------------------------------------------- /microservice/src/app.controller.ts: -------------------------------------------------------------------------------- 1 | import { Controller, Get } from '@nestjs/common'; 2 | import { Ctx, EventPattern, Payload, RmqContext } from '@nestjs/microservices'; 3 | import { EventData } from './EventData.interface'; 4 | import { RABBIT_TEST_PATTERN } from './constants'; 5 | 6 | @Controller('/send') 7 | export class AppController { 8 | constructor() {} 9 | 10 | @Get() 11 | getHello(): string { 12 | return 'hello' 13 | } 14 | 15 | @EventPattern(RABBIT_TEST_PATTERN) 16 | consume(@Payload() { data }: EventData, @Ctx() context: RmqContext) { 17 | console.log(`consuming data: ${data.counter}`); 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /microservice/src/app.module.ts: -------------------------------------------------------------------------------- 1 | import { Module } from '@nestjs/common'; 2 | import { AppController } from './app.controller'; 3 | import { TaskService } from './Task.service'; 4 | import { ClientsModule, Transport } from '@nestjs/microservices'; 5 | import { ScheduleModule } from '@nestjs/schedule'; 6 | import { RABBITMQ_QUEUE_NAME, RABBITMQ_URL } from './constants'; 7 | 8 | @Module({ 9 | imports: [ 10 | ScheduleModule.forRoot(), 11 | ClientsModule.register([ 12 | { 13 | name: 'RabbitClient', 14 | transport: Transport.RMQ, 15 | options: { 16 | urls: [RABBITMQ_URL], 17 | queue: RABBITMQ_QUEUE_NAME, 18 | queueOptions: { 19 | durable: false 20 | } 21 | } 22 | } 23 | ])], 24 | controllers: [AppController], 25 | providers: [TaskService], 26 | }) 27 | export class AppModule {} 28 | -------------------------------------------------------------------------------- /microservice/src/constants.ts: -------------------------------------------------------------------------------- 1 | export const RABBIT_TEST_PATTERN = "testPattern" 2 | export const RABBITMQ_URL = "amqp://localhost:5672" 3 | export const RABBITMQ_QUEUE_NAME = "test-queue" 4 | -------------------------------------------------------------------------------- /microservice/src/main.ts: -------------------------------------------------------------------------------- 1 | import { NestFactory } from '@nestjs/core'; 2 | import { AppModule } from './app.module'; 3 | import { MicroserviceOptions, Transport } from '@nestjs/microservices'; 4 | import { RABBITMQ_QUEUE_NAME, RABBITMQ_URL } from './constants'; 5 | 6 | async function bootstrap() { 7 | const app = await NestFactory.create(AppModule); 8 | const rabbitMq = app.connectMicroservice({ 9 | transport: Transport.RMQ, 10 | options: { 11 | urls: [RABBITMQ_URL], 12 | queue: RABBITMQ_QUEUE_NAME, 13 | queueOptions: { 14 | durable: false 15 | } 16 | } 17 | }); 18 | 19 | await app.startAllMicroservicesAsync(); 20 | await app.listen(3000); 21 | } 22 | bootstrap(); 23 | -------------------------------------------------------------------------------- /microservice/test/app.e2e-spec.ts: -------------------------------------------------------------------------------- 1 | import { Test, TestingModule } from '@nestjs/testing'; 2 | import { INestApplication } from '@nestjs/common'; 3 | import * as request from 'supertest'; 4 | import { AppModule } from './../src/app.module'; 5 | 6 | describe('AppController (e2e)', () => { 7 | let app: INestApplication; 8 | 9 | beforeEach(async () => { 10 | const moduleFixture: TestingModule = await Test.createTestingModule({ 11 | imports: [AppModule], 12 | }).compile(); 13 | 14 | app = moduleFixture.createNestApplication(); 15 | await app.init(); 16 | }); 17 | 18 | it('/ (GET)', () => { 19 | return request(app.getHttpServer()) 20 | .get('/') 21 | .expect(200) 22 | .expect('Hello World!'); 23 | }); 24 | }); 25 | -------------------------------------------------------------------------------- /microservice/test/jest-e2e.json: -------------------------------------------------------------------------------- 1 | { 2 | "moduleFileExtensions": ["js", "json", "ts"], 3 | "rootDir": ".", 4 | "testEnvironment": "node", 5 | "testRegex": ".e2e-spec.ts$", 6 | "transform": { 7 | "^.+\\.(t|j)s$": "ts-jest" 8 | } 9 | } 10 | -------------------------------------------------------------------------------- /microservice/tsconfig.build.json: -------------------------------------------------------------------------------- 1 | { 2 | "extends": "./tsconfig.json", 3 | "exclude": ["node_modules", "test", "dist", "**/*spec.ts"] 4 | } 5 | -------------------------------------------------------------------------------- /microservice/tsconfig.json: -------------------------------------------------------------------------------- 1 | { 2 | "compilerOptions": { 3 | "module": "commonjs", 4 | "declaration": true, 5 | "removeComments": true, 6 | "emitDecoratorMetadata": true, 7 | "experimentalDecorators": true, 8 | "target": "es2017", 9 | "sourceMap": true, 10 | "outDir": "./dist", 11 | "baseUrl": "./", 12 | "incremental": true 13 | }, 14 | "exclude": ["node_modules", "dist"] 15 | } 16 | -------------------------------------------------------------------------------- /nestjs-amqp/.eslintrc.js: -------------------------------------------------------------------------------- 1 | module.exports = { 2 | parser: '@typescript-eslint/parser', 3 | parserOptions: { 4 | project: 'tsconfig.json', 5 | sourceType: 'module', 6 | }, 7 | plugins: ['@typescript-eslint/eslint-plugin'], 8 | extends: [ 9 | 'plugin:@typescript-eslint/eslint-recommended', 10 | 'plugin:@typescript-eslint/recommended', 11 | 'prettier', 12 | 'prettier/@typescript-eslint', 13 | ], 14 | root: true, 15 | env: { 16 | node: true, 17 | jest: true, 18 | }, 19 | rules: { 20 | '@typescript-eslint/interface-name-prefix': 'off', 21 | '@typescript-eslint/explicit-function-return-type': 'off', 22 | '@typescript-eslint/no-explicit-any': 'off', 23 | }, 24 | }; 25 | -------------------------------------------------------------------------------- /nestjs-amqp/.prettierrc: -------------------------------------------------------------------------------- 1 | { 2 | "singleQuote": true, 3 | "trailingComma": "all" 4 | } -------------------------------------------------------------------------------- /nestjs-amqp/README.md: -------------------------------------------------------------------------------- 1 | ## nestjs-amqp 2 | 3 | A simple example app using [nestjs-amqp](https://github.com/nestjsx/nestjs-amqp) library 4 | 5 | ### Usage 6 | 7 | * `npm install` 8 | * `docker-compose up -d`. Start RabbitMQ container from root directory 9 | * `npm run start` 10 | 11 | -------------------------------------------------------------------------------- /nestjs-amqp/nest-cli.json: -------------------------------------------------------------------------------- 1 | { 2 | "collection": "@nestjs/schematics", 3 | "sourceRoot": "src" 4 | } 5 | -------------------------------------------------------------------------------- /nestjs-amqp/package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "nestjs-amqp-poc", 3 | "version": "0.0.1", 4 | "description": "", 5 | "author": "", 6 | "private": true, 7 | "license": "UNLICENSED", 8 | "scripts": { 9 | "prebuild": "rimraf dist", 10 | "build": "nest build", 11 | "format": "prettier --write \"src/**/*.ts\" \"test/**/*.ts\"", 12 | "start": "nest start", 13 | "start:dev": "nest start --watch", 14 | "start:debug": "nest start --debug --watch", 15 | "start:prod": "node dist/main", 16 | "lint": "eslint \"{src,apps,libs,test}/**/*.ts\" --fix", 17 | "test": "jest", 18 | "test:watch": "jest --watch", 19 | "test:cov": "jest --coverage", 20 | "test:debug": "node --inspect-brk -r tsconfig-paths/register -r ts-node/register node_modules/.bin/jest --runInBand", 21 | "test:e2e": "jest --config ./test/jest-e2e.json" 22 | }, 23 | "dependencies": { 24 | "@nestjs/common": "^7.0.0", 25 | "@nestjs/core": "^7.0.0", 26 | "@nestjs/platform-express": "^7.0.0", 27 | "@nestjs/schedule": "^0.3.1", 28 | "@types/amqplib": "^0.5.13", 29 | "nestjs-amqp": "^0.1.9", 30 | "reflect-metadata": "^0.1.13", 31 | "rimraf": "^3.0.2", 32 | "rxjs": "^6.5.4" 33 | }, 34 | "devDependencies": { 35 | "@nestjs/cli": "^7.0.0", 36 | "@nestjs/schematics": "^7.0.0", 37 | "@nestjs/testing": "^7.0.0", 38 | "@types/express": "^4.17.3", 39 | "@types/jest": "25.1.4", 40 | "@types/node": "^13.9.1", 41 | "@types/supertest": "^2.0.8", 42 | "@typescript-eslint/eslint-plugin": "^2.23.0", 43 | "@typescript-eslint/parser": "^2.23.0", 44 | "eslint": "^6.8.0", 45 | "eslint-config-prettier": "^6.10.0", 46 | "eslint-plugin-import": "^2.20.1", 47 | "jest": "^25.1.0", 48 | "prettier": "^1.19.1", 49 | "supertest": "^4.0.2", 50 | "ts-jest": "25.2.1", 51 | "ts-loader": "^6.2.1", 52 | "ts-node": "^8.6.2", 53 | "tsconfig-paths": "^3.9.0", 54 | "typescript": "^3.7.4" 55 | }, 56 | "jest": { 57 | "moduleFileExtensions": [ 58 | "js", 59 | "json", 60 | "ts" 61 | ], 62 | "rootDir": "src", 63 | "testRegex": ".spec.ts$", 64 | "transform": { 65 | "^.+\\.(t|j)s$": "ts-jest" 66 | }, 67 | "coverageDirectory": "../coverage", 68 | "testEnvironment": "node" 69 | } 70 | } 71 | -------------------------------------------------------------------------------- /nestjs-amqp/src/app.module.ts: -------------------------------------------------------------------------------- 1 | import { Module } from '@nestjs/common'; 2 | import { AppService } from './app.service'; 3 | import { AmqpModule } from 'nestjs-amqp'; 4 | import { ScheduleModule } from '@nestjs/schedule'; 5 | 6 | @Module({ 7 | imports: [ 8 | ScheduleModule.forRoot(), 9 | AmqpModule.forRoot({ 10 | name: 'rabbitmq', 11 | hostname: 'localhost', 12 | port: 5672, 13 | username: 'guest', 14 | password: 'guest', 15 | })], 16 | providers: [AppService], 17 | }) 18 | export class AppModule {} 19 | -------------------------------------------------------------------------------- /nestjs-amqp/src/app.service.ts: -------------------------------------------------------------------------------- 1 | import { Injectable } from '@nestjs/common'; 2 | import { InjectAmqpConnection } from 'nestjs-amqp'; 3 | import { RABBITMQ_QUEUE_NAME } from './constants'; 4 | import { Cron, CronExpression, Timeout } from '@nestjs/schedule'; 5 | import { EventData } from './eventdata.interface'; 6 | import { json } from 'express'; 7 | 8 | @Injectable() 9 | export class AppService { 10 | 11 | private count: number 12 | 13 | constructor(@InjectAmqpConnection('rabbitmq') private readonly rabbitConnection) { 14 | this.count = 0 15 | } 16 | 17 | async publish(message: EventData) { 18 | try { 19 | const channel = await this.rabbitConnection.createChannel(); 20 | await channel.assertQueue(RABBITMQ_QUEUE_NAME); 21 | const jsonMessage = JSON.stringify(message) 22 | console.log(`sending message: ${jsonMessage}`) 23 | channel.sendToQueue(RABBITMQ_QUEUE_NAME, Buffer.from(jsonMessage, "utf8")); 24 | } catch (e) { 25 | console.log(`publish error: ${e}`) 26 | } 27 | } 28 | 29 | @Cron(CronExpression.EVERY_SECOND) 30 | async publishCron() { 31 | const message: EventData= {data: {counter: this.count++}} 32 | await this.publish(message) 33 | } 34 | 35 | @Timeout(10000) 36 | async consumeCron() { 37 | try { 38 | const channel = await this.rabbitConnection.createChannel(); 39 | await channel.assertQueue(RABBITMQ_QUEUE_NAME); 40 | await channel.consume(RABBITMQ_QUEUE_NAME, (message ) => { 41 | console.log(`consuming data: ${message.content.toString("utf8")}`) 42 | }) 43 | } catch (e) { 44 | console.log(`consume error: ${e}`) 45 | } 46 | } 47 | } 48 | -------------------------------------------------------------------------------- /nestjs-amqp/src/constants.ts: -------------------------------------------------------------------------------- 1 | export const RABBITMQ_QUEUE_NAME = "test-queue-nestjs-amqp" 2 | -------------------------------------------------------------------------------- /nestjs-amqp/src/eventdata.interface.ts: -------------------------------------------------------------------------------- 1 | export interface EventData { 2 | // pattern: string, 3 | data: { 4 | counter: number 5 | }; 6 | } 7 | -------------------------------------------------------------------------------- /nestjs-amqp/src/main.ts: -------------------------------------------------------------------------------- 1 | import { NestFactory } from '@nestjs/core'; 2 | import { AppModule } from './app.module'; 3 | 4 | async function bootstrap() { 5 | const app = await NestFactory.create(AppModule); 6 | await app.listen(3000); 7 | } 8 | bootstrap(); 9 | -------------------------------------------------------------------------------- /nestjs-amqp/test/app.e2e-spec.ts: -------------------------------------------------------------------------------- 1 | import { Test, TestingModule } from '@nestjs/testing'; 2 | import { INestApplication } from '@nestjs/common'; 3 | import * as request from 'supertest'; 4 | import { AppModule } from './../src/app.module'; 5 | 6 | describe('AppController (e2e)', () => { 7 | let app: INestApplication; 8 | 9 | beforeEach(async () => { 10 | const moduleFixture: TestingModule = await Test.createTestingModule({ 11 | imports: [AppModule], 12 | }).compile(); 13 | 14 | app = moduleFixture.createNestApplication(); 15 | await app.init(); 16 | }); 17 | 18 | it('/ (GET)', () => { 19 | return request(app.getHttpServer()) 20 | .get('/') 21 | .expect(200) 22 | .expect('Hello World!'); 23 | }); 24 | }); 25 | -------------------------------------------------------------------------------- /nestjs-amqp/test/jest-e2e.json: -------------------------------------------------------------------------------- 1 | { 2 | "moduleFileExtensions": ["js", "json", "ts"], 3 | "rootDir": ".", 4 | "testEnvironment": "node", 5 | "testRegex": ".e2e-spec.ts$", 6 | "transform": { 7 | "^.+\\.(t|j)s$": "ts-jest" 8 | } 9 | } 10 | -------------------------------------------------------------------------------- /nestjs-amqp/tsconfig.build.json: -------------------------------------------------------------------------------- 1 | { 2 | "extends": "./tsconfig.json", 3 | "exclude": ["node_modules", "test", "dist", "**/*spec.ts"] 4 | } 5 | -------------------------------------------------------------------------------- /nestjs-amqp/tsconfig.json: -------------------------------------------------------------------------------- 1 | { 2 | "compilerOptions": { 3 | "module": "commonjs", 4 | "declaration": true, 5 | "removeComments": true, 6 | "emitDecoratorMetadata": true, 7 | "experimentalDecorators": true, 8 | "target": "es2017", 9 | "sourceMap": true, 10 | "outDir": "./dist", 11 | "baseUrl": "./", 12 | "incremental": true 13 | }, 14 | "exclude": ["node_modules", "dist"] 15 | } 16 | -------------------------------------------------------------------------------- /nestx-amqp/.eslintrc.js: -------------------------------------------------------------------------------- 1 | module.exports = { 2 | parser: '@typescript-eslint/parser', 3 | parserOptions: { 4 | project: 'tsconfig.json', 5 | sourceType: 'module', 6 | }, 7 | plugins: ['@typescript-eslint/eslint-plugin'], 8 | extends: [ 9 | 'plugin:@typescript-eslint/eslint-recommended', 10 | 'plugin:@typescript-eslint/recommended', 11 | 'prettier', 12 | 'prettier/@typescript-eslint', 13 | ], 14 | root: true, 15 | env: { 16 | node: true, 17 | jest: true, 18 | }, 19 | rules: { 20 | '@typescript-eslint/interface-name-prefix': 'off', 21 | '@typescript-eslint/explicit-function-return-type': 'off', 22 | '@typescript-eslint/no-explicit-any': 'off', 23 | }, 24 | }; 25 | -------------------------------------------------------------------------------- /nestx-amqp/.prettierrc: -------------------------------------------------------------------------------- 1 | { 2 | "singleQuote": true, 3 | "trailingComma": "all" 4 | } -------------------------------------------------------------------------------- /nestx-amqp/README.md: -------------------------------------------------------------------------------- 1 | ## nestx-amqp 2 | 3 | A simple example app using [nestx-amqp](https://github.com/nest-x/nestx-amqp) library 4 | 5 | ### Usage 6 | 7 | * `npm install` 8 | * `docker-compose up -d`. Start RabbitMQ container from root directory 9 | * `npm run start` 10 | 11 | -------------------------------------------------------------------------------- /nestx-amqp/nest-cli.json: -------------------------------------------------------------------------------- 1 | { 2 | "collection": "@nestjs/schematics", 3 | "sourceRoot": "src" 4 | } 5 | -------------------------------------------------------------------------------- /nestx-amqp/package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "nestx-amqp-poc", 3 | "version": "0.0.1", 4 | "description": "", 5 | "author": "", 6 | "private": true, 7 | "license": "UNLICENSED", 8 | "scripts": { 9 | "prebuild": "rimraf dist", 10 | "build": "nest build", 11 | "format": "prettier --write \"src/**/*.ts\" \"test/**/*.ts\"", 12 | "start": "nest start", 13 | "start:dev": "nest start --watch", 14 | "start:debug": "nest start --debug --watch", 15 | "start:prod": "node dist/main", 16 | "lint": "eslint \"{src,apps,libs,test}/**/*.ts\" --fix", 17 | "test": "jest", 18 | "test:watch": "jest --watch", 19 | "test:cov": "jest --coverage", 20 | "test:debug": "node --inspect-brk -r tsconfig-paths/register -r ts-node/register node_modules/.bin/jest --runInBand", 21 | "test:e2e": "jest --config ./test/jest-e2e.json" 22 | }, 23 | "dependencies": { 24 | "@nestjs/common": "^7.0.0", 25 | "@nestjs/core": "^7.0.0", 26 | "@nestjs/platform-express": "^7.0.0", 27 | "@nestjs/schedule": "^0.3.1", 28 | "@types/amqplib": "^0.5.13", 29 | "nestx-amqp": "^1.3.1", 30 | "reflect-metadata": "^0.1.13", 31 | "rimraf": "^3.0.2", 32 | "rxjs": "^6.5.4" 33 | }, 34 | "devDependencies": { 35 | "@nestjs/cli": "^7.0.0", 36 | "@nestjs/schematics": "^7.0.0", 37 | "@nestjs/testing": "^7.0.0", 38 | "@types/express": "^4.17.3", 39 | "@types/jest": "25.1.4", 40 | "@types/node": "^13.9.1", 41 | "@types/supertest": "^2.0.8", 42 | "@typescript-eslint/eslint-plugin": "^2.23.0", 43 | "@typescript-eslint/parser": "^2.23.0", 44 | "eslint": "^6.8.0", 45 | "eslint-config-prettier": "^6.10.0", 46 | "eslint-plugin-import": "^2.20.1", 47 | "jest": "^25.1.0", 48 | "prettier": "^1.19.1", 49 | "supertest": "^4.0.2", 50 | "ts-jest": "25.2.1", 51 | "ts-loader": "^6.2.1", 52 | "ts-node": "^8.6.2", 53 | "tsconfig-paths": "^3.9.0", 54 | "typescript": "^3.7.4" 55 | }, 56 | "jest": { 57 | "moduleFileExtensions": [ 58 | "js", 59 | "json", 60 | "ts" 61 | ], 62 | "rootDir": "src", 63 | "testRegex": ".spec.ts$", 64 | "transform": { 65 | "^.+\\.(t|j)s$": "ts-jest" 66 | }, 67 | "coverageDirectory": "../coverage", 68 | "testEnvironment": "node" 69 | } 70 | } 71 | -------------------------------------------------------------------------------- /nestx-amqp/src/app.module.ts: -------------------------------------------------------------------------------- 1 | import { Module } from '@nestjs/common'; 2 | import { AppService } from './app.service'; 3 | import { AMQPModule } from 'nestx-amqp'; 4 | import { ScheduleModule } from '@nestjs/schedule'; 5 | import { RABBITMQ_URL } from './constants'; 6 | 7 | @Module({ 8 | imports: [ 9 | ScheduleModule.forRoot(), 10 | AMQPModule.forRootAsync({ 11 | useFactory: () => ({ 12 | urls: [RABBITMQ_URL], 13 | }), 14 | }), 15 | ], 16 | providers: [AppService], 17 | }) 18 | export class AppModule {} 19 | -------------------------------------------------------------------------------- /nestx-amqp/src/app.service.ts: -------------------------------------------------------------------------------- 1 | import { Injectable } from '@nestjs/common'; 2 | import { PublishQueue, SubscribeQueue } from 'nestx-amqp'; 3 | import { QUEUE_NAME } from './constants'; 4 | import { Cron, CronExpression } from '@nestjs/schedule'; 5 | import { EventData } from './eventdata.interface'; 6 | 7 | @Injectable() 8 | export class AppService { 9 | 10 | private count: number 11 | 12 | constructor() { 13 | this.count = 0; 14 | } 15 | 16 | @PublishQueue(QUEUE_NAME) 17 | async testPublishQueue(content: EventData) { 18 | console.log(`publishing message ${JSON.stringify(content)}`) 19 | } 20 | 21 | @Cron(CronExpression.EVERY_SECOND) 22 | async cron() { 23 | const data: EventData = {data: {counter: this.count++}} 24 | await this.testPublishQueue(data) 25 | } 26 | 27 | @SubscribeQueue(QUEUE_NAME) 28 | yourSubscribeQueueMethod(content: EventData){ 29 | console.log(`consuming message: ${JSON.stringify(content)}`) 30 | } 31 | 32 | } 33 | -------------------------------------------------------------------------------- /nestx-amqp/src/constants.ts: -------------------------------------------------------------------------------- 1 | export const RABBITMQ_URL = "amqp://localhost:5672" 2 | export const RABBITMQ_QUEUE_NAME = "test-queue" 3 | export const QUEUE_NAME = "test-queue-nestx-amqp" 4 | -------------------------------------------------------------------------------- /nestx-amqp/src/eventdata.interface.ts: -------------------------------------------------------------------------------- 1 | export interface EventData { 2 | // pattern: string, 3 | data: { 4 | counter: number 5 | }; 6 | } 7 | -------------------------------------------------------------------------------- /nestx-amqp/src/main.ts: -------------------------------------------------------------------------------- 1 | import { NestFactory } from '@nestjs/core'; 2 | import { AppModule } from './app.module'; 3 | 4 | async function bootstrap() { 5 | const app = await NestFactory.create(AppModule); 6 | await app.listen(3000); 7 | } 8 | bootstrap(); 9 | -------------------------------------------------------------------------------- /nestx-amqp/tsconfig.build.json: -------------------------------------------------------------------------------- 1 | { 2 | "extends": "./tsconfig.json", 3 | "exclude": ["node_modules", "test", "dist", "**/*spec.ts"] 4 | } 5 | -------------------------------------------------------------------------------- /nestx-amqp/tsconfig.json: -------------------------------------------------------------------------------- 1 | { 2 | "compilerOptions": { 3 | "module": "commonjs", 4 | "declaration": true, 5 | "removeComments": true, 6 | "emitDecoratorMetadata": true, 7 | "experimentalDecorators": true, 8 | "target": "es2017", 9 | "sourceMap": true, 10 | "outDir": "./dist", 11 | "baseUrl": "./", 12 | "incremental": true 13 | }, 14 | "exclude": ["node_modules", "dist"] 15 | } 16 | --------------------------------------------------------------------------------