NODE HELP

rxjs NPM (How It Works For Developers)

Published October 24, 2024
Share:

Introduction

In contemporary web development, handling intricate asynchronous processes and producing dynamic content on the fly is frequently necessary for building effective and responsive systems. Node-IronPDF and RxJS (Reactive Extensions for JavaScript) are two strong libraries that can assist developers in easily achieving these objectives. Together, they offer a powerful solution for managing real-time data streams and producing polished PDF documents in a Node.js environment.

A reactive extensions library for JavaScript called RxJS was created to facilitate reactive programming with Observables, simplifying the creation of asynchronous or callback-based applications with better debuggable call stacks. With its extensive operators for creating, combining, filtering, and transforming data streams, it enables developers to work declaratively with data. This improves the manageability and intuitiveness of managing events, asynchronous requests, and other real-time data sources. In today's highly interactive web apps, RxJS's ability to handle complicated asynchronous tasks elegantly is important.

We will look at how to include Node-IronPDF and RxJS reactive extensions library for JavaScript in a Node.js application in this article. We will begin by going over the fundamentals of installing the required libraries and configuring a Node.js project. After that, we'll get into creating and managing Observable creation methods with RxJS and show you how to utilize Node-IronPDF to use these Observables to initiate the creation of PDFs. By the time you finish reading this post, you should know exactly how to use sophisticated PDF generation with reactive programming to create dynamic, responsive Node.js applications.

What is RxJS?

A library for reactive programming with Observables—a crucial component of reactive apps—is called RxJS. It has a more modular file structure that makes working with asynchronous data streams, events, and operations in JavaScript applications easier to handle and more declarative for developers. A modular file structure not only enhances readability and maintainability but also promotes code reuse and easier testing. Here's how you can structure your RxJS-based project in a modular way.

Observables that emit various values over time, operators that manipulate, filter, and combine these data streams, and strong tools for handling concurrency and asynchronous operations are some of the key components of RxJS. RxJS encourages the use of functional programming, which enables programmers to represent intricate asynchronous processes in clear, understandable code. Achieving better, more debuggable call stacks in RxJS is entirely possible with the right approaches and tools.

rxjs NPM (How It Works For Developers): Figure 1 - RxJS

Real-time data processing, event-driven programming, controlling intricate asynchronous processes like HTTP requests, and reactively handling user interface interactions are among the common use cases for RxJS. It is a well-liked option for developing reactive and scalable apps in both frontend and backend JavaScript development due to its adaptability and extensive API surface.

Create and Config RxJS NPM

To create and configure RxJS in a Node.js project using npm, follow these steps:

Install RxJS

Next, use npm to add RxJS's latest library version as a dependency to your project via npm get. We can also check the npm version for the supported library version explicitly:

npm install rxjs
npm install rxjs
'INSTANT VB TODO TASK: The following line uses invalid syntax:
'npm install rxjs
VB   C#

Set Up RxJS in Your Node.js Application

RxJS can be used in your Node.js application after it has been installed. An Observable can be created and subscribed to using the following simple example:

The following code should be added to a file:

// Import necessary RxJS modules
const { Observable } = require('rxjs');
// Create an Observable that emits three values
const observable = new Observable(observer => {
  observer.next('Hello');
  observer.next('RxJS');
  observer.next('World');
  observer.complete();
});
// Subscribe to the Observable
observable.subscribe({
  next: value => console.log(value),
  complete: () => console.log('Observable completed'),
});
// Import necessary RxJS modules
const { Observable } = require('rxjs');
// Create an Observable that emits three values
const observable = new Observable(observer => {
  observer.next('Hello');
  observer.next('RxJS');
  observer.next('World');
  observer.complete();
});
// Subscribe to the Observable
observable.subscribe({
  next: value => console.log(value),
  complete: () => console.log('Observable completed'),
});
' Import necessary RxJS modules
'INSTANT VB TODO TASK: The following line could not be converted:
const
If True Then
	Observable } = require( 'rxjs');
' Create an Observable that emits three values
'INSTANT VB TODO TASK: Lambda expressions and anonymous methods are not converted by Instant VB if local variables of the outer method are referenced within the anonymous method:
const observable = New Observable(observer =>
If True Then
	observer.next( 'Hello');
	observer.next( 'RxJS');
	observer.next( 'World');
	observer.complete()
End If
)
' Subscribe to the Observable
observable.subscribe({ [next]:= Function(value) console.log(value), complete:= Function() console.log( 'Observable completed')});
VB   C#

Observable Creation: Using the Observable constructor, we create an observable that emits the values "Hello," "RxJS," and "World" in that order.

Subscription: We use the subscribe method to subscribe to the observable. The complete callback records a message when the Observable completes, and the subsequent callback logs each value that is broadcast to the terminal.

rxjs NPM (How It Works For Developers): Figure 2 - Observable Output

Additional Configuration

A wide range of operators are available in RxJS for combining, filtering, and altering Observables. These operators are available for import into your application as needed:

const { Observable } = require('rxjs');
const { map, filter } = require('rxjs/operators');
// Example: Using operators with Observables
const observable = new Observable(observer => {
  observer.next(1);
  observer.next(2);
  observer.next(3);
  observer.complete();
});
observable.pipe(
  filter(value => value > 1),
  map(value => value * 10)
).subscribe({
  next: value => console.log(value),
  complete: () => console.log('Observable completed'),
});
const { Observable } = require('rxjs');
const { map, filter } = require('rxjs/operators');
// Example: Using operators with Observables
const observable = new Observable(observer => {
  observer.next(1);
  observer.next(2);
  observer.next(3);
  observer.complete();
});
observable.pipe(
  filter(value => value > 1),
  map(value => value * 10)
).subscribe({
  next: value => console.log(value),
  complete: () => console.log('Observable completed'),
});
'INSTANT VB TODO TASK: The following line could not be converted:
const
If True Then
	Observable } = require( 'rxjs');
'INSTANT VB TODO TASK: The following line could not be converted:
const
If True Then
	map, filter } = require( 'rxjs/operators');
' Example: Using operators with Observables
const observable = New Observable(Sub(observer)
	observer.next(1)
	observer.next(2)
	observer.next(3)
	observer.complete()
End Sub)
observable.pipe(filter(Function(value) value > 1), map(Function(value) value * 10)).subscribe({ [next]:= Function(value) console.log(value), complete:= Function() console.log( 'Observable completed')});
VB   C#

This sample shows how to construct and work with Observables using RxJS library operators. First, the map and filter operators are imported from the rxjs/operators module, and the Observable class is loaded from the rxjs module. The next step is to create an Observable, which consecutively emits the values 1, 2, and 3 before finishing. The pipe technique converts this Observable, enabling the chaining of various operators. The map operator multiplies each of these values by 10 after the filter operator limits the values that can pass through to those greater than one.

rxjs NPM (How It Works For Developers): Figure 3 - Operators with Observables Output

The altered Observable is then subscribed to, and each of the resultant values is logged to the console by the subsequent callback, which also logs a message indicating that the Observable has been completed when it is finished. Consequently, 20 and 30 will appear in the console output, followed by "Observable completed".

Getting started

To utilize RxJS (Reactive Extensions for JavaScript) and Node-IronPDF in a Node.js application, we will combine RxJS's reactive programming features with Node-IronPDF's PDF creation functionalities. With this combination, we can manage async data streams and produce PDF documents on the fly in response to events or modifications in the data.

What is IronPDF?

An IronPDF library is a potent Node.js library that seeks to convert HTML content into incredibly high-quality PDF pages. It speeds up the process of turning HTML, CSS, and other JavaScript files into properly formatted PDFs without compromising the original online content. This is a highly useful tool for web applications that need to produce dynamic, printable documents such as invoices, certifications, and reports.

IronPDF has several features, including customizable page settings, headers, footers, and the option to add fonts and images. It can manage complex styles and layouts to ensure that each test PDF output follows the specifications. Moreover, IronPDF controls JavaScript execution within HTML, allowing accurate dynamic and interactive content rendering.

rxjs NPM (How It Works For Developers): Figure 4 - IronPDF

Features of IronPDF

PDF Generation from HTML

Convert HTML, CSS, and JavaScript to PDF. Supports two modern web standards: media queries and responsive design. Handy for using HTML and CSS to dynamically decorate PDF documents, invoices, and reports.

PDF Editing

It is possible to add text, images, and other material to already-existing PDFs. Extract text and images from PDF files. Merge many PDFs into a single file. Split PDF files up into several distinct documents. Add headers, footers, annotations, and watermarks.

Performance and Reliability

In industrial contexts, high performance and reliability are desirable design attributes. Easily handles large document sets.

Install IronPDF

To gain the tools you need to work with PDFs in Node.js projects, install the IronPDF package.

npm install @ironsoftware/ironpdf
npm install @ironsoftware/ironpdf
'INSTANT VB TODO TASK: The following line uses invalid syntax:
'npm install @ironsoftware/ironpdf
VB   C#

RxJS Observables and IronPDF

Make a file and configure Node-IronPDF to integrate RxJS:

// Import necessary modules
const { Observable } = require('rxjs');
const { IronPdf } = require('node-ironpdf');
// Create an instance of Node-IronPDF
const ironPdf = new IronPdf();
// Example Observable that emits data periodically
const observable = new Observable(observer => {
  let counter = 0;
  const intervalId = setInterval(() => {
    counter++;
    observer.next({ eventNumber: counter });
    if (counter === 3) {
      observer.complete();
      clearInterval(intervalId);
    }
  }, 1000); // Emit every second
});
// Subscribe to the Observable and generate PDFs with Node-IronPDF
observable.subscribe({
  next: async data => {
    try {
      const htmlContent = `<h1>Event Report</h1><p>Event Number: ${data.eventNumber}</p>`;
      const pdf = await ironPdf.createFromHtml(htmlContent);
      const filePath = `./reports/event_report_${data.eventNumber}.pdf`;
      await pdf.saveAs(filePath);
      console.log(`PDF report generated: ${filePath}`);
    } catch (error) {
      console.error('Error generating PDF:', error);
    }
  },
  complete: () => console.log('Observable completed'),
});
// Import necessary modules
const { Observable } = require('rxjs');
const { IronPdf } = require('node-ironpdf');
// Create an instance of Node-IronPDF
const ironPdf = new IronPdf();
// Example Observable that emits data periodically
const observable = new Observable(observer => {
  let counter = 0;
  const intervalId = setInterval(() => {
    counter++;
    observer.next({ eventNumber: counter });
    if (counter === 3) {
      observer.complete();
      clearInterval(intervalId);
    }
  }, 1000); // Emit every second
});
// Subscribe to the Observable and generate PDFs with Node-IronPDF
observable.subscribe({
  next: async data => {
    try {
      const htmlContent = `<h1>Event Report</h1><p>Event Number: ${data.eventNumber}</p>`;
      const pdf = await ironPdf.createFromHtml(htmlContent);
      const filePath = `./reports/event_report_${data.eventNumber}.pdf`;
      await pdf.saveAs(filePath);
      console.log(`PDF report generated: ${filePath}`);
    } catch (error) {
      console.error('Error generating PDF:', error);
    }
  },
  complete: () => console.log('Observable completed'),
});
' Import necessary modules
'INSTANT VB TODO TASK: The following line could not be converted:
const
If True Then
	Observable } = require( 'rxjs');
'INSTANT VB TODO TASK: The following line could not be converted:
const
If True Then
	IronPdf } = require( 'node-ironpdf');
' Create an instance of Node-IronPDF
const ironPdf = New IronPdf()
' Example Observable that emits data periodically
const observable = New Observable(Sub(observer)
	Dim counter As let = 0
	const intervalId = setInterval(Sub()
		counter += 1
		observer.next({ eventNumber:= counter })
		If counter == 3 Then
			observer.complete()
			clearInterval(intervalId)
		End If
	End Sub, 1000)
End Sub)
' Subscribe to the Observable and generate PDFs with Node-IronPDF
'INSTANT VB TODO TASK: Lambda expressions and anonymous methods are not converted by Instant VB if local variables of the outer method are referenced within the anonymous method:
observable.subscribe({ [next]:= async data =>
If True Then
	Try
		const htmlContent = `(Of h1) [Event] Report</h1>(Of p) [Event] Number: $
		If True Then
			data.eventNumber
		End If
		</p>`
		const pdf = Await ironPdf.createFromHtml(htmlContent)
'INSTANT VB TODO TASK: Local functions are not converted by Instant VB:
'		const filePath = `./reports/event_report_$
'		{
'			data.eventNumber
'		}
		.pdf`
		Await pdf.saveAs(filePath)
		console.log(`PDF report generated:= ${filePath}`)
	Catch e1 As [error]
		console.error( '@Error generating PDF:', @error);
	End Try
End If
, complete: Function() console.log( 'Observable completed'),
End If
)
VB   C#

First, the required modules are imported: IronPDF from Node-IronPDF and Observable from RxJS. IronPDF offers features for producing and modifying PDF documents within Node.js, whereas Observable is used to build a data stream that emits information over time. To make PDF creation and management operations easier later in the code, an instance of IronPDF is then created.

The definition of an Observable with the name observable forms the basis of the application. This Observable is made to use setInterval to release data objects { eventNumber } regularly. In this instance, it emits three values at intervals of one second (1000 milliseconds): { eventNumber: 1 }, { eventNumber: 2 }, and { eventNumber: 3 }.

rxjs NPM (How It Works For Developers): Figure 5 - RxJS with IronPDF Output

The subsequent callback in the subscription to the Observable observable deals with each emitted value (data) as it comes in. Based on the eventNumber from the transmitted data, an HTML content string (htmlContent) is created inside this callback. The PDF document is subsequently created using document.fromHtml(htmlContent) with this HTML content. In the./reports directory, every PDF file is saved with a distinct filename (event_report_1.pdf, event_report_2.pdf, etc.).

Using a try...catch block, error handling is included in the subsequent callback to handle any possible errors that can arise during the PDF creation process. When an error happens, a console is used to log it to the console.error.

Ultimately, during the subscription's full callback, a message titled "Observable completed" is recorded in the console, signifying that the Observable has concluded its value emission.

rxjs NPM (How It Works For Developers): Figure 6 - PDF Outputs

Conclusion

Reactive programming and dynamic PDF production work well together, as demonstrated by the integration of RxJS with Node-IronPDF in a Node.js application. In response to real-time events or data changes, this combination provides a reliable solution for managing asynchronous data streams and producing PDF documents of expert quality.

Through the use of RxJS Observables, developers may effectively oversee and modify asynchronous data streams, simplifying the handling of intricate workflows and enabling declarative and reactive responses to user interactions or external events. Applications like analytics dashboards, interactive reporting tools, and monitoring systems that need to handle data in real time must have this capacity.

Finally, RxJS combined with Node-IronPDF enables developers to create responsive and scalable apps that manage real-time data efficiently and offer smooth PDF creation. This combination makes it possible to develop complex, event-driven apps with dynamic reporting capabilities that improve operational effectiveness and user experience. Together, RxJS and Node-IronPDF provide a potent toolkit for contemporary Node.js programming, supporting tasks like handling dynamic document creation, interfacing with real-time data sources, and producing periodic reports.

Developers can choose the best model with ease when there are project-specific license options that are well-defined. These features enable developers to tackle a variety of problems quickly, efficiently, and successfully.

< PREVIOUS
Prettier - NPM (How It Works For Developers)
NEXT >
react hook form NPM (How It Works For Developers)

Ready to get started? Version: 2024.11 just released

Free npm Install View Licenses >