npm.io
3.0.3 • Published 2 years ago

adonis6-amqp

Licence
MIT
Version
3.0.3
Deps
2
Size
34 kB
Vulns
0
Weekly
0
DeprecatedThis package is deprecated

adonis6-amqp


npm-image license-image

Introduction

Typesafe AMQP provider for AdonisJS 6. Feel free to contribute or give suggestions.

Quick setup

Install the library
npm i adonis6-amqp
yarn add adonis6-amqp
pnpm add adonis6-amqp
Configure
node ace configure adonis6-amqp

Setup

Once installed, you can configure Adonis6 AMQP in your application inside config/amqp.ts.

const amqpConfig = defineConfig({
  connection: {
    host: env.get('AMQP_HOST'),
    port: env.get('AMQP_PORT'),
    user: env.get('AMQP_USER'),
    password: env.get('AMQP_PASSWORD'),
  }
})

declare module 'adonis6-amqp' {
  interface AmqpQueues {
    // 'your.queue': Infer<typeof yourValidator>
  }
}
Create a queue

Define your queues inside start/amqp.ts.

import { amqp } from 'adonis6-amqp'

export const yourQueue = await amqp
  .createQueue('your.queue', yourValidator)
  .useHandler(YourHandler) // Optional
  .register()
Validator

Each queues need a validator to validate incoming/outgoing data (you must use vinejs to create queue validators).

import vine from '@vinejs/vine'

export const yourValidator = vine.compile(
  vine.object({
    randomString: vine.string(),
  })
)
Handler

If you want to listen on the queue you need to create a queue handler.

import { AmqpQueues, QueueHandler } from 'adonis6-amqp'

export class YourHandler extends QueueHandler {
  handle(data: AmqpQueues['your.queue']) { }
  handleError(error: Error) { }
}
Send message to queue

Just import the queue you registered where you want to send a message.

import { yourQueue } from '#start/amqp'

// Typesafe
yourQueue.sendMessage({ randomString: 'test' })

License

Adonis6 AMQP is open-sourced software licensed under the MIT license.

Keywords