How to interact with a web server using JavaScript

I am trying to develop an interactive chat application using the open source web server AppWeb.
I need to have some kind of machinism that will allow the web server to send updated messages to the client, so when the remote usre sends messages that will be automatically updated on the client side.

There are several ways to do this using HTML5 Web Sockets and server-sent events.
But we need to implement it in HTML and JavaScript only not HTML5.

So, I need some pool of machanism that will support pooling my web server for new events.

So, how should I write a mananism pool in Javascript using Sockets.
How should this be implemented on the server?

Thanks!

+4
source share
2 answers

There are already some examples ... depending on the server side, you can go java-hello-world or php-hello-world or ...

if you cannot use websocket , you have to go the old way, create the window.setInterval interval and pull the data from the server, for example. $.ajax() . I don't know any other alternative to bi-directional connection (websocket) ... see kayahrs answer

as you asked for this:
$.ajax() is a jQuery way to do xhr . basically, it runs an asynchronous request to the server, which returns xml or json or text or ... (independently). when this request returns, a supported eventHandler is triggered, and you can respond to the response. you can also use plain xhr, but this is a bit inconvenient for handling the original xhr.
jQuery supports some shorthand overloads for $.ajax() , for example. $.getJSON() , $.get() , ...

implementation example:

 $.get("test.cgi", function(data){ alert("Data Loaded: " + data); }); 
+2
source

There is another way to send messages from the server to the client. You should use an iframe for this, which connects to a PHP script (or any other method that you use on the server side) that does not close the connection. The PHP script then sends JavaScript messages whenever the client needs to be informed about something. After each message, the server flushes the output stream to ensure that the data actually finds its path to the client and is not cached by some sort of output buffer. Here is a small example code of a PHP script loaded in an iframe (not tested and not complete, just to show the basics):

 <html> <body> <script type="text/javascript"> function receiveMsg(data) { // Do something with the data, for example send it to some function // in the parent frame (Where your chat application lives) } <?php while (true) // You may also implement some abort state which should // be checked here { $data = waitForData(); // This is your magic function on the server // which waits for data to be send to the client echo "receiveMsg('" . $data . "');"; // Let say data is just a string. // You may want to use JSON instead flush(); } ?> </script> </body> </html> 

The advantage of this method is that it does not rely on polling. Therefore, you do not need to send requests to the server every x seconds. And when you do everything right on the server side, messages sent by one user are received as quickly as possible by other users, and not after a few seconds. The downside is that you have a permanent HTTP connection for each chat user. But the server may require fewer resources, and then dozens of full HTTP requests per minute for the chat user.

+1
source

All Articles