Comet (server-push, ReverseAjax etc.)

Just starting out? Need help? Post your questions and find answers here.
Seymour Clufley
Addict
Addict
Posts: 1233
Joined: Wed Feb 28, 2007 9:13 am
Location: London

Comet (server-push, ReverseAjax etc.)

Post by Seymour Clufley »

For anyone who doesn't know what "comet" is, here's a page. Basically, it refers to any setup which allows for a server to send data to clients when it wants, rather than when a client has requested it.

I've written a small PBI and JS for handling Comet requests. It uses the long polling technique. It works, but with a limitation: if you have two (or more) webpages open, only one of them receives the response. This may be a bug in the JS rather than the PBI - I don't know yet.

I'll post the code for this setup soon (I'd like to try and solve the above problem first). In the meantime I have a question for people who know more about this stuff than me...

The long polling technique is pretty crude, and also not reliable cross-browser because different browsers wait different lengths of time for a response before simply cancelling the request. It also means that your server will have loads of open connections, all the time, waiting to be responded to and closed. And in Chrome, the "Waiting for website.com..." status remains onscreen constantly.

Apart from long polling, there are some other methods.

One of them is server push. That article describes stuff I don't understand, especially this:
the web server does not terminate a connection after response data has been served to a client. The web server leaves the connection open such that if an event is received, it can immediately be sent to one or multiple clients. Otherwise the data would have to be queued until the client's next request is received
But there's a (possibly much better) method: server-sent events. This is the one I'm really interested in. Does anyone have any experience of doing this? If not, does anyone know if this kind of thing is possible with PB?
JACK WEBB: "Coding in C is like sculpting a statue using only sandpaper. You can do it, but the result wouldn't be any better. So why bother? Just use the right tools and get the job done."
Seymour Clufley
Addict
Addict
Posts: 1233
Joined: Wed Feb 28, 2007 9:13 am
Location: London

Re: Comet (server-push, ReverseAjax etc.)

Post by Seymour Clufley »

I've fixed the "multi-page problem". Funnily enough it had nothing to do with JS: it's to do with PB and maps.

In this situation:

Code: Select all

ForEach mp()
  If condition
    DeleteMapElement(mp(),MapKey(mp())
  EndIf
Next
If the condition is #True for the first map element, it doesn't seem to proceed to the second element. At least, that's what I think was happening.

The map had two elements and when the first was deleted it didn't move on to the second, but seemed to think it had gone through all the elements. Weird.

So anyway the Comet framework now works (hopefully) and I've posted it in Tips & Tricks.

Meantime if anyone can help or advise with server-sent events, it'd be much appreciated. :)
JACK WEBB: "Coding in C is like sculpting a statue using only sandpaper. You can do it, but the result wouldn't be any better. So why bother? Just use the right tools and get the job done."
Seymour Clufley
Addict
Addict
Posts: 1233
Joined: Wed Feb 28, 2007 9:13 am
Location: London

Re: Comet (server-push, ReverseAjax etc.)

Post by Seymour Clufley »

Seymour Clufley wrote:Meantime if anyone can help or advise with server-sent events, it'd be much appreciated. :)
Seven years later, I did it myself!
JACK WEBB: "Coding in C is like sculpting a statue using only sandpaper. You can do it, but the result wouldn't be any better. So why bother? Just use the right tools and get the job done."
Post Reply