Click to See Complete Forum and Search --> : client alert with Javascript


mwong7025
May 23rd, 2005, 12:54 PM
I have a program that serves as a web server and send information to the web clients when they are connected to it using Java script. I need to implement something that can send alerts to the web clients from my program. I was told the only way to get the alerts is by having the web clients polling for it periodically, and my program can send the alerts (if any) to the clients. One side effect of this is client page will get updated (refreshed) when the polling occurs which I don’t care much about. I am looking for ways to do this, any ideas? I think what I need is something like the live news ticker that it can send update news to the client.

wildfrog
May 23rd, 2005, 02:49 PM
One way to do this is to have a seperate "hidden" frame that periodically polls your server, and if there are any "news" then the server returns a script that updates your "main" frame.

Another way to do this (using nothing but HTML and scripting) is to return a "never ending" multipart response to the client, like:


HTTP/1.1 200 Ok
Date: Wed, 23 May 2005 21:21:24 GMT
Last-Modified: Wed, 23 May2005 21:21:24 GMT
Content-type: multipart/mixed; boundary=THIS_STRING_SEPARATES

--THIS_STRING_SEPARATES
Content-type: text/html

<html>
<script>
function yourAlertFunc(...)
{
// your alert code here
}
</script>
<body>
<!-- your html code here
</body>
</html>
Now the browser has enough to render the page, but instead of closing the connection the server goes into some sort of "sleep" mode, waiting for the "news" to happen. And when it does, the server continues its response:

--THIS_STRING_SEPARATES
Content-type: text/html

<script>
yourAlertFunc('this is an alert!");
</script>
Now, the browser should append this code to the end of the already received code and then exceute the 'yourAlertFunc'.

It's been a while since I've done this, so there are probably some bugs in my example (you might have to dig around in RFC2616), but with some tweaking it should work for most browsers.

Beware or users hitting the escape key or firewalls, proxies and other network components trying to tear down your "idle" HTTP connection.

- petter

mwong7025
May 23rd, 2005, 04:36 PM
Thanks for your response.

Can you send me some sample code as to how do it with the first method - have a seperate "hidden" frame that periodically polls your server.