views:

214

answers:

4

First let me explain the data flow I need

Client connects and registers with server
Server sends initialization JSON to client
Client listens for JSON messages sent from the server

Now all of this is easy and straightforward to do manually, but I would like to leverage a server of some sort to handle all of the connection stuff, keep-alive, dead clients, etc. etc.

Is there some precedent set on doing this kind of thing? Where a client connects and receives JSON messages asynchronously from a server? Without using doing manual socket programming?

+1  A: 

The problem is that HTTP is a request response protocol. The server cannot send any data unless a requests is submitted by the client.

Trying to circumvent this by macking a request and then continously send back responses on the same, original, requests is flawed as the behavior does not conform with HTTP and it does not play well with all sort of intermediaries (proxies, routers etc) and with the browser behavior (Ajax completion). It also doesn't scale well, keeping a socket open on the server is very resource intensive and the sockets are very precious resources (ordinarly only few thousand available).

Trying to circumvent this by reversing the flow (ie. server connects to the client when it has somehting to push) is even more flawed because of the security/authentication problems that come with this (the response can easily be hijacked, repudiated or spoofed) and also because often times the client is unreachable (lies behind proxies or NAT devices).

AFAIK most RIA clients just poll on timer. Not ideal, but this how HTTP works.

Remus Rusanu
When you say "pool on a timer" what do you mean exactly? Did you mean poll?
DevDevDev
yes, poll :) Send an HTTP request every N seconds, to check if anything changend in the state.
Remus Rusanu
Hmm the overhead of that on the client side might be too much.
DevDevDev
Many newer RIA clients are using Comet/Ajax Push instead of polling/Ajax Pull, as Comet has the advantages of faster update and less requests. Comet is what Gmail and Facebook chat use.
Kaleb Brasee
Exactly, the overhead becomes too great. I've experienced this myself while implementing a polling Ajax chess web app, before you know it the clients are sending tons of requests to keep up-to-date.
Kaleb Brasee
I wasn't aware that 'long polling' as described in the Comet link has a technical moniker, but I was placing it in the same bracket as 'timer polling'. I'm sure is better than timer based though.
Remus Rusanu
Right I know how to all of this using Javascript and a browser using Orbited specifically, but is there a Comet implementation on the iPhone then?
DevDevDev
+2  A: 

A possible solution is known as Comet, which involves the client opening a connection to the server that stays open for a long time. Then the server can push data to the client as soon as it's available, and the client gets it almost instantly. Eventually the Comet connection times out, and another is created.

Not sure what language you're using but I've seen several of these for Java and Scala. Search for comet framework and your language name in Google, and you should find something.

Kaleb Brasee
+1 the Wiki article is very good starting point
Remus Rusanu
I am aware of Comet, but I have only seen robust client solutions to this written in JS.
DevDevDev
I've heard of a Flash app that enables you to communicate over a port connection instead of using JS, but I don't remember what it's called.
Kaleb Brasee
Oh yeah, Juggernaut for RoR.
Kaleb Brasee
Jetty has a comet application. I would imagine there are several other app servers (e.g. Tomcat, JBoss, etc) that also have equivalent implementations.
Vitali
A: 

GWT provides a framework for this kind of stuff & has integration with Comet (at least for Jetty). If you don't mind writing at least part of your JavaScript in Java, it might be the easier approach.

Vitali
+1  A: 
  1. In 'good old times' that would be easy, since at the first connection the server gets the IP number of the client, so it could call back. So easy, in fact, that it was how FTP does it for no good reason.... But now we can be almost certain that the client is behind some NAT, so you can't 'call back'.

  2. Then you can just keep the TCP connection open, since it's bidirectional, just make the client wait for data to appear. The server would send whatever it wants whenever it can.... But now everybody wants every application to run on top of a web browser, and that means HTTP, which is a strictly 'request/response' initiated by the client.

  3. So, the current answer is Comet. Simply put, a JavaScript client sends a request, but the server doesn't answer for a looooong time. if the connection times out, the client immediately reopens it, so there's always one open pipe waiting for the server's response. That response will contain whatever message the server want's to send to the client, and only when it's pertinent. The client receives it, and immediately sends a new query to keep the channel open.

Javier