Node.js - queue.enqueue()
Send messages to a queue.
import { queue } from '@nitric/sdk'
const batchQueue = queue('batch').allow('enqueue')
await batchQueue.enqueue({ example: 'value' })
Parameters
- Name
messages
- Required
- Required
- Type
- Object or Object[]
- Description
A message or an array of messages to send to the queue.
Examples
Send a message to a queue
import { queue } from '@nitric/sdk'
const batchQueue = queue('batch').allow('enqueue')
const item = { message: 'payloads can be any serializable object' }
await batchQueue.enqueue(item)
Send multiple messages to a queue
import { queue } from '@nitric/sdk'
const batchQueue = queue('batch').allow('enqueue')
const items = [
{
type: 'Email',
to: 'hello@example.com',
subject: 'Notification',
message: 'A notification from Nitric',
},
{
type: 'SMS',
to: '+17200000000',
message: 'A text message from Nitric',
},
]
await batchQueue.enqueue(items)
Dealing with failures
In rare cases when enqueueing messages some might fail to be sent. The response from enqueue()
will include an array of any messages that failed to send. You can process this array to retry or log the error.
const failed = await batchQueue.enqueue(messages)
for (const message in failed) {
console.log(message)
}