views:

393

answers:

5

I have created a simple chat server that is driven by client polling. Clients send requests for data every few seconds, and get handed any new messages as well as information about whether their peer is still connected.

Since the client is running on a mobile platform (iPhone), I've been looking for ways of getting rid of the polling, which quickly drains the battery. I've read that it's possible to keep an http connection open indefinitely, but haven't understood how to utilize this technique in practice. I'm also wondering whether such connections are stable enough to use in a mobile setting.

The ideal scenario would be that the server only sends data to clients when an event that affects them has occurred (such as a peer posting a message or going off line).

Is it advisable to try to accomplish this over http, or would I have to write my own protocol over tcp? How hard would it be to customize xmpp to my need (my chat server has some specialized features that I would have to easily implement).

+1  A: 

I just found this article myself, which describes the following technique (which I referred to in the question):

... have the client make an HTTP request and have the server hold the request on the queue until there is a message to push. if the TCP/IP connection is lost or times-out, the client will make a new HTTP request, and the delay will only be the round trip time for a request/response pair . . . this model effectively requires two TCP/IP connections for HTTP, client to server, though none permanent and hence mobile friendly

Felixyz
+4  A: 

How about push technology? see http://en.wikipedia.org/wiki/Comet_(programming)

txwikinger
http://stackoverflow.com/questions/337985/comet-server-push-to-client-on-iphone
Russell Leggett
Yep, this is what I had in mind, and the discussion Russell L linked to is very helpful too. Thanks both.
Felixyz
+1  A: 

You might like to check out this project which uses a variety of techniques including Comet. Release details are here, here's a snippet from that page

It’s my distinct pleasure to be able to announce the first public showing of a project that I’ve been working on in my spare time in the last month or two, a new Web Based IRC chat application.

This project brings together a lot of new technologies which had to be developed to make this a feasible, scalable and efficient.

Some of the underlying tools build to make this posible that i consider ’stable enough’ are already released, such as the php Socket Daemon library i wrote to be able to deal with hundreds up to many thousands of “Comet” http connections, and an equal amount of IRC client connections.

Paul Dixon
A: 

I think this is nearly impossible and dangerous. The internet works stateless and connectionless meaning that the connection between client and server is always handled as unreliable. And this is not for fun.

By trying to get a stateful connection you are introducing new issues. Especially from a 3g application. What if the connection breaks? You have no control over the server and cannot push.

I think it would even be easier to send sms/text messages and have an application that handles that.

milovanderlinden
I does seem a lot of people have used it successfully using Cometish approaches (BOSH etc). If the connection breaks... you establish a new one? Would be interesting to hear more about why you think this is nearly impossible.
Felixyz
+3  A: 

I think you're describing XMPP over BOSH.

http://xmpp.org/extensions/xep-0206.html

I've used this http-binding method between a chat server and javascript client on non-mobile devices. It worked well for me.

ire_and_curses