Introduction Multithreading is a process of executing two or more threads simultaneously. The most programming languages provide an API for convenient work with threads and parallelism. The developer can focus on application logic, not on the communication channel, synchronization or architecture setup. added support for module with an experimental flag. But starting from this feature is available out of the box. It’s a good start for multithreading paradigm in Node. Node 10.5 worker_threads Node 11.7 has a huge potential, so support for this module was added to Odi from early stages. As always, Odi goal is to provide convenient, clean and minimalistic API (with magic 🧙♂️), so developers can focus on development, not on setup. worker_threads Project Setup From the last article, Odi got new CLI command which initializes project, defines scripts and installs all required dependencies. odi init Currently, there are only a few options: -j, --jsx-templating add jsx templates-d, --database add typeorm integration Structure By default, the project will have the following structure. All required dependencies will be installed during command execution based on the template type. Also if flag was provided, folder will be added in the project root directory. -j views Source Files There are 2 predefined source files in the basic template. The file that contains basic Odi configuration. By default, the server port is set to index.ts 8080 And the file with a simple controller definition. home.controller.ts That’s enough to start with Odi and development. Scripts For now, the only script is available out of the box. In the next release, command will be available using Mocha and Chai. npm start npm test Simply run the following command npm start This script includes compilation and actual running, so after completion, you can easily open the browser and check URL. http://localhost:8080 Threads By design, Node is single threaded with non-blocking I/O. Such approach has many pros and cons. The main advantage is simplicity. The developer does not need to care about threads manipulations, data synchronization and etc. But any resource-intensive tasks will block the event loop. Worker threads can process resource-intensive operations, so the main thread is always available. It’s really important for Server-Side applications, as any blocking task will delay accepting and processing of new client requests. Task Let’s create the resource-intensive (blocking) function for getting an answer ( or ) based on random values generation. Yes No Mathematical operations in most cases CPU intensive, so it’s a great example for our goals. Running this function with factor takes ~ for execution. 200_000_000 5 sec Blocking As mentioned above, any blocking operation won’t allow other tasks to execute until it’s finished. The best way to understand blocking is UI. Let’s add simple CLI loader to our application using library, just for example. Ora First of all, we need to install it. npm install ora @types/ora And change the Controller method in the following way. When the handler is triggered, the loader will appear in the terminal and will be spinning until our calculations are finished. Also, the time that was used for request processing will be printed. Let’s start our server and trigger handler from the browser. The loader is not spinning, as our calculation blocked the process. The loader must have the possibility to re-render frames every milliseconds but can’t do it, as the event loop is blocked by call. 80 getAnswer Consequences Let’s imagine that we have this code in the real application. The handler will block accepting and processing new clients requests. It will seriously affect the client experience. Such operations must be placed into other application or in the other thread. Workers Odi provides convenient API for multithreading. The developer does not need to think about any type of setup. Definition It’s really easy to define in Odi application and container. There are some similarities with definition. Let’s wrap function. Worker Service getAnswer Only decorator is required for definition. Now we can inject it in the controller as other dependencies. Worker Note, keyword must be added before the worker method call, even if it’s not async, as communication between threads is done in an asynchronous way. await That’s all! 🧙♂️ The method will be executed in another thread and the result will be returned to the main. Review Now, example with UI loader can be tested. Everything is working. The loader is spinning, as the code is running in another thread, so UI can re-render frames. Check To be sure that the method was processed in another thread, simply change in the next way. getAnswer Information about thread will be available right in the console. Comparison As you can see above, zero configuration is required for workes setup and processing. No event emitters, event handlers, filename and etc are required like in the official . Odi cares about initialization, messaging, method calls and errors handling. example Limitations There are no limitations in addition to basic ones. Remember that the worker is something like another application, so runtime instances can’t be accessed between different threads. Also, container can’t be accessed over the threads, so every single thread will have its own container. Dependency Injection Use Cases Basically, workers threads can be used in next approaches: Background and scheduled tasks Resource-intensive operations Queue-based processing Those approaches can be easily improved and adapted for each particular need, but all of them leads to performance improvements and application flexibility. More You can check Odi repo on GitHub. _🌪🌌 Opinionated, Declarative, Idiomatic framework for building scalable, supportable, enterprise applications and…_github.com Odi-ts/odi Also, there is another interesting article about Odi. _Real-time communication, project setup, templating and much more_hackernoon.com Building chat with Odi (Node.js) Thanks for reading! Feel free to leave any feedback, ideas or questions. _Let us know how we can improve. Vote on existing ideas or suggest new ones. · Collect and organize feedback with Nolt_odi.nolt.io Odi on Nolt If you like Odi, simply support us with start on GitHub. 🌟✨ Keep reading, much more interesting things will be shipped in the next updates! 😉🧙♂️