This project demonstrates the use of JavaScript's multithreading capabilities, specifically leveraging `worker_threads`, in a NestJS application for processing images.
This project demonstrates the use of JavaScript's multithreading capabilities, specifically leveraging worker_threads
, in a NestJS application for processing images. The service allows users to upload multiple images, which are then processed (resized, blurred, and converted to different formats) concurrently using multithreading, significantly improving the performance and efficiency of the application.
JavaScript is traditionally single-threaded, which means it handles one operation at a time in a sequence. However, with the advent of modern JavaScript runtimes and the worker_threads
module in Node.js, it's possible to perform tasks concurrently. Multithreading is crucial for resource-intensive operations such as image processing, large file handling, and complex computations because it allows you to utilize multiple CPU cores, reducing execution time and improving application responsiveness.
JavaScript, running in a Node.js environment, supports multithreading through the worker_threads
module. This module allows developers to create separate threads of execution within the same process. Each thread runs independently, and the main thread can communicate with workers through messaging. This setup is ideal for handling CPU-bound tasks in parallel, making full use of multicore systems.
sharp
library.imgProcessing.controller.ts
): Manages HTTP requests and delegates image processing to worker threads.imgProcessing.service.ts
): (Optional) If further abstraction is needed, services can handle business logic.imgProcessing.helper.ts
): Contains utility functions for image chunking and saving processed images.worker/w.js
): Worker thread script that processes images using the sharp
library.uploadFile.pipe.ts
): Validates the uploaded files to ensure they are images.imgProcessing.interface.ts
): Defines TypeScript interfaces for strongly typing the application’s data.sharp
library for image processingClone the repository:
git clone https://github.com/SJ22032003/nest-multi-threading
cd nest-multi-threading
Install the dependencies:
npm install
Start the application:
npm run start:dev
curl -X POST http://localhost:3000/process \
-F 'images=@/path/to/image1.jpg' \
-F 'images=@/path/to/image2.png' \
-F 'cors-level=4' \
-F 'blur-level=2' \
-F 'width=500' \
-F 'height=500' \
-F 'format=png'
This project showcases how JavaScript(Typescript), traditionally single-threaded, can efficiently handle CPU-bound tasks like image processing using multithreading with worker threads. By leveraging NestJS's robust architecture, this application efficiently manages concurrent tasks, making it a powerful example of modern JavaScript capabilities.
This project is ideal for learning about multithreading in JavaScript, building performance-critical applications, and gaining experience with NestJS, making it a valuable addition to any developer's portfolio.