3

How to create a NATIVE chat app for the iPhone? So far we have been exploring a few options:

  1. Creating a web service using php or other web based language and have the app connect to that. Only problem is we can't figure out how to create "push" messaging with this, where the user will not have to refresh the conversation constantly.

  2. Hosting an application on a server such as Windows Azure which will communicate to the iPhone app using TCP/IP. This way it seems like "push" messaging could be achieved by simply sending a packet to the iPhone. However, we have never done this before and don't know if we would run into any unforeseen potholes.

Have any of you made such an app before? If so how did you go about doing it? If not, what method would you recommend?

Thank you in advance!

EDIT: To tell you exactly what we're trying to do: we need to make an app where a user can join a chat room and send/ receive messages from that chat room. There will also be custom features like that users will have their own profiles, etc. We would also like to make this as flexible as possible, so that we can integrate it on other platforms like android and blackberry later on.

So essentially the part that I'm stuck on is the send/ receive messages from a chat room. What technology should we use server side?

peterh
  • 11,875
  • 18
  • 85
  • 108
Rob
  • 7,028
  • 15
  • 63
  • 95
  • 1
    You may wish to investigate WebSockets, which can help provide some level of push-services to web browsers; [see a demo](http://yaws.hyber.org/websockets_example.yaws). – sarnold Apr 09 '11 at 08:37
  • How scalable does this need to be on the server? ie how many concurrent users? How many in a chat group at the same time? – Jim Morris Apr 12 '11 at 08:22
  • This needs to very scaleable. Will start with maybe 1,000 users and 200 chat rooms, but will hopefully blow up to 100,000 users and 10,000 chat rooms. – Rob Apr 12 '11 at 08:30

3 Answers3

2

Something with an open socket, like Socket.IO could work. Node.js is a good server-side framework to explore. Here's an related SO question: iPhone Objective-C socket communication with Socket.IO

EDIT: Question has changed since posting this answer -- originally question asked about web apps. ALso, originally the question was not clear that you wanted answers about the server side more than the client side.

On the server side, I would still recommend Node.js -- sounds like you want to use C# though, which makes me wonder why you're asking again about what server side tech to use. Most languages will provide you with ways to connect a socket to a client and access a database, which are the two main requirements of the app that it sounds like you want to make. Use whatever language you're comfortable with. However, some are going to come with libraries that may come in handy for this type of communication -- Node.js and Ruby on Rails (more useful if you want to do a polling-based solution)

Look at http://code.google.com/p/cocoaasyncsocket/ for a good library for doing socket communication from the iPhone without having to delve too deep into the low-level functions.

Community
  • 1
  • 1
jnpdx
  • 45,847
  • 6
  • 64
  • 94
  • That looks very interesting. Would it run on a windows server? And could I use C# to program it? Javascript is not my strongest language – Rob Apr 09 '11 at 08:44
  • The same basic principal will work -- here's an SO question where there's an example of an open socket server written with C#. Client side is in JS, but could easily be implemented in Obj-C on the iPhone: http://stackoverflow.com/questions/2211898/c-web-socket-server – jnpdx Apr 09 '11 at 19:58
  • Ok so let me see if I have this correctly: I'd use Socket.IO on the iPhone client, and a websocket as the server, right? But how about just creating a C# program, hosting it online and having my client connect to it using TCP/IP? What would the differences be? – Rob Apr 09 '11 at 21:16
  • Socket.IO is just a protocol for doing communication over TCP/IP -- a way to designate connections/messages/etc -- no matter what language you implement it in, or on what platform, or for that matter, what protocol, you're if you don't want to poll for responses, you'll be keeping a socket open and pushing data through it – jnpdx Apr 10 '11 at 00:49
  • So what's the difference between a websocket and TCP/IP? – Rob Apr 10 '11 at 06:44
  • A websocket is just a way to communicate over TCP --> http://en.wikipedia.org/wiki/WebSockets – jnpdx Apr 10 '11 at 08:14
  • So why wouldn't I just use raw TCP/IP? I understand I'd have to keep the sockets open, but what does that mean from a practical standpoint? – Rob Apr 10 '11 at 22:42
  • +1 on the suggestion for Node.js. Don't be ankle-chained to C# just because that's what your comfortable with. – Ben Scheirman Apr 12 '11 at 00:58
  • So what exactly would be the difference between using node.js vs just hosting a C# program on a server? (I'm assuming hosting a C# program is what I should be doing, right?) – Rob Apr 12 '11 at 20:13
  • I don't think it's very practical for any of us to enumerate the differences between Node.JS and writing a homebrewed C# server -- maybe if you ask a specific question, someone can answer – jnpdx Apr 13 '11 at 01:39
  • Sorry, it's hard for me to ask specific question because I've never done anything like this before. Do you have any links on how to create the very basic server side code for this type of thing? Maybe I just need to read a nice long tutorial. – Rob Apr 13 '11 at 03:34
  • Here's a very basic implementation in Node.js -> http://dhotson.tumblr.com/post/271733389/a-simple-chat-server-in-node-js – jnpdx Apr 13 '11 at 23:04
  • To answer an earlier question, one would use WebSockets from JavaScript in a browser because JavaScript in a browser can't use raw sockets. One would never use WebSockets in a native app (unless you absolutely had to talk to a server that only spoke WebSocket). – Fantius Apr 17 '11 at 00:27
2

I've done this several times. Scaling to 100K concurrent users is non-trivial. If you want an off-the-shelf system I suspect ejabberd may do what you want. although the protocol IMHO is too verbose and uses far more bandwidth than necessary.

If you want to write your own solution and have the flexibility to write your own protocol and have the maximum possible scalability in the future then use a language that allows you to distribute the application across several servers. It is easier to allow that from the get go rather than writing a single server solution then have to retroactively make it distributable.

Having written servers like this in c++, Java and Erlang I would say the easiest and most relevant tool was Erlang. It makes good use of multi core processors and with a good design it facilitates distributing across several servers. C++ was the hardest!

I have also used Java with tools like JETTY and RabbitMQ to write a highly scalable system that required using HTTP as the protocol.

Personally I prefer a custom binary protocol as it allows you to reduce bandwidth to a minimum, and avoids DOS attacks and such as the protocol is well defined and lengths are sent before the packet, where as non binary protocols need to be parsed as they come in, with no idea of how big the packets may be.

Jim Morris
  • 2,870
  • 24
  • 15
  • Yea I've usually done things like this with custom binary protocols as well, but the more I think about security and allowing it to support future features/ technologies the less I want to do it myself. I also have zero experience running my own servers, etc. so I'm thinking about just having our clients use a cloud service such as Azure or GAE. Do you think that would work well? – Rob Apr 14 '11 at 07:21
  • Sorry can't help with those services I've not used them. However if you want to use something more people are familiar with I have recently used JSON as the protocol for a chat system. The only difference is I prepend all JSON strings with the size of the JSON string. This makes the protocol understandable by others, and also somewhat readable over the wire for debugging. If running your own servers is the issue but you are comfortable writing them, have you looked into using Amazon S3 to deploy? it is certainly scalable enough, and you are not locked into any specific O/S or solution. – Jim Morris Apr 14 '11 at 07:23
  • JSON sounds like a great idea, it would be very easy to extend and encrypt. Is the reason why you start the JSON with a sizeof because you want to prevent DOS attacks where the hacker sends million character long strings? Also, S3 looks like it's just a storage service, what about the processing power that's going to be required? – Rob Apr 14 '11 at 07:44
  • Sorry meant Amazon EC2 not S3. EC2 can be extended with as many instances as you want when the load increases, presuming the server can be distributed across multiple servers. – Jim Morris Apr 14 '11 at 19:25
  • 1
    I added the sizeof for two reasons, one it helps with DOS, but mainly because when you recieve a JSON string with TCP it can com ein in fragments, and it is hard to tell when you have the entire packet, plus two or more JSON strings can be concatenated. By having the length prepended you know when the entire string is read and can be processed, and if you get multiple ones back to back that is easily handled too, as you just pull off one JSON string at a time. It makes the TCP listener much easier as you don't dispatch it until you have read the entire thing. – Jim Morris Apr 14 '11 at 19:28
1

Why not try XMPP protocol first? XMPP is based on TCP/IP.

There are several OpenSource server solution, clients, and application libraries. XMPP already supports chat room like service. You can define extension easily.

Yi Zhao
  • 6,768
  • 1
  • 18
  • 18
  • I heard XMPP is great for this type of thing... but I would still have no idea how to install it. Do you have any links for XMPP demos? (how to install on a server, etc) – Rob Apr 14 '11 at 06:11
  • You can refer to http://code.google.com/p/objectivexmpp/ for the Objective-C XMPP library. I believe there are some other alternatives. For XMPP server, I think eJabber http://www.ejabberd.im/ is a good choice. Apache Vysper http://mina.apache.org/vysper/ is worth to consider. – Yi Zhao Apr 14 '11 at 06:53
  • Would I be able to host XMPP on an Azure server? Also, I'm going to have to add a lot of custom features into the chats, how well does XMPP handle this (as opposed to just doing it from scratch)? – Rob Apr 14 '11 at 07:01
  • With my understanding Azure is a Microsoft VM which running windows server. So you need to select a XMPP server for Windows. Simply using Apache Vysper http://mina.apache.org/vysper . XMPP is very extensiable, the message is a XML indeed, your client could be do any thing based on it. – Yi Zhao Apr 14 '11 at 07:07
  • Ok just making sure I got this straight: Step 1 - Sign up for Azure (I'm using them because I know C# really well and like the flexibility), Step 2 - Install Apache Vysper onto my Azure service, then Step 3 - Create extensions to change details about the chat rooms and user profiles. Is that correct? – Rob Apr 14 '11 at 07:10
  • I am not familiar with Azure. I don't think it is a necessary step unless you deploy on Azure. Apache Vysper is a Java based application, I am not sure it is working on Azure. Chat room and user profiles are built in by XMPP server. – Yi Zhao Apr 14 '11 at 07:57
  • I'd a quick check with Azure. I am not sure the Java/JRE could be installed on Azure :( – Yi Zhao Apr 14 '11 at 08:11