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.
David Nascimento
- ts-node-dev;
- dotenv;
- typescript;
- redis;
- express;
- The queue/job taks;
- Environment vars;
- Redis pub/sub;
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
.
- Config you environment;
- Git clone
git clone https://github.com/david32145/nodejs-queue
; - Run
yarn install
ornpm install
for install dependencies; - Copy .env.example to .env and fill with your config. Below an example of
.env
;
REDIS_HOST=localhost
REDIS_PORT=6379
- Run
yarn dev:queue
ornpm run dev:queue
to raise the task serverqueues
; - Run
yarn dev:server
ornpm run dev:server
to raise the task serverqueues
; - Edit
src/index.ts
orsrc/queue.ts
and start the game.