Mohamed Ali Nakouri
Mohamed Ali Nakouri

Reputation: 145

Sending Many POST requests simultaneously Nodejs

I am new to Nodejs so excuse me for any mistake .. :)

Let me explain what i am trying to do :

basically i am making a push notification service for my platform .. i will explain further.. I have two NodeJs servers (using express) :

SERVER 1 : it gets everything needed from the database such as ( device registration , identifier ..) and should send to the second server.

SERVER 2 : This server Receives a JSON ( contains everything i need ) to create the FCM and APNS payload and then send to the convenient provider (FCM,APNS).

what i am using : i am using axios to send POST requests.

The issue : since the 1st server will be sending big amount of requests ( usually 5K or more -- it's dynamic) at the same time , axios cannot handle that , and I've tried many other alternatives to axios but faced the same thing.

My question : How can i send that amount of requests without any issues ?

PS: when i send few requests ( 100 or bit more) i face no errors ...

I hope everything is clear and i would really appreciate any help.

Code Example of the Request with Axios :

PS: it always falls in the "[Request Error] ..."

    try
                {
          
                  axios.post(endpoint,{sendPushRequest})
                        .then( response => {
                            console.log(response.data);
                        })
                        .catch(  er => {

                            if (er.response) {
                                console.log("[Response Error] on sending  to Dispatcher...");
                                // The request was made and the server responded with a status code
                                // that falls out of the range of 2xx

                                console.log(er.response.data);
                                console.log(er.response.status);
                                console.log(er.response.headers);

                            } else if (er.request) {
                                console.log("[Request Error] on sending  to Dispatcher...");
                                
                                // The request was made but no response was received
                                // `error.request` is an instance of XMLHttpRequest in the browser and an instance of
                               
                                
                            } else {
                                // Something happened in setting up the request that triggered an Error
                                console.log('[Error]', er.message);
                            }
                            console.log(er.config);
                        });
                }
                catch (e) {
                    console.log('[ Catch Error]', e);
                }

Upvotes: 0

Views: 251

Answers (1)

Raghu Chahar
Raghu Chahar

Reputation: 1773

Usually, for doing this kind of asynchronous stuff you should use any queuing service as if the second server gets busy which it might in case of handling such a huge number of rest APIs your user would miss the notification.

Your flow should be like:

SERVER 1: it gets everything needed from the database such as ( device registration, identifier ..) and should push/publish to any queuing service such as rabbitMQ/Google pubsub etc.

SERVER 2: Instead of having rest APIs, this server should pull messages from the queue recursively and then Receives a JSON ( contains everything I need ) to create the FCM and APNS payload and then send to the convenient provider (FCM, APNS).

This is beneficial because even if anything happens to your server like busy/crashes the message would persist in the queue and on restarting the server you would be able to do your work(sending a notification or whatever).

Upvotes: 1

Related Questions