Implementing parallel HTTP requests with a dynamic number of requests using RxJs

Currently, I am in the process of building a Node.js API that utilizes Express. As part of this project, I am incorporating the node-rest-client module to handle HTTP requests.

One of the key API endpoints that needs to be developed is /api/v1/users/:userId, which will provide comprehensive user information including details about the departments they are associated with.

In order to retrieve this information, the backend REST services required are:

/users/:userId - This service returns a JSON object containing user information as well as a list of department IDs. For example:

{ "name" : "xxx",
  "departments" : [1, 5 ,6, 8]
}

/departments/:departmentId - This service provides a JSON object with department details:

{ 
  "id" : x,
  "name" : "xxx"
}

When making a call to /api/v1/users/1, the following steps need to be executed:

  1. GET /user/1 ->
    { "name" : "user1" , "departments" : [1, ,5 ,7 ,8]}
  2. Retrieve the department IDs and make individual calls to /departments/deparmentId
  3. Once all relevant data has been collected, assemble the complete JSON response and return it.

I intend to optimize the request handling by parallelizing the operations using RxJs. I believe that utilizing Rx.Observable.zip() should suffice for this purpose.

The challenge lies in determining how to use Observable.zip() when dealing with an unknown number of Observables representing each HTTP request call within an array.

If the size of the array was fixed, calling zip() would look something like this:

var observables = [ obs1, obs2 ];

Rx.Observable.zip( observables[0], observable[1], function(...){...});

However, since the number of observables is variable, I am unsure of how to implement zip().

Answer №1

One way to implement this is by leveraging the ... operator in es6:

var observableList = [obs1, obs2, obs3, ...,  obsx];
Rx.Observable.zip(...observableList, function() {});

Answer №2

One alternative method involves using the forkJoin function, which has the ability to accept observables within an array.

Rx.Observable.forkJoin([obs1, ..., obsN]).subscribe(...);

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Tips for speeding up the loading of JSON with large data on HTTP requests or webpages

When requesting the page (via HTTP or webpage), it seems to be very slow and even crashes unless I load my JSON data with fewer entries. This issue is critical as I anticipate needing to work with large amounts of data frequently in the future. Below are t ...

Out of the blue, my session stopped functioning

I am encountering a major issue with sessions. I am working on developing an application using "Redis" in node.js along with various libraries such as express. However, I have run into a problem where the sessions are no longer functioning properly. Desp ...

Validator Express: Left-hand Side Brackets

Can LHS Brackets be validated in express-validator for advanced filtering? Here is an example querystring: ?test[gt]=1 I have attempted the following validations: query("test.gt").isAlphanumeric() query("test[gt]").isAlphanumeric() I ...

The next-auth module is encountering an issue: it cannot locate the 'preact-render-to-string' module when trying to execute the getServerSession() function

While migrating my project to the app/ directory in nextjs 13.4, I encountered an issue when trying to access the session using getServerSession() in a server component: import { authOptions } from "@/pages/api/auth/[...nextauth]"; import { getServerSessio ...

WSL problem: Unable to run executable file due to Execution format error

I'm currently in the process of installing nodejs on my WSL Ubuntu subsystem for Windows, and I used the command: sudo apt-get install nodejs However, when I attempted to check the node version using node --version, I encountered the following problem ...

Proxyquire: The inability to stub fs.readFileSync

Having trouble stubbing readFileSync on fs from the NodeJS core? The issue is isolated in the code snippet below. When running the test with Mocha, an error occurs: > mocha tests/test.js Some description 1) "before all" hook 0 pa ...

Guidelines for ApostropheCMS: Implementing Middleware to Deliver Varied Pages

I am working with ApostropheCMS v3 project and I am looking to implement some middleware within modules/@apostrophecms/page/index.js. Here is what I have in mind: module.exports = { ... handlers(self, options) { return { "@apostrophecms/ ...

What could be the reason for why get/post methods are causing Unauthorized (401) errors?

My project involves both a log-in and sign-up feature. For the sign-up part, I utilized Express-Validator, while for the log-in part, I integrated Passport.JS. However, when I added the passport JS declaration in app.js, it resulted in an Unauthorized erro ...

Tips for creating an array in a <script> tag within an hbs view template

Currently, I am delving into the world of full stack web development with a focus on Node/Express. The project at hand is to create a voting app as part of a challenge from FreeCodeCamp, which you can find here. To display user votes in pie charts on the f ...

Update Mongoose data conditionally using an array of objects

I am facing a challenge with my Cart schema in Mongoose. The CartItems are stored as an array of objects. { _id: string; items: [ { product: string; options: { size: string; color: string; } quantity: number; ...

Utilizing Socket.io and SailsJS for a Chat Application: Detecting Client Shutdown and Updating User Status to Offline

I'm currently developing a multi-user chat application using node-webkit and SailJs, and I've been working on implementing the login status of users. When a user opens the application, they are considered online. The scenarios in which a user wou ...

What is the process for retrieving a list of authenticated users from firebase?

I'm currently working on a firebase project where users authenticate with their mobile number for login. My task involves retrieving all mobile numbers using the node.js SDK. Here's how I've set up the firebase: const fs = require('fir ...

Encountering difficulties while attempting to modify information in mongodb

Need help with updating specific fields in a Mongoose driver and Express JS setup. Here is the schema: var mongoose = require('mongoose'), Schema = mongoose.Schema; var ProfilesSchema = new Schema({ presentRound: { type: Numb ...

Tips for storing and retrieving the data fetched from an Axios GET request in a variable

I am currently working on an integration project that involves using API's from two different websites. I have successfully created a rest API that allows both software to send post and get requests, with my API serving as the intermediary. Whenever ...

How can I utilize the Json data retrieved through the findById method in my code?

My current project involves creating an API that retrieves data from Patients (id and name), Physicians (id and name), and Appointments (id, phyId, patId, app_date) in order to display the patients scheduled to see a specific physician. To achieve this, I ...

Every time I attempt to run "npm install" on Visual Studio Code, I encounter an error

Every time I try to run npm install, I encounter this error. My node version is 18.9.1 and I have exhausted all possible solutions. Any help would be greatly appreciated. '''npm ERR! code ENOENT npm ERR! syscall open npm ERR! path C:\Us ...

Socket.io lost connection

Recently, I've encountered an issue with my chat application built using nodejs, Express, socket.io, and angular. Despite functioning well most of the time, it has a tendency to randomly disconnect after no more than 2 minutes, resulting in several ne ...

The node/express server is throwing an error due to an undefined parameter

What seems to be the issue with my string parameter? var express = require('express'); var app = module.exports = express(); var mongoose = require('mongoose'); var bodyParser = require('body-parser'); var braintree = require ...

What is the best practice for initializing stubs in Mocha - should they be directly initialized in the describe() block or inside

When it comes to utilizing stub variables for resetting and restoring during afterEach() and after() callbacks, my strategy involves defining the stubs within the describe() block for easy access: describe('my SUT', () => { const myStub = s ...

Revamping Cookie-based sessions in express.js

I am currently utilizing the cookie-session module for Express.js to manage sessions. My goal is to refresh sessions on each page load or ajax call, as is typically seen in similar setups. Unfortunately, the documentation lacks any information regarding ...