Best practice: Send continuous Data (like GPS Position) from iOS to another client - iphone

I'm looking for a best-practice for the following scenario:
A Mobile Device (iOS) sends its (Sensor-)Data to another device (iPad, Laptop, PC, whatever) over the Internet in realtime.
Sure, I need a web service but as far as my understanding is, a webservice gets "called" and returns Data or takes Data. But I require a constant exchange of sensor data between two devices in nearly realtime. I'm looking to implement something like a Multiplayer Online Game, how do they do such a thing? Or the Glympse service?

"Just because it's called GameKit doesn't mean its uses are limited to games. Any data you want to pump through that connection is fair game"
How true. The name GameKit is such a terrible misnomer, which very unfortunately diminishes it's significance, there are so much in this kit that you can do to develop serious apps.

The way I'd approach it is to create a "server" out there on the net, that would run some application (like a CGI script) to collect the data (store it, in maybe a database) - then allow the remote device to query it.
This means that every device has a "well-known service" which they are connecting to - potentially "logging into" - and selecting the data from.
For example, each "client" could push their "vessel name" and GPS location. The CGI script on the server would just put these in a MySQL database - as simple table containing "Vessel name" and "Location".
Alternativiley, clients could query and "pull" GPS locations for a specific vessel name. As simple CGI script that would take "Vesel Name" as a value, and send a MySQL query to the database to return the "Location". It could send the location (and vessel name) back in XML format.
The iPhone client could user NSXMLParser - or even a JavaScript "AJAX" client could use it's own inherent XML parsing capabilites to send a request for one (or more) vessels, and receive the results.
What you don't want to do is to have each client have to speak directly to each other client. This will get you into trouble with firewall rules, and mess up when you try to scale many-to-many communications.

If your devices are both running iOS, you can use the peer-to-peer connectivity functions in GameKit for this. There's a pretty good question and answer about that here.
Just because it's called GameKit doesn't mean its uses are limited to games. Any data you want to pump through that connection is fair game (so to speak).

You may use XMPP protocol to send/receive any xml data in almost realtime.
You will need to create chat room and every XMPP client (your app) will just need to login to that room. Message that is sent to the room will be delivered to every XMPP client.
Info on protocol and public servers can be found here: http://xmpp.org/
Good book on XMPP: http://download.cnet.com/XMPP-The-Definitive-Guide/3000-20412_4-75114351.html
Link to free iOS XMPP library can be found here: iOS messenger SDK

Related

Sending data directly between phones that are not co-located

I have an idea for an iPhone app that will make billions of dollars for me, and help me take over the world. I am currently designing the architecture of the application. I had originally assumed that I would need to create and host a database that would allow users to upload their data and share it with other, targeted, users. But I thought it would be better if the users could send their data directly to each other without having it stored in a database. However, I haven't been able to find anything that would suggest this is possible.
Is it possible to send data between iPhones that are not in close proximity (bluetooth) and not on a LAN? For instance...User #1, who is in Tennessee, creates some information and hits "Send to others" and it sends that information directly to User #2, who is in Arizona, and User #3, who is in Maine. Could the text messaging functionality be hacked to do that?
(I realize that the phones belonging to Users #2 and #3 would have to be on, and running the app that would receive the data)
I am new to mobile development, and I am still getting used to the functionality of mobile devices so, I am certain this is a dumb question. But, it is worth asking because, if it can be done, this would dramatically change the architecture (and maintenance costs) of this application.
Thanks, in advance, for whatever advice/pointers you can give.
This is something handled by using the publish - subscribe pattern. You may want to look into a plug 'n play service such as pubnub. That will let you do just that. Phones that should be receiving each other's messages will have listeners set up on the same pubnub channel, and will receive a notification when any of the other phones publish to that channel. Something like pubnub has a pretty simple API/SDK that you can use to get a prototype up and running pretty quickly (and for free, at least at first).
User #1's app sends the message to your server. You server does an APNS push to the phones of users #2, and #3. They don't even need to be running the app.
No need to mark this correct or up-vote it, but I will take a high-paying job in the new world order.
Amazon has an application service called Simple Queue Service (Amazon SQS) which Allows you to create queue's with messages in them tat app's can subscribe to.
To quote their page:
Amazon Simple Queue Service (Amazon SQS) offers a reliable, highly
scalable, hosted queue for storing messages as they travel between
computers. By using Amazon SQS, developers can simply move data
between distributed components of their applications that perform
different tasks, without losing messages or requiring each component
to be always available. Amazon SQS makes it easy to build an automated
workflow, working in close conjunction with the Amazon Elastic Compute
Cloud (Amazon EC2) and the other AWS infrastructure web services.
Amazon SQS works by exposing Amazon’s web-scale messaging
infrastructure as a web service. Any computer on the Internet can add
or read messages without any installed software or special firewall
configurations. Components of applications using Amazon SQS can run
independently, and do not need to be on the same network, developed
with the same technologies, or running at the same time.
They have an iOS API to integrate it into your app, and it is free for up to 100,000 messages per month and then $0.01 per 10,000 messages after that.
More info here:
http://aws.amazon.com/sqs/
Is it possible to send data between iPhones that are not in close
proximity (bluetooth) and not on a LAN?
The two devices obviously need some kind of connectivity.
For instance...User #1, who is in Tennessee, creates some information
and hits "Send to others" and it sends that information directly to
User #2, who is in Arizona, and User #3, who is in Maine. Could the
text messaging functionality be hacked to do that?
The problems here are:
helping each device discover the correct address for the others, and...
ensuring that the devices can reach each other.
Mobile devices are constantly moving around on the network and changing their IP addresses as they go. As you're driving to work, your device may have Internet access through it's 3G connection, except for those times when you're driving through a tunnel and don't have any connectivity at all. Once you enter your building, a wifi connection becomes available, so the device switches to that. But your company's network has a firewall that blocks incoming connections, and it's impossible to know in advance which IP address you'll get from the DHCP server at any given time anyway. The same is true for the folks in Arizona and Maine and wherever else.
A server, on the other hand, is usually located at an address that's both easy to discover (thanks to the Domain Name System), easy to reach, and almost always available. For those reasons, it's common to have mobile devices communicate with each other by going through an intermediate server.

Set up server for simple iPhone game

I am looking for information on how to create and run a simple server game that will take turn information from one user, do some number crunching and pass the resultant information on to the other players in the game.
What I am looking for is a push in the right direction. While I have been coding for the iPhone for almost two years now, my server experience is limited to making a URL request to a PHP script that pulls information from a database and return it to the iPhone requesting the information. I have no idea how to take information from an iPhone and send it to other specific iPhone or players.
Any help you can give is greatly appreciated. Tutorials would be nice but even just a hint of how to go about doing this would be fantastic.
Thanks.
If you have more experience on the Objective-C side, I recommend you use iOS 4.1's Game Center connectivity to connect your users. Once you've established a GKSession between your users, sending data to all the connected peers is a snap. You could do your number crunching on one device that acts as a server and send the result to the clients for processing. This is how I do the peer-to-peer gaming in my iOS game, Cee-lo. Check out the Game Kit Programming Guide.
You should look at creating a RESTful web service on PHP that accepts your players data. This service can post the data to your database. When other players request the data from your PHP page, it can just request the latest from the database, which should have the latest friends information.
This a fairly common pattern, you can lookup using JSON to send and receive between the iPhone and a PHP site.
Creating a rest api with PHP

iphone app communication without using webservices

I want to send some Text plus a image from one iphone application to other iphone app but restriction is I should not use a web server in between communication,Is there any way to fulfill it ?
Details: There are two independent devices and could be far enough say out of network. My requirement one app adds some text with a image and sends it to another iphone which can be at any long distance , and the app installed in another iphone will read that info and image into itself.
Actually there is a solution that meets your needs — and that fits to bbums answer:
Create a HTTP-Server on the iPhone, using cocoahttpserver. than you will ask some webservice like whatismyip.com for your public ip. with this your iPhone can be connected worldwide.
But very likely ur wifi-network is not forwarding your port to the iPhone. Ash.
And even if: Now it gets difficult. How to publish your ip from one phone to the other? hmmm... — I got it: I will exchange the information in a centralized space! In the web!
... wait — that would be a Webserver.
You see: Without any kind of server in the Web the users would need to exchange ip manually and have full admin power and knowledge about the local network.
So IMHO bbums answer is the only way to go.
PS: I am working with http server running on iPhones. In local network that works great, especially with bonjour. And you can use them over distance network — but only with reconfiguration of your router — something you shouldn't force your user to do
There is far from enough information to provide a specific answer.
two apps on two different devices?
are the two devices on the same network?
are the two devices both on WiFi?
do you need the user to receive a notification or something if the app isn't running?
If on same device, you can define a custom URL handler in the destination app and then openURL: in the source app to pass the data over. Encode your image and text into the URL, but be careful of size limitations.
If on different devices, there are many possible solutions, but answering the above questions will be critical to actually knowing what solution is appropriate.
Given your comment -- two apps, different devices, arbitrary networks -- then you are going to have to have some kind of server in between. Note that the recently added Game Center does have the ability to rendezvous two users, but it has a very particular user experience that may not be appropriate to your needs.
I would suggest that you investigate using push notifications to notify the receiving user of the availability of content. As for moving the content between, no direct connection is possible and you will have to have some kind of store-and-forward server in between. And, yes, a web server is going to be the easiest possible solution simply because HTTP is ubiquitous these days.
If there's no network of any kind available, but both parties have amateur radio licenses, then hooking the two devices up to HF packet radios might work.
THIS is super EASY.
I would code up some software that can turn data into modem signal, like the good old dial up modem. The device would actually make those annoying buzzing sounds.
You get the phone number for your friends nearest landline and call him.
He places his iPhone near the phones receiver in listen mode and you connect to his phone using your audible modem.
Bingo, via the power of sounds you have sent data which is decoded on his device and all for the very cheap price of a phone call, there are pretty cheap these days especially if you use Skype.
Easy Way (relatively speaking)
A way two apps on different networks can communicate without setting up a web server of some sort is as follows.
Use an existing third party storage system like DropBox.
Each app would need the login and password for your DropBox. Then both apps can read and write files that the other app can see.
An existing app that does this is a shopping list app called ShopShop.
The app on my phone and my wife's phone both link to the same DropBox account and the app keeps the shopping list synced up when one of us adds something to the list.

How does iphone apps interact with server?

I am a new programmer who is new to iPhone development and server stuff. I have a lot of questions to ask.
You don't have to answer all the questions; any help is appreciated!
How does iPhone apps interact with server?
Is there a particular kind of server i should use to interact iphone app with server?
If there is no particular kind of server then what kind of server can be used?
What are their advantages and disadvantages?
What should the iPhone app (which is the client) do in order to interact with the server?
How does the server know which iPhone to send data to?
What should the server do in order to interact with iPhone app (client)?
Your best bet is to have your iPhone make web requests of a web server. Your iPhone app acts just like a web browser, making http requests to a web server, and parsing the response.
I'm building an app right now that hits PHP scripts I've written that do database work, etc, and return JSON objects. It's not fancy--I could have built a whole SOAP or RPC web service, but I didn't do that, it just makes GET requests with query-string arguments.
There are handy libraries you want to know about. Google "iPhone JSON" to find the JSON library written by Stig Brautaset, that's the one most people seem to be using. Also, rather than putting yourself through all the hoops that the iPhone's built-in web client framework requires, go get ASIHTTPRequest, a very powerful and MUCH simplified web client library.
As a general rule, you want to do as much processing on the server as possible. For instance, there's a place in my app I'm searching for events happening within a user-specified range of their local coordinates ("within 10 miles of me"). I wrote PHP to build a latitude/longitude bounding box, and query from the database based on that. That's WAY faster than bringing a bunch of events down and then asking Core Location to calculate their distance from where I'm standing.
You've asked quite a few questions so I'll try my best to answer them all:
First, you need to be a bit clearer, what type of server are you talking about? Email server, web server, lolcat server, it depends.
At the basic level, the iphone communicates over the internet. The internet uses Internet Protocol, and there are two standard protocols built atop of IP: Transmission Control Protocol, and User Datagram Protocol. Each has it's own uses and functions.
TCP/IP and UDP/IP make up the backbone of internet communication.
A more specific application protocol is built atop of these two internet protocols, with a specific format to a given application. For example, HTTP is the standard protocol for transferring HTML and other Web information between a web server to a web browser client, over TCP.
So, your iPhone would use whatever protocol is required to commuincate with the server. For more common server communication, the iOS SDK provides methods to construct messages (for example if you wish to make an HTTP request to a web server, you can use initWithContentsOfURL to send a GET request).
If you built a custom server, then you will need construct the required message protocol on the iphone, and send it to the server, using either TCP or UDP (whatever your custom server expects).

Iphone Game Dev Over The Internet

I was wondering is there an easy way to communicate between iphones over the Internet(Not LAN/Bluetooth) or must there be a dedicated server in which all the iphones running an application needs to connect to?
For instance, suppose I'm writing a game which works on the Internet. Once four clients joins a room, game starts. must I implement a server in which every Iphone client connects to (for instance if server was developed on Windows it could be a Service) or is there another way to address this when developing Internet-based application?
Thank you
We have an online game that uses the iPhone (see www.ownthisworld.com if you want an idea of what we have done). Basically we use a php back end that accepts requests and returns xml data to the phone. It works quite well, but it depends on how much data you would be sending and your expected response times. In any event, our architecture of PHP/MySql backend works fine for our needs. By using the normal internet route, you do not have to worry about firewalls so much either.
You'd at the very least need some sort of central match making service so that the iPhones would be able to find each other, which would require some sort of dedicated server.
Secondly, devices on the internet can't always simply do a direct connection with each other. If all of the devices are behind a NAT or a firewall that doesn't allow incoming connections, you'd need a central dedicated server to host the game on. If at least one of the iPhones can accept incoming direct connections, you could have that iPhone host the game for the others.
There are some ways to punch through a NAT, but they're generally pretty horrifying in their complexity, and you'd still need the central match making service to pair up players.