Is is possible to determine when a website receives a request (client-side)? - httprequest

I am making a nodejs program that sends HTTP requests. How could I determine how quickly the request reaches the website?
I can determine the amount of time between sending the request and receiving a response from the server. I'm not sure if I can take this time and just divide it by 2 to determine the amount of time between sending the request and the server receiving the request.
Here is an example of how I would determine the amount of time between sending the request and receiving a response from the server.
var request = require('request');
var options = {
url = 'someurl.com';
}
var date1, date2;
function callback(error, response, body) {
if(!error) {
console.log('Error.');
} else {
date2 = new Date().getTime();
console.log(date2-date1);
}
}
date1 = new Date().getTime();
request(options, callback);
This would give me the total time (the amount of time between sending request and receiving response), but not the time that I want (the amount of time between sending request and the website receiving request).

It depends on whether you have access to the API, which you are sending your requests.
If that is the case, then you can keep your date1 and generate date2 on the server upon receiving the request. Then include that in your response and calculate the difference.
For that synchronise your client<->server timeserver (ntp).
Otherwise you can always keep your approach and divide by 2 as you mentioned.
Hope it helps !

Related

What is the order of HTTP responses, data, and error, and are they guaranteed to be in that order?

I am debugging/testing the part of my app that sends an HTTP POST, with a file to upload, to a 3rd party server and need to be sure of the order of the info that server sends back.
Below is a very trimmed down sample of how I handle what is returned by the server.
At this moment, am debugging for files sent that exceed the LimitRequestBody size set in Apache. Yes, I do check file size in my app before sending, but am trying to debug for anything possible, i.e. malicious bot sending data outside of my app.
What I can't seem to find online is the lifecycle of what a server will send back in terms of the response, data, and error, and need to be sure I will get them back in this order:
Response
Data
and if there's an error:
Error (and then nothing else)
uploadSession.dataTask(with: upFile.toUrl!)
{ (data, response, error) in
if let response = response {
upLoadInvClass.upResp(resp: response)
}
if let error = error {
upLoadInvClass.upErr(error: error)
}
if let data = data {
upLoadInvClass.upData(data: data)
}
}.resume()

sending the same request multiple times in a raw. some return with status code 204 and some are ok

I'm sending a post request to the same url multiple times in a row. Some of the requests return 200 status code and some return 204 saying no content. the request updates some content in a mongo database. I don't know if this is relevant. What could cause this problem?
My index.js:
app.post('/updatetrialsession',authenticateJWT ,(req,res)=>{
User.findOne({username:req.user.username}).then(user=>{
var trialIdx=user.examTrials.findIndex(it=>it.trialId===req.body.trialId)
var questionIdx=req.body.questionIdx
if(trialIdx!==-1){
user.examTrials[trialIdx].questions[questionIdx]=req.body.question
user.examTrials[trialIdx].currentQuestion=questionIdx+1
user.examTrials[trialIdx].countDown=req.body.countDown
user.examTrials[trialIdx].numOfSolved=req.body.numOfSolved
var filter={
'username':user.username
}
var update={
$set:{
'examTrials':user.examTrials
}
}
User.findOneAndUpdate(filter,update).then(user=>{
console.log('updated')
res.json({
status:"success"
})
})
}
})
})
Note: all requests go to this url "/epdatetrialsession".
when there's a 20 sec gap between each request everything works just fine. but when the server get flooded with say a request every 2 seconds or so, some return with 204 status code
I have had the same issue once. In my experience, When sending multiple number of requests to the server within a very short period of time, sometimes the server fails to answer all of them. Depends on the server capability. Did you try to check this by, sending the request for only the ones which returns 204 one at a time and see whether they still returns a 204? If so, the server has responded correctly even when sending the requests in a row. But if the server return a content with 200 when sending one at a time, then the issue might be with the server when handling multiple requests at a time.

Is there a way to make an HTTP request reach a server at an exact time?

I am creating a nodejs program that sends HTTP requests. I want the HTTP requests to reach the server at an exact time. For example, I might want to send an HTTP requeset and have it reach the server at exactly Tuesday, August 6, 2019 1:00:00 PM, or 1565096400000 (epoch timestamp in milliseconds).
How would I do this?
One thing I've considered is using setTimeout() to time my requests at a certain time, but the time at which they reach the server is inconsistent.
Here is an example of a using setTimeout to time my requests.
In this example, I would want the request to reach the server at exactly 1565096400000 (epoch timestamp).
var options = {
url = 'someurl.com';
}
function callback(error, response, body) {
if(!error) {
console.log('Error.');
} else {
console.log(body);
}
}
var time_until_request = 1565096400000 - Date.now();
//We want the request to reach the server at exactly 1565096400000 (epoch //timestamp).
var timeout = setTimeout(function(){
request(options, callback);
},time_until_request);
Of course, with what I am doing, the request won't reach the server at the time I want it to. It will reach the server some time before or after - I believe due to inconsistencies with latency.

Mandrill send API will queue email occasionally

My email will occasionally get queued instead of sending download link immediately using a paid Mandrill account- other times it will send quickly. I would like it to be send at time of calling the API. The are no errors in the Mandrill API control panel.
This code works great most of the time via a node server on Heroku...I am using 'send_at' parameter w current Date using the javascript Date() method. How can I send an email consistently - sometimes queues can be 1 hour 30 min long.
// Text version of message
me_data.text += message;
// Html for message
me_data.html = results['html'];
// current Date for sending
var sendDate = new Date();
mandrill_client.messages.send(
{"message": me_data, "async": async, "ip_pool": ip_pool, "send_at": sendDate},
function (result) {
console.log(result);
//success
},
function (e) {
//_____________________________Error
console.log('A mandrill error occurred: ' + e.name + ' - ' + e.message);
}
);
This issue is no longer a problem. Please ignore the above posts about the time parameter as I am using this variable (clearly "send_at" from API doc) and Mandrill is sending.
This must have been a fluke of timing as I had paid a short time before launching production app. You have to pay (set credits) in Mandrill to use the send_at parameter - in other words upgrade from the free version. It may take some time to come into effect.
Mandrill has been working flawlessly since this issue like quick as lighting. So this is not an issue any longer for my stuff.

XmlHttpRequest not reporting server side progress

I have a client server application. The server side is completely in java. The client side has a few lines of html and the rest in plain javascript (I don't use ajax or jquery etc).
The client receives user inputs through an input box and when the user clicks a button, the client sends a POST request to the server using XmlHtpRequest. The server then runs a process that can take several minutes. The server periodically (every 5 seconds) sends a progress update message with HTTP status code 202 and with content "Processing.... completed 5 / 100". The numbers 5 and 100 are a sample. Instead of 100, it is the actual final counter of the process and instead of 5 it is the current counter indicating the progress. When the server finishes the processing it sends a 200 OK with the final output as the content. I have an output text area where I want to display the progress message during the processing and later the final output is also placed in the same output text area.
My client side javascript code is:
<script>
var xhr = new XmlHttpRequest();
function sendCommandToServer() {
window.xhr.onreadystatechange = getServerResponse;
window.xhr.open("POST", (document.domain + "/" + "executeCommand"), true);
window.xhr.send("arguments and data for the command");
}
</script>
<script>
function getServerResponse() {
if(window.xhr.status === 202) {
myTextArea.value = window.xhr.responseText;
window.xhr.onreadystatechange = getServerResponse;
return;
}
if(window.xhr.status === 200) {
myTextArea.value = window.xhr.responseText;
return;
}
alert("processing error");
}
</script>
The server correctly gets the command and runs the process and finishes and produces the output. It also correctly sends the progress messages. But my client side javascript does not get all of them. I am able to get the 1st progress message from the server - something like - "Processing .... 5 / 100" - but after that I am not able to get further calls to getServerResponse() to continue updating the progress as and when the server sends messages. If I modify the server side to avoid sending progress and send only the final output result, it works correctly and I get the final output result displayed at the client. I run this on IE10 and Chrome, with the same behavior. Can xmlHttpRequest() be used for this use-case? Am I doing something wrong? Any help will be appreciated. Thanks.
I'm new to this myself and trying to work out a method of achieving something similar.
I think the 202 status code is the only response you will get, ie the server will send a single response when it has accepted the job (202), or a 200 response when it has completed.
Source:
http://100pulse.com/http-statuscode/202.jsp