NestJS Dependency Injection in Worker Threads
I will show you how to use worker threads in a NestJS app. There may be cases when you need to do CPU-intensive tasks on the backend like big JSON parsing, video encoding, compression, etc. If you have at least 2 cores in your processor's configuration you can run javascript in parallel for these tasks and not block the main thread of the NestJS app that handles client requests. An excellent way of doing this is by using node.js worker threads . Note: You shouldn't use worker threads for I/O operations as node.js already gracefully handles this for you. Let's dive into a worker thread code example: import { Injectable } from '@nestjs/common' ; import { Worker } from 'worker_threads' ; import { workerThreadFilePath } from '../../workerThreads/config' ; @Injectable () export class WorkerThreadExample { async runWorker () { const worker = new Worker( workerThreadFilePath , { workerData : 'data to be sent from main th