Reputation: 5681
I created a couple web services for my app as follows. 1. Allows a host device to send its information to the web service and will store it in a sql database. 2. Allows a client device to see all nearby devices based on the information in the sql database.
I want the client to now be able to click one of the devices to connect to, which would then somehow link the two together. The question I have is right now my web services can only send a response to a device's request. What I want is if the host device maybe presses a button, it will send that info to the web service, which will then send some information to the client who previously connected.
How can I accomplish sending information from a web service to a device that hasn't requested anything?
Background info: I am using a php based web service and ASIHTTPRequest to send and receive the information on the iphone side.
Upvotes: 0
Views: 172
Reputation: 16124
In 99% of cases, you're going to want to use polling. Think of any application that gets messages (email, Words with Friends, whatever). They all call the server and ask "are there any new messages for me?" You'll have to figure out the right frequency for your application.
In the rare case where you need near realtime communication, then you'll need to setup your app to run as a server. See here: What classes do I use to make an iPhone act as a server?. Obviously the battery and bandwidth cost of this is much higher.
Upvotes: 1
Reputation: 8808
You could probably keep a persistent connection open, but this is going to break when network conditions change (such as when moving from network to network, on/off cellular, etc.).
The client could poll the server at rapid intervals, but this will not only incur a higher than desirable load on your server but, much more importantly, burn through the client device's battery. (Doubly so on 3G since the cellular radio has a fairly lengthy minimum power-up interval regardless of how small a network transaction might be.)
If you are talking about fairly sporadic updates (real-time updates really would call for the first option above, with appropriate error handling and reconnection if a TCP session is killed), you might consider Apple Push Notifications - provided that the notification payload is consistent with the APNS guidelines. They would behave almost exactly as you describe.
Upvotes: 0