david32145 / nodejs-queue

An app using node, redis and queue tasks.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

NodeJS Queue

This app uses NodeJS and Redis. The queue tasks are an important features in server, the use cases like Mail send or Report generate are processes that not is recommended doing in http server for performance reasons. Therefore, the http server publishes only the task to Redis and server that process the tasks consumes them from Redis and processes.

πŸ‘¨πŸΌβ€πŸ’» Development Contact

David Nascimento

πŸš€ Technologies

  • ts-node-dev;
  • dotenv;
  • typescript;
  • redis;
  • express;

🎌 What I've learned

  • The queue/job taks;
  • Environment vars;
  • Redis pub/sub;

πŸŽ₯ How Usage

Declare the Queue.

import { Queue } from "../lib"

interface Mail {
  to: string
  from: string
  content: string
}

function mailCallback(mail: Mail) {
  console.log("> New Mail: ", mail)
}

const mailQueue = new Queue<Mail>("mail", mailCallback)

export default mailQueue

And enqueue Queue.

import MailQueue from "../queues/Mail.ts"

await MailQueue.enqueue({
    content: String(body.content),
    from: String(body.from),
    to: String(body.to),
  })

You can be view the full example into src/server.ts.

βœ‹πŸ» Prerequisites

πŸ”₯ Install and Running

  1. Config you environment;
  2. Git clone git clone https://github.com/david32145/nodejs-queue;
  3. Run yarn install or npm install for install dependencies;
  4. Copy .env.example to .env and fill with your config. Below an example of .env ;
REDIS_HOST=localhost
REDIS_PORT=6379
  1. Run yarn dev:queue or npm run dev:queue to raise the task server queues;
  2. Run yarn dev:server or npm run dev:server to raise the task server queues;
  3. Edit src/index.ts or src/queue.ts and start the game.

About

An app using node, redis and queue tasks.


Languages

Language:TypeScript 100.0%