devios1
devios1

Reputation: 38035

What's the best way for the server to send messages to a web client?

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?

Upvotes: 3

Views: 399

Answers (4)

sipsorcery
sipsorcery

Reputation: 30734

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.

Upvotes: 1

Chris S
Chris S

Reputation: 65456

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.

Upvotes: 0

kemiller2002
kemiller2002

Reputation: 115538

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.

Upvotes: 0

Sophie Alpert
Sophie Alpert

Reputation: 143194

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

What you're looking for is Comet.

Upvotes: 5

Related Questions