Comet applications without using IFrames - javascript

I am just getting started creating an AJAX application using server side push. I am using Grizzly Comet on Glassfish V2. Almost all the sample applications use IFrames for updating content on the client side. I want to stick to using JavaScript only. Are there any such sample applications (preferably ones that do not use a JavaScript library)?

The point about the push is that the HTTP request never ends. Not all client implementations handle that correctly. As I see it, it's possible to do it with pure javascript in Firefox with its extensive API, but the XMLHTTPRequest object would timeout and would not be able to stream you the content.
IFrame is good, you could also try the object tag if its about standard compliance.
Before XMLHTTPRequest and Ajax had no name, we used to get data from the servers through IFrames.

This is how I figured out how to do a push with just javascript and php.
Have javascript do an initial call and load content into a div.
Then have the javascript call back to the php and have the php sleep until it see there is a new update.
Then send out the data to everyone and call back to php and sleep again.
This allows for long polling and fewer calls back to back. I personally put a time of 5 mins to 30 mins on the php script.

Related

Is there any way to send http requests from JS by pushing button, to a C++

I am currently designing a GUI for a piece of hardware. We want the GUI to be able to be accessed over a browser. The browser would be displaying a map generated from C++ code, but I also need to send some params to the code that generates the map from buttons on the JS front end? Is there anyway to accomplish this. I have done a little research so far and know about web sockets and AJAX, but I am not entirely sure it is what I am looking for. In an ideal world I would be able to just send UDP packets, but my research tells me that is not possible, not is TCP. Is this correct?
Thank's in advance for any help!
Your C++ code could setup a server that listens for requests. You could use libhttpserver, for example. Then, in your JavaScript code, you can use XMLHttpRequest or the Fetch API (for newer browsers) to make an HTTP request to the server, which would then return a new static page (with the generated map embedded into the page).

Post json to server in regular intervals in plan javascript

I am building a javascript plugin that a client will include in his. The plugin will track data user activity like page scroll, button clicks mouse movements etc and then save to web storage. This data then need to be send to a back end server at regular intervals of say 5-10 seconds in json format.
If I use a simple ajax for sending data by using setInterval method
will that impact user browsing experience.?
Will using websockets make more sense in this case or is there any other way?
NOTE: The backend server application is based on Spring MVC and the javascript plugin will push data to server externallly from clients application.
If you really wanted minimal impact on the UI thread, you could spawn a WebWorker and do the AJAX requests from there.
Here is a good answer explaining AJAX requests in the context of a WebWorker.
Here is another short example of implementing AJAX in a WebWorker.

Javascript send string to console application

I want to execute some javascript that will send a string of text to a c++/java console application running on the same machine as the web browser
What javascript should I execute? and how should I receive the string?
As far as I know, there are two ways to transmit data to somewhere else using JavaScript. The first is using an XMLHttpRequest, the second is using a WebSocket. In both cases, the JavaScript code will establish a connection to another program, which in this case you want to be your C++/Java program.
In the first case, if you want to communicate using an XMLHttpRequest (or other libraries that use this, such as jQuery's get or post), you'd need to make sure that your C++/Java application starts a small webserver. This way, the JavaScript code can establish a connection to it and send data. I'm sure there are C++/Java libraries which you can use for this, but even if you can't it should be fairly simple to get something to work with just plain socket code. The text-based HTTP protocol that you need for this is not that difficult.
In the second case, you'd need to make sure that a WebSocket server is started in your C++/Java application. Your JavaScript code then can connect to this server and just send its data. I'm not that familiar with the WebSocket protocol but I suspect that it's slightly less trivial, so using a library for this would be a good choice.

Updating Client Side DOM from Node.js

I have re-written this description as the original was not clear at all, I think I can summarize this much easier with:
using node.js, is it possible to update the client side DOM without leaving the original connection open that created the page headed and body, and doing a response.write() with each new update ??
Effectively I am looking to update the page in the browser from node.js after the page has loaded and the connection closed ??
Thanks
The way I see it, this has nothing to do with whether you use node.js or PHP or whatever, if you want to modify the DOM on the client side, after the HTTP request finishes, these are your two options. Either you
use a combination of javascript's setTimeout() and requests to the XHR object to poll a JSON or HTML fragment (I won't go into AJAX polling here), or
use websockets. These are connections that stay open so you can pass messages back and forth while your page is running. Look it up.

Cross Domain requests using JQuery

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...

Categories