Search code examples
ajaxnode.jsexpressreactjsplivo

Express + Plivo: Ajax POST request fails times but I still get the same sms on my phone twice


I'm using Express, React, Ajax, Plivo.

I have an ajax POST request that sends the data(user phone number and text message) from client to my express server. When I send the post request I get the text message on my phone but when I check chrome dev tools it tells me that the request status is pending(which in the server it logs as status 202), after a while the request status changes to failed on chrome dev tools.

This is what I get on the server logs:

    Status: 202
    API Response:
     { api_id: 'api-id-string',
      message: 'message(s) queued',
      message_uuid: [ 'random message_uuid string' ] }
    POST / - - ms - -

    Status: 202
    API Response:
     { api_id: 'api-id-string',
      message: 'message(s) queued',
      message_uuid: [ 'random message_uuid string' ] }
    POST / - - ms - -

I noticed that I get the sms immediately after making the POST request but the request stays pending for 4 minutes if it's sent twice. What I mean is, if the request pass the 2 minutes mark I get the text message again and then the request fails in 3.9 minutes. If I get the sms once then the request fails at 2 minutes. So every 2 minutes I get POST / - - ms - - which I'm guessing it means that's when the server stops trying to complete the request so it retries to complete it and sends the sms again.enter image description here

In the image above I submitted two POST requests, two different text messages. First it took approximately 4 minutes to fail, for that one I got the same sms twice. In the second one it took 2 minutes to fail, I got the sms once for that once.

I've been looking at other answers and seems like for a lot of people the request is sent twice because of favicon but in my case I'm console logging the url request and the url is the same for both. Also its not making another request, it seems like its retrying to make the request so this question is not a duplicate.

This is the code I have on my server:

app.post('/', function(req, res) {
    console.log(req.url);
    p.send_message({
        src: plivoNumber,
        dst: '1'+req.body.number,
        text: req.body.message,
    }, function(status, response) {
        console.log('Status:', status);
        console.log('API Response:\n', response);
    });
});

This is the ajax POST request:

$.post('/', {
     number: this.state.number,
     message: this.state.message
});

I'm not very experienced so my debugging skill is not the best or maybe I'm just making a newbie mistake.


Solution

  • You're not responding to the client requests so the connections/requests are timing out. Add at least a res.end() in your p.send_message() callback if you have nothing to send back to the client (other than a 200 OK HTTP status code).