1.0.3 • Published 2 years ago

deep-rabbit-handler v1.0.3

Weekly downloads
-
License
ISC
Repository
-
Last release
2 years ago

Deep Instinct rabbitMQ Handler

How to use

Sending a message

const { queueMessage, createPublisher } = require('deep-rabbit-handler')
const publishMsg = createPublisher(qHost, qUser, qPassword, qPort)

function sendMessage(message){
    const q = await publisher
    const qMsg = await q.send(qName, messageObj)
    
    if (qMsg !== messages.MESSAGE_SENT) {
        res.status(500)
    }

    res.send(qMsg)
}

Listen to messages

const { queueMessage, createConsumer } = require('deep-rabbit-handler')
const { MESSAGE_CALLBACK_VALIDATION_ERROR } = queueMessages

createConsumer(qHost, qUser, qPassword, qPort).then((consumer: ISubscriber) => {
  new AuditLogProcessor(consumer, saveMessageInDb)
})

class messageProcessor {
  constructor(private sub: ISubscriber, private actionOnMessage: any) {
    sub.subscribe(qName as string, this.receiveMessage.bind(this))
  }

  async parseMessage(msg: string) {
    try {
      const msgParse: queueMessage = JSON.parse(msg)

      return msgParse
    } catch (err) {
      throw new Error(`message parse error: ${err}`)
    }
  }

  async receiveMessage(message: string) {
    try {
      const parseMsg = await this.parseMessage(message)

      if (messageValidation.error?.details) {
        console.log(`${MESSAGE_CALLBACK_VALIDATION_ERROR}, ${messageValidation.error.details[0].message}`)
      } else {
        this.actionOnMessage(parseMsg)
      }

      return true
    } catch (err) {
      console.error('action service Error', err)
      return true
    }
  }
}

export default messageProcessor
1.0.2

2 years ago

1.0.1

2 years ago

1.0.3

2 years ago

1.0.0

2 years ago