Asked  7 Months ago    Answers:  5   Viewed   37 times

Do I need to pass back any HTTP headers to tell the browser that my server won't be immediately closing the connection and to display as the HTML is received? Is there anything necessary to get the HTML to incrementally display like flush()?

This technique used to be used for things like chat, but I'm thinking about using it for a COMET type application.

 Answers

53

Long polling is a common technique to do something like this; to briefly summarise, it works as follows:

  1. The client sends an XHR to the server.

    • If there is data ready, the server returns this immediately.
    • If not, the server keeps the connection open until data does become available, then it returns this.
    • If the request times-out, go back to 1).
  2. The page running on the client receives this data, and does what it does with it.

  3. Go back to 1)

This is how Facebook implements its chat feature.

This article also clears up some of the misconceptions of long-polling, and details some of the benefits of doing so.

Wednesday, March 31, 2021
 
Jeff
answered 7 Months ago
72

There's not much technically wrong with your code so far - you just need to actually do something with it.

In your PHP file, instead of return $rowdata;, you need to output it in some way. Currently, it's just sending a blank document back to the javascript, so you'll need to echo out the code. Normally, when using a number of objects to be returned to javascript, JSON is a good format. Check out json_encode.

On the other side, in the js, you'll need to take the response and update the page in some manner. Currently, you're just returning it again.

I suggest you go through a few ajax tutorials, and consider using a framework such as jQuery to do the heavy lifting for you. You might also want to do a bit of reading on this topic, as you have some fundamental misconceptions.

Saturday, May 29, 2021
 
Yrtymd
answered 5 Months ago
26

jQuery is not necessary to do this:

<script>
function writetostatus(input){
    window.status=input
    return true
}
</script>

However, most newer browsers prevent you from setting the text in the status bar from JavaScript.

Friday, June 18, 2021
 
Marcelo
answered 4 Months ago
87

You may want to look at WebChat2 This project uses comet, AJAX, and a custom HTTP server to communicate with IRC via sockets.

Wednesday, July 28, 2021
 
tika
answered 3 Months ago
70

Perfmon in my opinion is one of the best tools to do this but make sure you properly configure the sampling interval according to the time you wish to monitor.

For example if you want to monitor a process:

  • for 1 hour : I would use 1 second intervals (this will generate 60*60 samples)
  • for 1 day : I would use 30 second intervals (this will generate 2*60*24 samples)
  • for 1 week : I would use 1 minute intervals (this will generate 60*24*7 samples)

With these sampling intervals Perfmon should have no problem generating a nice graphical output of your counters.

Friday, August 20, 2021
 
Gumbo
answered 2 Months ago
Only authorized users can answer the question. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :