views:

168

answers:

4

Links to articles would also be appreciated--I don't know the terminology to search for.

I'm looking to learn how a web application can allow for server-to-client communications. I know the web was not designed for this and that it has been something of a hurdle, and am just wondering what the state of this is, and what the best practices are.

An alternative is constant or occasional polling via ajax, but is it possible for web servers to maintain stateful connections to a web client?

Edit: Another way to ask this question is how does StackOverflow tell a page that new posts are available for it to display that little bar at the top?

+5  A: 

StackOverflow polls the server to check if there is more data.

What you're looking for is Comet.

Ben Alpert
Ah yes this rings a bell--the long-running request. The problem with this is it creates the appearance of a perpetually-loading web page. Personally I find those awful.
chaiguy
A: 

Web browsers really aren't set up to handle this sort of communication. The communication is a one way street where the web server is listening on a port (typically 80 or 443) for for information to be sent to it.

I just read the link on comet, and it's interesting approach, but what has to be remembered is that it is still technically being opened by the client. The server is sending code for it to execute, but the browser is ultimately in control and determines when the server communicates with it.

With today's web browsers the server can never technically execute a message being sent to it without the help of the browser. Technically you might be able to get around that by executing some Active X control on the client machine...but I haven't tried it.

Kevin
A: 

You can't, HTTP is stateless. A long time ago Netscape implemented HTTP Push but it wasn't a sucess.

I'd use polling with a web service or similar; no plugin (that is Flash, Java,Silverlight) will have rights in its sandbox to use raw sockets so it'll be a waste of time trying to implement it that way.

Chris S
+1  A: 

To get true two way communications from a browser you need to use a plugin technology like Silverlight, Flash, et al. Those plugins can create TCP connections that can establish a two way persistent connection with a server socket. Note that you can't really establish the TCP connection with the HTTP server so you'd have to create an additional server agent to do the communicating back to the browser.

Basically it's a completely differnet deployment model to what AJAX sites like Stackoverflow, Gmail etc. use. They all rely on the browser polling the server at set intervals.

sipwiz
Fascinating concept! Do you suppose that it would be possible to write an invisible flash component that establishes a tcp connection, and depending upon a message from the server, could cause certain partial updates of a page?
chaiguy
Yes, although the Flash plugin would have to be already installed. I'm more knowledgeable about Silverlight in it's case you could definitely do what you're describing as it has good hooks back to the hosting page's HTML. Silverlight can use C# as well whereas Flash is ActionScript (yuk).
sipwiz
Yeah Silverlight is definitely the more attractive option from a development perspective, but I'm thinking about adoption rates.
chaiguy