I have a system where I inject a script tag into the dom.
The browser tries to load the script url, which is actually a long-post to a Tornado server.
I'm trying to implement an "abort" feature, where I stop the long poll. I need to do this because I am long-polling multiple different URL's which eventually exhausts the browser's socket pool for my server.
I have tried removing the specific script node from the DOM, but the browser is stubborn and continues waiting for a response from the server.
Is it possible to tell the browser to stop trying to load a resource (specifically a javascript file) once I've included it into the DOM?
You could try an asynchoronous abort "mission" and kill the pid of the request. Thsi kind of tehnique is very similar to this post Cancel PHP script with XHR? so you could run an extra ajax call for an abort which handles it like in the post above. Happy new year!
Related
I have made an application in which i make Ajax call to node server, but the problem is that, if user redirects to another page then also server continues to process the request made by Ajax call and after completing that request, it starts processing the new redirect request.
I am using express as a framework,i don't want to know about client side abort functions, all i need to do is stop server side processing of request made by Ajax call, and instantly start processing for the new request.
all i need to do is stop server side processing of request made by Ajax call, and instantly start processing for the new request.
If your server is not already processing new requests whether the old request is still processed or not, then you have a bigger problem that cannot be solved just by cancelling the requests that are not needed any more.
Your server should always process new requests no matter if the old ones are still processed or not. To make sure it does you need to make sure to never use any blocking call in your handlers. No functions like fs.readfileSync (or anything with Sync in the name), no long running for or while loops (unless they are in async function and don't block the event loop) etc.
When you don't block on old requests before you handle the new ones, you will not have the problem that you describe in your question in which you ask about a specific solution instead of the problem, and the solution you ask about has two issues: it wouldn't solve the problem anyway and it would be impossible to implement.
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 busy developing a Firefox extension and I have a Widget that opens a Panel. How do I have a background script that is constantly running/polling the server to fetch updates. These updates would then need to be injected into the DOM of the Panel.
All of your extension scripts are "background scripts". What you are asking about seems to be a way to poll the server regularly - you use the timers module for that, method setInterval(). The callback would than use the request module to send a request to the server.
As to communicating information to a widget/panel - you use the usual approach to send messages to the content scripts running there.
I have a web page where I redirect the user to if I see that IP is not valid. I want that user's browser will get no response but kept into waiting state, but I also want that my server thread is not blocked for that request. So the idea was that server will response quickly but the user' browser will put into waiting loop to discourage the user. How can I achieve this easily? Is it possible without JavaScript? If JavaScript is the only way then suggest that solution.
Thanks
The problem here is that HTTP is a request/response protocol. If you are redirecting the user, you're responding to that original request. Unless the user generates another request (via javascript or whatever) then the server can't respond again.
You don't need to put the browser into a 'waiting state', if the server simply doesn't reply to the request the browser will just keep on waiting.
I think you're trying to say that your IP task takes a long time, and how to deal with that effectively?
Perhaps if you clear up your question I can answer more fully.
No this is not possible. The problem is to keep the browser waiting with no response you need to keep the connection open and not write to it. The result of this will be:
It will cause your server to run out of connections and use memory to maintain connections
The client will timeout, even if you do not reply
You could purhaps reply with a page that contained a java script with an infinate loop.
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...