I noticed that jquery ajax request sometimes take about 1-2 seconds before the responce comes is this normal and is there any way to short that time to minimal?
The responce type is json and it's small html code.
Thanks!
Here are the places the slowness could be:
Database <--> Web server latency
Database Query
Database <--> Web server bandwidth
Server side script code
Web server <--> client latency
Web server <--> client bandwidth
In short, yes, a delay is normal after the request is sent and before the response comes. However, you didn't specify exactly what is happening with the request.
Is it just the request that is taking up to much time? Is the delay before it sends the request? I work with jQuery's ajax engine all the time in our app. I haven't really ever seen latency before the request is sent. What do you use to monitor the ajax request? You can check this using firebug. Enable the net panel and watch the timing.
If the only delay is after the response comes then it may be the HTML that is returned. Is there a lot of HTML that is returned? Is there javascript in the HTML? Are you injecting the HTML in the page and the delay is after that?
Depending on how you coded your solution, the speed largely depends on internet connection from you to server. You can try launch it on localhost to see if it takes that long, if it does, then something is wrong with your code. If you could post a simplified code illustrating the problem we can try to figure it out :D
Related
I have a weird problem in a preproduction environment.
I have a website which performs some operations on a Web Api hosted on another server.
Usually it takes under a second to make a specific post request, but after 5 minutes of inactivity the same post request will take 10-30 seconds. (According to google chrome network tab)
The mentioned post request is not the first request in a list of requests performed.
The request is done using Ajax.
I have run SQL profiler to see if the database queries were running slow, but these are all performing fine, and it looks like the POST request is just returning with a delay.
Do you guys have any idea why this is happening?
A very likely cause is compiling at the database level. The first time you run a query with most database engines, the sql has to be compiled and an execution plan developed. This is then cached for a while. When it is no longer cached, it has to be done again. The longer your sql string, the longer it takes to compile.
The solution is to use a stored procedure. Once it runs once it stays compiled and the execution plan is always available.
Problem solved!
The problem was that an email was sent using SMTPClient in the request.
In a web application the smtp client is not async and therefore it had to be sent before the POST request returned.
Making it async using the reply from TheCodeKing in this question:
How do I avoid a delay when sending email from my application?
solved the problem!
Suppose I have server. A client loading an HTML file containing a javascript library will have the script executed by the browser. The problem here is that if the client's computer is slow, the processing will take a long time.
So I want to move the processing to the server side. But, instead of having to rewrite the entire javascript library into another language, I simply want to run the javascript on the server.
Googling "server side javascript" directs me to Node.JS, which in my imagination have the capability to do so. But, I cannot find a tutorial which does just that. Does this mean that there really is no easy way to do so? For example, because the javascript script may contain DOM specific things such as document.getElementById(), which does not make much sense on the server side.
There is no trivial way to simply shift processing of JS from the client to the server.
You need to break the code down into code that must run on the browser (such as, assuming you don't want the browser to load an entirely new page, DOM manipulation) and code that can run on the server.
Then you need to provide a way to pass data between the server and the browser, this is normally done via HTTP (using Ajax).
When you take input from the client you need to send it to the server in an HTTP request (instead of just passing it as an argument to a function). The server needs to read the HTTP request, process it, and make an HTTP response.
The Ajax callback then needs to parse the response and run any client side logic (such as DOM updates) in response.
Note that network communication times will impact performance.
You can't "merge" the client and server in this way. All you could do is process the data on the server and just display it in the client without any further processing. Maybe you should refresh you knowledge about HTTP and how websites are send to the clients. Without any additional tricks, like websockets, comet or ajax polling, you can't access the client after you send the initial website to it. Even than you can just send data to the client.
When you want to stick to Javascript, Node.js is a good option. But even than you would need to send the data you want processed to the server, process it there and send back the processed data in JSON or "display ready" HTML.
I am building a mobile site that works like a slideshow. There are a number of image slides and you can swipe left and right to traverse the slides.
I would like to monitor the download speed performance of the slides using javascript and report the times to the server.
I presume that ajax is the way to report the times, although I am new to ajax. My fist concern is that the report sent to the server should be as lean as possible. Also it is only really necassary for the communication with the server to be one way.
Can the flow go just in the direction of from the browser to the server without any responses being sent back to the browser? Or do the http post and put methods have to send a response back to the browser? Obviously notthing is actually needed to be sent back to the browser and to the mobile site it doesn't even really matter if the request is a success or failure.
If a response does have to be sent to the browser what should my MVC 3 controller return? Can just a head with success or failure be returned?
Finally which of the http POST and PUT methods is best for this and what will be the best data format to use?
Do a POST to an action on the server.
I would suggest you use JSON for the data as it is quite lean and makes it easy to decode. However, if you wanted to make it really lean you you could just use a parameter on the url, like ..../reportPerf?d=123 and then your action should take a parameter "d".
The server does need to respond, but you can just
return new HttpStatusCodeResult(200)
That will only take a few bytes.
Sorry for the brevity, on a mobile device.
This is a followup question to the one here
Here's briefly what I am trying to do. The File server creates a text file to indicate an end of the process. On a webpage on the Web Server, I loop every x seconds and make an ajax request to find out if the test file exists (ajax request to http://fileserver/files/UserFile.txt)
I've tried the following approaches so far:
Trigger a web method from the client side that creates a HttpContext object to verify if the text file exists. But this is too strenous on the server and I started getting all kinds of exceptions in the Event Viewer.
YQL works great but unfortunately it's too slow. The user waits atleast twice the amount of time.
I am looking for a solution that doesn't involve the server side. Somehow, I'd like to use JQuery to verify the existence of a text file on the fileserver.
Any thoughts?
You should be able to use JSONP and JQuery.ajax() to do cross-domain request work. Play with the jsonp and jsonpCallback attributes. Alternatively, you can use JQuery.getJSON().
Serving a single file from the filesystem is the most simple operation a web server can do. If that is already too much, then all other solutions will be worse. Find out why the server takes so long to serve a simple file and fix that.
Note: I'm assuming that the file is small since you say "test file". If it's a big file, the server will actually send it to the client which will need a lot of resources.
What you can try is to add an ASP page to the web site which runs code on the server that checks whether the file is there and just returns a tiny piece of HTML which you can add to the page with jQuery.load().
I may be miles off base here but... could you not create ONE asynchronous (!) Ajax client request with a HUMONGOUS timeout. Fire it, and wait. You would be invoking some server script that checks every so often, in a loop on the server (using sleep in between), whether the file exists. And not replying to the Ajax request until the file finally shows. The server script then replies and exits.
EDIT: Depending on the server-side scripting framework used, you may even get some OS support. You may be able to sleep on a status change in the directory...
I have a situation where a display on a webpage needs to be updated at random. I am wanting to do this in AJAX but am not sure how to do this other than to do a
while(true) { ajaxFunction(); sleep(1) }
Type thing.
The problem with this is that the webpage needs to be updated very quickly on a change to the server, but the changes could happen very sporadically sometimes never.
EDIT: This is an Iphone application using a UIWebView, is it possible to use the iPhone's push notification to interface with the javascript?
Thanks!
I think what you're describing is Comet and there are a couple of plugins for jQuery:
http://code.google.com/p/jquerycomet/
http://plugins.jquery.com/project/Comet
The only way I can think of is to constantly query it. Just keep your responses small so you're not moving a bunch of data around for no reason. You could even suspend the response from the server until data is available.
setInterval(function(){
$.get("updates.php", function(result) {
alert(result);
});
}, 5000);
Might even build some logic into updates.php to cancel the setInterval() after 10 minutes of inactivity on the users part. That will kill off constant requests from users who are no longer logged in.
You should consider implementing some kind of a comet (server push) technology, when you want to optimize the servers load. If you only have a few users, than a polling solution is suitable.
About comet: comet technologies are nothing else, but making a simple http request to the server, where the server does not respond to the request immediately, but waits until there is something to respond with. Until than the thread on the server is suspended.
There are some technical aspects you should consider when implementing a server push technology (like where should I suspend the thread). It is best to use an open source one. It is easy to find them on the web if you search for comet.
If the changes are sporadic, consider not using any AJAX. Wait for the user to refresh or revisit the page.