Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using XMPP to send realtime-ish information to running iOS app

I'm developing an iOS app. There are different 'areas' to the app, and users can post 'things' to them. Other users watching the same area should have the posted thing sent to their phone (but only if the app is running).

I've started work on a Django backend with a RESTful API. I plan on hosting the backend on Amazon Web Services, and each time something is added to an 'area', placing the time of update and 'area' into a queue, using Amazon Simple Queue Service. I'm going to make a Python app that processes items in the queue, querying the database to see which users have new 'things' they can download - currently this part is planned to then send an Apple Push Notification to the phone, and if the app is running it can make a simple RESTful request to get the new data in JSON format.

However I believe XMPP would be a better solution (using XMPPFramework on the iOS side), and would make it possible to support Android in the future.


I've done a lot of research into how to use XMPP for this purpose, but the documentation suggests that you would be using the public XMPP network, that users would have to set up an XMPP account, and is unclear how to send non-chat information.

This project requires the use of XMPP purely to send information from the server to a users device. Thus it should be a private XMPP network, if necessary users can be registered for an XMPP account on this private network (but behind the scenes, they shouldn't know what technology is powering the app.)


Is it possible to use XMPP just to send information to a mobile app? A lot answers/tutorials on the internet basically just suggest using an XMPP server and client - with no mention of how you can hook up the two on a private network to send data as determined by something other than the XMPP server.

Thank you for your help in advance :)

like image 506
Jon Cox Avatar asked Dec 20 '11 15:12

Jon Cox


1 Answers

Yes, it is possible. More than that it is completely possible to send and recieve anything you want through XMPP.

Generally XMPP is used for jabber chat accounts. This means a few things.

  1. Each user needs to be set up as a user on the server. This is so that the server knows which clients to push messages to. Its not difficult just to set up a random user/pass and link it directly to the app.

  2. The sending of chat information just means that there is a sender and a reciever. In your case you can probably disregard the sender, but it might be useful if you have multiple places where the messages could come from.

  3. A message can contain any information you want. I can't remember completely (its been a while since I've used XMPP) but I think that you can either send XML within the actual message itself (or json or whatever) or you can ATTACH extra XML to the message and just leave the message itself blank (or with a nominal value).

What you need to get started is an XMPP server. You can install this on your web server (I assume you can probably install it on AWS, but I'm not sure). This is the server I always use, Openfire, its very simple to set up and use (and has a nice web interface which you can use to set everything up). This lets you send a recieve messages, and makes the whole thing "private". This means that, although you could potentially connect to public jabber networks, you can also restrict access to the server.

Next you want a web-based api. There is a good php framework, xmpphp which will help you set up an api. This api will allow you to send messages from your python script (e.g. post req to curl) to the xmpp server which will deliver it to the iphone. If you don't want to send messages back then you're all set. Listening for XMPP messages using php is a whole other kettle of fish!

Another thing to keep in mind is that you will need to send statuses from the app so that the server knows if you are logged in and "listening" or not. Again, this is all reasonably well documented on the internet if you look for it (I can't quite remember the exact ins and outs). So in your python/php script you can test to see if the user you are trying to send a message to is currently "logged on" and "listening" as it were. If they're not you will need to implement the push notifications thing and send a push notification. If you're unsure about this there is a fantastic service called urban airship which I will reccommend. They send upto 1,000,000 push notifications a month for free (and subsequent notifications are pretty reasonable). Otherwise be prepared for a world of hurt setting up the service yourself! (I've heard!)

I hope that's answered most of your questions. Its a pretty specialist subject (lots of people won't know about XMPP - its one of those things you'll only know about if you need to know about it). XMPP is very powerful though, and a much better solution than repeating http requests and all sorts. There's quite a lot of information on the internet if you can find it, but you might have to be a bit clever about how to interpret the information, its one of those things that hasn't quite moved over to iPhone in the mainstream yet (in my opinion).

EDIT... one more thing

Beware of drop outs - internet drop outs and such. It sounds obvious, but it always bites me! Always forget that sometimes the internet cuts off and my XMPP connections need to be reconnected. Esp if people are putting the app in the background and then coming back to it later. :) Remember to reconnect when you need to.

like image 144
Thomas Clayson Avatar answered Sep 21 '22 12:09

Thomas Clayson