Jump to content

How to update webpage automatic from serverside.

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
4 replies to this topic

#1
kjartani

kjartani

    Newbie

  • Members
  • Pip
  • 2 posts
Hello
Im working with a challenge.
Lets say I have entered a webpage and that it has been loaded by my browser.
What I want is to update some of that webpage content automatic from serverside without any interference from client.
I have been searching around without finding anything about this.
Can anyone help.

Regards

Kjartan Iversen

#2
dbug

dbug

    Programmer

  • Members
  • PipPipPipPip
  • 155 posts
The server cannot initiate an update of the content already downloaded by the client (using a standard web browser). What you can do is include some javascript code in the page sent to client to use a dynamic update using AJAX and javascript from the client side. When some event is signaled (mouse click, timer, ...) you can send an AJAX request to the server. The answer of the server can later be processed by javascript and merged with current content without reloading the entire page.

#3
kjartani

kjartani

    Newbie

  • Members
  • Pip
  • 2 posts
I am aware of the way it has been done till now but I recall reading somewhere about a way to update from serverside by reusing the already established "session" beetween client and server.
Sorrily I know very little about this communication (maybee someone has a tutorial about this?) to try to fix this from the bottom.
I will continue searching about this question and will appreciate further replies to my tread.

Kjartan

#4
dbug

dbug

    Programmer

  • Members
  • PipPipPipPip
  • 155 posts
I think there is a way to do this, but it requires to mantain the connection open with the client once it has been stablished the initial connection. This means that the PHP script must not end, and from time to time send new data to the client (probably in the form of small javascript fragments to update the content). This is not the right way to do this, and I'm not sure if it works with all browsers, because it's possible that some sent data won't be processed inmediately by the client once received.

Some time ago I saw something similar. I think they send all the html data (till the </html>) and later send <script> tags with additional code. I think some browsers (maybe not all) will process this extra scripts, so the server can make updates when he wants. I haven't tried it, so I don't know if it works.

Anyway, this requires to mantain an open connection for each client. This seems a very high resource consuming solution, not applicable to servers with many clients.

#5
Alexander

Alexander

    It's Science!

  • Moderators
  • 4,124 posts
I will definitely warn you that this challange is rather stupid (learning wise), but that is the last of that. You will want to tell Apache to turn off automatic buffering (implicit flushing, etc), especially by zlib with the PHP ini setting output_buffering="0", or .htaccess entry php_value output_buffering "0".

The theory is, you will flush the results of the output buffer to browser maintaining Apache's child connection, freeze the thread 5 seconds, and flush out the modifying JavaScript:
<?php
// Warn IE, Opera of incremental rendering
print str_repeat(" ", 256)."<pre>"; 
flush();

// Flush current text out of buffer
print "PAGE CONTENTS...  <span id='foo'>Not done</span><br/>\r\n"; 
flush(); 
sleep(1);
echo "\r\n";
flush();
sleep(5);

//Flush secondary buffer to browser
echo "<script language='JavaScript'>document.getElementById('foo').innerHTML = '<b>Done</b>';</script>\r\n";
flush();
?>
If your Apache is not set up much different from defaults, this silly example should work in most browsers. For your note: This is the only way you can edit the "output buffer", it would be silly to think you can change the page with it other than using JS.
Be sure to read the updated FAQ! || Health is achieved through the same 10,000 steps.
If a suggested code/method fails, informing us is less important than telling us why or what errors occurred.