Posts

Showing posts with the label nodejs

Facade Design Pattern

Image
Introduction The Facade is a structural design pattern . It helps create a simple intermediary object that interacts with multiple systems (such as subsystems ). The responsibilities of the Facade Pattern include: Simplifying complex interactions with systems through the intermediary Facade object. Hiding the complex internal operations of subsystems, making them easier to use. Frequency of use: quite high. Problem and solution In cases where some processes require interaction with multiple services or third-party systems, and these processes are needed repeatedly in various places within the system, the typical solution might be to copy and paste the logic to those places. However, this leads to duplicated code in many locations, making maintenance and updates difficult when there are changes. The solution is to use the Facade Pattern to create an intermediary object to communicate with subsystems. This allows for the implementation of complex logic in a centralized and well-defin...

Implementing Microservices with NodeJS TypeScript using the Moleculer Framework

Image
Introduction Moleculer is a fast, modern, and powerful microservices framework for NodeJS . It helps build efficient, reliable, and highly scalable services. Originally designed for JavaScript , Moleculer now supports Typescript and offers a CLI tool that creates boilerplates as easily as Nest , Next , Vite React , and Angular. Implementing in an Existing NodeJS Project If you already have a NodeJS project and want to integrate Moleculer , it's simple. Just install the package and use the provided APIs . yarn add moleculer To create a service like this: import { ServiceBroker } from 'moleculer' const broker = new ServiceBroker () broker . createService ({ name: 'math' , actions: { add ( ctx ) { return Number ( ctx . params . a ) + Number ( ctx . params . b ) }, }, }) broker . start () . then (() => broker . call ( 'math.add' , { a: 1 , b: 2 })) . then (( res : number ) => console . log ( '1 + 2 =' ...

Using fluent-ffmpeg to work with videos in NodeJS

Image
Introduction ffmpeg is a popular library that provides APIs for extracting information and manipulating videos. It supports various programming languages such as JavaScript , Ruby , and more. In this article, I'll give you a simple example of how to use ffmpeg to create thumbnails from videos and resize videos in NodeJS with TypeScript . Prerequisites Before we start, you need to install ffmpeg . The installation process depends on your operating system. If you're using Ubuntu , it's straightforward: sudo apt install ffmpeg Next, set up your NodeJS Typescript project. After that, install the package ` fluent-ffmpeg `. This package will serve as the interface for interacting between NodeJS and the previously installed ` ffmpeg `. yarn add fluent-ffmpeg Source Code Update the ` main.ts ` file with the following content: import * as ffmpeg from 'fluent-ffmpeg' const srcVideo = 'path/test.mp4' const outputVideo = 'path/test-resized.mp4...

Integrating NodeJS with Google Cloud Pub/Sub

Image
Introduction Google Cloud Pub/Sub is a fully-managed, real-time messaging service that enables you to send and receive messages between independent applications. This article will guide you through integrating NodeJS with Google Cloud Pub/Sub for simple message sending and receiving. Additionally, I've written an article on using Kafka , a distributed event streaming platform, to demonstrate message sending and receiving through NodeJS and Golang . Prerequisites Before proceeding with the following steps, make sure you have: A Google Cloud account with Pub/Sub enabled. Basic knowledge of NodeJS . In this article, I'm using a NodeJS TypeScript project. You can find setup instructions here . Implementing the Code After setting up your NodeJS TypeScript project , install the following package: yarn add @google-cloud/pubsub Next, update the ` main.ts ` file with the following content: import { PubSub , Topic , Subscription } from '@google-cloud/pubsub' export c...