-1

I know almost nothing about server/sockets programming. Pardon my ignorance.

I'm making an iOS app that needs to integrate with my web server. The function is analogous to a chat room - multiple clients will be 'connected' and 'listening' to a server session, any one client can send a 'bit', and all clients will receive the 'bit'.

Should I use low-level socket listening and callbacks for this? Is there a better, more power-efficient way? A cool framework I should use?

rmaddy
  • 314,917
  • 42
  • 532
  • 579
emma ray
  • 13,336
  • 1
  • 24
  • 50

2 Answers2

1

There are many options for this:

  1. Use a socket to your server and roll your own protocol
  2. Use web sockets and long polling from your app. This means you will fire a HTTP request and your server will keep it open for, let's say, one minute waiting for messages. Take a look here to start: What are Long-Polling, Websockets, Server-Sent Events (SSE) and Comet?
  3. Use a chat server like Openfire or Ejabberd, which use XMPP, and on your client use XMPPFramework (https://github.com/robbiehanson/XMPPFramework)

Depending on the complexity of your app (authentication? blocking contacts? one-to-many and one-to-one chat?) you can decide on any of the above options. With more details I may be able to help you more.

Community
  • 1
  • 1
Matías R
  • 2,195
  • 1
  • 14
  • 12
  • Very helpful answer, thank you. I'm not actually implementing chat, but the paradigm is very similar. I basically want to send a single message at a system-level - I don't need any advanced blocking, auth, etc features. Push notifications might actually be the way to go... – emma ray Apr 14 '14 at 13:52
  • Indeed, now that you've clarified that, I agree that for broadcasting messages without active participation of the receivers, push notifications is the way to go. – Matías R Apr 14 '14 at 14:19
0

If you want to be able to receive data while the app is not in the foreground, you will need to use Apple's push notification feature, which is implemented in hardware and the only way to make network connections to a device that is in power saving mode.

There's plenty of documentation how it works, basically the device registers with your server (after asking the user for permission), you use the token it gives you to send a ping to Apple's server, which forwards the ping on to the device. The device can then contact your server and download the actual data you want to send to it.

If you're OK with the server only communicating with your app while the app is running, you have a few options. The easiest is "long polling" where the app sends a HTTP request to the server, using something like this:

NSURL *serverUrl = [NSURL URLWithString:@"http://example.com/"];
NSString *response = [NSString stringWithContentsOfURL:serverUrl usedEncoding:NULL error:NULL];
NSLog(@"%@", response);

Instead of the server responding instantly, it can wait around for a long time (for example 45 seconds) until it has something to send to the device, then it responds with the information.

If 45 seconds are reached without having anything to send, it just sends a nothing response and the phone immediately opens up a new URL request to the same URL. This keeps the server from having a bunch of old/abandoned connections open. Typically connections are dropped after 60 seconds, so you want to respond before that time limit is reached. Obviously you'll want to do the request on a background queue with NSOperationQueue.

There are other options, you could use sockets, you could start a web server on a custom HTTP port on the phone (eg: https://github.com/robbiehanson/CocoaHTTPServer). But long polling is often the easiest choice.

Most apps use a combination of push notifications and something else.

Abhi Beckert
  • 32,787
  • 12
  • 83
  • 110