Setting Up A Job Queue For A Node App [Tutorial]

March 14, 2018

Many web apps will need a job queue at some point in their development.

A job queue enables you to uncouple long-running or resource-intensive tasks from the request/response lifecycle.

Creating PDFs, sending emails, and processing audio or video are tasks that are best handled by a job queue. A queue also enables you to balance load and triage tasks.

One of the best job queue solutions for Node is kue by Automattic.

With Kue, you can quickly deploy a job queue microservice with a rest API and simple UI.

How Kue Works

Kue uses Redis as a database to store jobs along with Node's built-in event emitters.

Starting from scratch:

mkdir jobs-queue && cd jobs-queue
touch index.js
npm init

Install kue:

npm i kue --save

In index.js:

const kue = require('kue')
const queue = kue.createQueue({
  redis: process.env.REDIS_URL
})
var data = {
  title: 'my-job',
  message: 'lorem ipsum'
}
var task = queue.create('task', data)
  .delay(60 * 1000) // 1 minute
  .priority('high')
  .save()
queue.process('task', (job, done) => {
  console.log("message", job.data.message)
  done()
})