Node.js - queue.send()

Send tasks to a queue.

import { queue } from '@nitric/sdk'
const batchQueue = queue('batch').for('sending')
const payload = {}
await batchQueue.send({ payload })

Parameters

  • Name
    tasks
    Required
    Required
    Type
    Task or Task[]
    Description

    A task or an array of tasks to send to the queue.

Examples

Send a task to a queue

import { queue } from '@nitric/sdk'
const batchQueue = queue('batch').for('sending')
const payload = { message: 'payloads can be any serializable object' }
await batchQueue.send([{ payload }])

Send multiple tasks to a queue

import { queue } from '@nitric/sdk'
const batchQueue = queue('batch').for('sending')
const tasks = [
{
payload: {
type: 'Email',
to: 'hello@example.com',
subject: 'Notification',
message: 'A notification from Nitric',
},
},
{
payload: {
type: 'SMS',
to: '+17200000000',
message: 'A text message from Nitric',
},
},
]
await batchQueue.send(tasks)

Dealing with failures

In rare cases when sending tasks to a queue some tasks might fail to be sent. The response from send() will include an array of any tasks that failed to send. You can process this array to retry or log the error.

const failed = await batchQueue.send(tasks)
for (const task in failed) {
console.log(task)
}
Last updated on Dec 19, 2024