Can Javascript pull data from across the internet?

I need a client that does monitoring of some remote equipment, shows status up/down, and basic statistics about the state of the equipment. On the client side, I’ve got HTML and Javascript only. On the server side I can deliver pretty much anything.

I can change the .src on an image and thereby force a reload of an image. So for status, I can change an image on the server between say green.gif and red.gif. But I can’t find a way to get raw numbers from the server to the client. Ideally I’d like to make a socket call. Second choice would be to invoke and read the response of a CGI call. Neither seem to be options for javascript.

Any ideas? Thanks.

Not really. Javascript is mostly limited to manipulating objects on the page you’re on, refreshing or redirecting that page to somewhere else, or opening or closing windows.

What you want done is usually achieved with some server-side code, such as CGI or servlets (or ASP or JSP or PERL or PHP or… ), which prepares the page before it’s sent to the browser.

Preparing the page in your case includes getting the remote data and formatting it for display.

OTOH, if you have control of the local and remote sites, you could open the remote site in a window and execute a javascript function on it, which returns the data you’re looking for. To display it on the local site, you’ll have to use either dynamic HTML, or display the contents in form fields.

Does this make any sense?

You could create an invisible frame on the client side which is refreshing every couple of seconds, each time pulling data from a server CGI script. The main frame that the client sees can be in a setTimeout() loop, checking the invisible frame for new values then updating when necessary.

If your server is running IIS, you can use ASP code to get data from the server to the client (without refreshing the page). Just set up a global variable at the beginning of the page (in ASP), and then you can reference it within the client-side javascript.

You would do something like:

(at the beginning of the page)


<%
dim x
x = DatabaseRetrievalFunction()
%>

(then in the Body of the page)


<SCRIPT>
function servertoclient(){

servertoclient = <%=x%>;

}
</SCRIPT>

Also, if you want the data to be continuously updated, you can make a call to the ASP function within the javascript, the same way that you reference the ASP variable.

The simplest solution, IMO, would be to have the server generate a dynamic page with all relevant data every time it is hit. Then add a Refresh in the header to have the client pull the page every five seconds or so.

If your client has Java you could write a Java applet to do it in real-time, but Java generally pisses me off.

Javascript is a glue language, and by itself, can’t do much. When I developed a solution to this problem, I built custom objects that live on the server to dynamically query the status of any machine I was interested in. I’m going to assume that you’re using vanilla javascript with none of the enhancements that allow you to interact with objects. You need some sort of middle tier to assemble the information for you in a format javascript will understand. It really doesn’t matter what you use to build it as long as it has the ability to produce a text file. The simplest solution is to have your middle tier periodically produce a javascript file that can be included in your client page. Here’s an example (psuedo-code):

My server might produce a data file that looks like this:

var StatusCode[] = new Array[5];
StatusCode[0] = 100;
StatusCode[1] = 200;
StatusCode[2] = 100;
StatusCode[3] = 100;
StatusCode[4] = 200;

or, more easily:

var Server1 = “active”;
var Server2 = “disabled”;
var Server3 = “unknown”;

Then, this data file, which is really a javascript code snippet, can be included in your html client page like so:

<HTML>
<HEAD>
<SCRIPT SRC=“myDataFile.js”></SCRIPT>
</HEAD>
<BODY>
Include whatever other javascript functions you like to process the variables that are now available to you.
</BODY>
</HTML>

That satisfies the requirements of your original post - only HTML and Javascript on the client. Once again, the trick is creating an object on your server that can produce data in a format usable by javascript (without file i/o or object creation).

Thanks all, for the suggestions so far…

evilhanz, I like your line of thinking. Won’t the data that is passed to the page be static, though? so on the server side if a new array .js is created, an existing page won’t be updated with that info. true?

friedo, Marcus, this is the conclusion I’ve come to so far as well, and what you describe (auto-refreshing page that is periodically rebuilt on the server)is exactly what I’m doing right now, but I’d really rather have some way of refreshing items individually. Baraqiyal, doesn’t the invisible frame really do the same thing? Is there a benefit of the invisble frame over a refresh meta tag?

Starbury, this is exactly what I’m looking for, but unfortunately, I won’t be using IIS on the server side. If that functionality exists and can be read by either a MS or NS browser, there must be some way I can build it myself. Any thoughts?

The advantage to using the hidden frame refresh trick is that your page won’t continually flicker. You can read the values with a timed function in your Javascript and use DHTML to change the client data. Then there will be no flickering.

I’m not sure that Starbury has a handle on how ASP pages work, because his example won’t do what he says it will, unless I misunderstood his description. The technique he offers is a way to build dynamic HTML pages, but it won’t update itself automatically - there is no way the server can tell a client to update itself, because HTTP is a connectionless protocol. Once a page has been served, the server no longer knows anything about the client other than what it received in the last header.

There are a number of techniques for doing what you want to do. If you can force all your users to use only Internet Explorer, you can use one of the data source objects and come up with some kludge that way. XML is a possible solution here - you can set up an XML data source, point it at a URL on the server, and then set up a timer to trigger an update of the data every second or whatever. Then just make your server write your machine data out in XML format.

If you have to support many different browsers, your best solution is probably a Java Applet that you can embed on the page. You can either build the User interface into the Java applet, or just use the applet to grab the data for you regularly. That’s the standard technique used on other real-time web pages that provide things like stock tickers.

Is this an industrial automation environment? If so, how fast is your data changing? There are a number of commercial products out there that do what you’re trying to do, and you should look into them. Check out http://www.indx.com, and http://www.lighthammer.com.

Sure. With an invisible (actually hidden) frame, the screen your client is looking at will only refresh when it is necessary. This will save your client’s eyes from having to look at a constantly refreshing screen and also save a lot of bandwidth.
Given the specifications of only HTML and JavaScript on the client side, I’m pretty sure this is the best way.

Here is a nice set of javascript routines you can drop in a page which will do what you want. The example is a simple addition program that sends the numbers to a CGI and gets the sum back from it. It uses the hidden frame technique. The basic idea is:

[ul]
[li] hiddenframe.src = “mything.cgi?arg1=foo&arg2=bar”[/li][li] the CGI returns a document with javascript in it which sets “parent.myresults” and signals that it’s done[/li][li] the main document sees that the CGI is done and looks at the javascript objects that the subdocument in the hidden frame poked.[/li][/ul]

Don’t tell my consulting company about your suspicions, Sam, or I might be out of a job. It’s only a matter of time until they realize I don’t know what I’m doing…

Anyway, perhaps I’m misunderstanding what he is trying to do, but the code that I wrote will allow javascript to access that variable created by the ASP whenever he needs it.

Bill what type of server are you running?

Starbury, I believe what Sam is referring to is the fact that the ASP part of your page is never going to get re-executed. In other words, if the client side code waits several minutes and re-checks the value of “x”, it’s going to be the same. I think Bill was looking for something which would allow him to re-run some client code to get the updated status from the server. The only way your DatabaseRetrieval() function is ever going to get re-executed is if you reload the page, which he’s trying to avoid, as I understand it.

galt is right on the money. I want a client that - without any key presses or other prompting from the user - will update, say every 5 seconds, with data from a server. The server is running Linux w/Apache, but I’m pretty open to what else can run on the server. I’d prefer not to be constantly refreshing the page with a meta tag, since the refresh won’t be very pretty.

If I could run c code in a browser, what I’d do is open a socket connection to the server, and push through data periodically. But instead I’m running Javascript.

Using my scheme, the user won’t have to look at a constantly refreshing page. All the refreshing would be done in a –>hidden<– frame. When the main page detects that an update is needed, you can refresh or just use JavaScript to update the graphics or text on the screen.

Bill, I made a slight modification which can be found at http://www.codsquad.com/~galt/fib.html, so you can see that 1) the page is not refreshing (notice the bouncing text is continuously moving), and 2) no user input is required to initiate the communication.

Bill, it seems to me that what you <b>really</b> want is Remote Scripting. http://msdn.microsoft.com/scripting/ It’s somewhat difficult for first time users to get it working, but it handles all of your requirements. The remote scripting libraries embed a small java applet in your pages that broker requests to the server. Your responsibility is to write a few custom functions - some that live on the server, and some that live on the client that perform your custom task. On the client page, set your function to execute periodically, and voila - no refreshing. The function only grabs the specific information it needs from the server. There are better technologies than this for accomplishing what you want to accomplish, but it is the best solution given your responses to the other posts.

(I know you said HTML and Javascript only, but modern browsers come with Java support, so you might as well use it. Besides, you don’t have to code it - the work’s already done.)

Bill, it seems to me that what you <b>really</b> want is Remote Scripting. http://msdn.microsoft.com/scripting/ It’s somewhat difficult for first time users to get it working, but it handles all of your requirements. The remote scripting libraries embed a small java applet in your pages that brokers requests to the server. Your responsibility is to write a few custom functions - some that live on the server, and some that live on the client that perform your custom task. On the client page, set your function to execute periodically, and voila - no refreshing. The function only grabs the specific information it needs from the server. There are better technologies than this for accomplishing what you want to accomplish, but it is the best solution given your responses to the other posts.

(I know you said HTML and Javascript only, but modern browsers come with Java support, so you might as well use it. Besides, you don’t have to code it - the work’s already done.)

Thanks all. Appreciate the help.