Can RestKit receive Asynchronus responses? - objective-c

I am using RestKit to request data and perform actions using this data.
Once I have parsed the data and displayed it to the user, they can select an item to initiate playback. When the user has selected the playback the server will respond with a response code, but the server also notifies the app when the playback has finished.
e.g.
client server
request data ->
<-respond with JSON
request_playback ->
<-playback_started OK
<-playback_stopped OK
Hope that the diagram helps out.
I need the App to be aware of changes on the server, in this example playback started/stopped or recording started/stopped.
So basically Is there a way for RestKit to accept a response that is not initiated by a request?
Thanks for your help.
Greg.

You should look into things like 'long polling' or 'reverse AJAX' or 'web sockets'. These are all techniques to be able to leave a connection open to the server and continuously receive responses from the server.
Maybe an alternative cleaner solution is to use Apple's push notification mechanism.

Related

How to handle the application if connection breaks in between a web service call

In several interviews I have been asked about handling of connection, web service calls, server responses and all. Even now I am not clear about many things.Could you please help me to get a better idea about the following scenarios?
What is the advantage of using NSURLSessionDataTask instead of NSURLConnection-I have an idea like data loss will not happen even if the connection breaks for NSURLSessionDataTask but not for the latter.But how it works?
If the connection breaks after sending the request to a server or while connecting to server , How can we handle the code at our end in case of NSURLConnection and NSURLSessionDataTask?-My idea is to use Reachability classes and check when it becomes online.
The data we are sending got updated at the server side. But we don't get the response from server. What can we do at our side to handle this situation?- Incrementing timeOutInterval is the only thing that we can do?
Please help me with these scenarios. Thank you very much in advance!!
That's multiple questions, really, but I'll try to answer them all briefly.
Most failure handling is the same between NSURLConnection and NSURLSession. The main advantages of the latter are support for background downloads and cancelling groups of related requests.
That said, if you're doing a large download that you think might fail, NSURLSession does provide download tasks that let you resume the download if your network connection fails, similar to what NSURLDownload used to do on OS X (never available on iOS). This only helps for downloading large files, though, not for large uploads (which require significant server-side support to resume) or other requests.
Your intuition is correct. When a connection fails, create a reachability object monitoring that particular hostname to see when it would be a good time to try the request again. Then, try the request again.
You might also display some sort of advisory UI to say that you have no Internet connection. (By advisory, I mean something that the user doesn't have to click on and that does not impact offline use of the app any more than necessary; look at the Facebook app for a great example.)
Provide a unique identifier when you make the request, and store that on the server along with the server's response until the client acknowledges receipt of the response (or purge it anyway after some reasonable number of days). When the upload finishes, the server gives you back its response if it can.
If something goes wrong, the client asks the server to resend the response associated with that unique identifier. Once your client has the data, it acknowledges receipt and the server deletes the response. If you ask the server for the response and it doesn't have one, then the upload didn't really complete.
With some additional work, this approach can make it possible to support long-running uploads more reliably. If an upload fails, ask the server how much data it got for that identifier, then tell the server that you're going to upload new data starting at the next byte. On the server side, overwrite the old data starting at that byte (just in case some data was still being written when you asked for the length).
Hope that helps.

Process SMS when notified via websocket

So I managed to connect to the websocket with my API token and I do get notifications. For incoming calls, I do get a push with all info like so:
{"type":"push","targets":["stream"],"push":{"type":"mirror","source_device_iden":"XXXXXXX","source_user_iden":"ujC7S24sQxw","client_version":206,"dismissible":true,"title":"5555551212","body":"Incoming call","application_name":"Pushbullet","package_name":"com.pushbullet.android","notification_id":"6","icon"
"Big value here"}}
So I can see that call came from 555-1212 (I changed number for privacy) and it all makes sense. However, for SMSs, all I get is a notification that SMS changed. No body field so I can't see where it came from and what the message is. All it says is sms_changed for type:
{"type":"push","targets":["stream"],"push":{"type":"sms_changed","source_device_iden":"XXXXXXXXX"}}
What am I doing wrong? I would like to get the SMS message and sender info so that I can publish it. Any and all help will be greatly appreciated.
This is not publicly documented yet and we might be changing the implementation in the near future so I'm hesitant to make it public. Also I don't know the specifics of the current implementation.
You can view how it works right now by using www.pushbullet.com and looking at the network traffic (in chrome inspector) when you do SMS stuff on the website.

Sending Push notification from client - Worklight

I know that push notifications are being sent from a backend server. Is it possible to send notification from client itself? My application goes like this: It acquires its position using Location services API. once it enters a specific circle, the trigger calls a callback function. What i want to do is to use the callback function to push a notification to the device. Is there any way to do this?
Thank you very much in advance!
If you just want a notification when the app is active in the background, you can use local notifications: https://github.com/katzer/cordova-plugin-local-notifications. The plug-in Javascript has to be modified somewhat to work with a Worklight app, but with some simple modifications it works great, and allows your app running in the background to raise a notification without going through the server side round trip involved when using push.
That said, I implemented an app that did exactly what you are looking for (in my case, I needed some server side processing to figure out what the text of the push message should be) The geo-fence callback called an adapter, providing it with event details and the device ID. The adapter determined what message to send, and used unicast push to send it back to the device.
You can invoke a procedure in the client side. I tried to invoke in the client side with httpAdapter and its working.
If the notification doesn't need to come from the server, you can also create a service that will run in the background and show a dialogbox once it enters the geofence.

trigger a function when changes in the DB are being commited

I was always wondering if it's possible to create a block of code (probably php code) that will execute when a certain change is being committed to the database.
For instance, chat application. When a user sends a message, it will add a message to a table, then I would like to force all of the other users to an AJAX request to read this new value (rather than sending AJAX request every 100ms to check if there is a new message)
I remember something that involved node.js and some other type of DB rather than mysql. If this is the only solution, can it work along with a normal mysql database?
Thanks in advance!
Yes, MySQL supports triggers, but they are pretty much limited to do other data operations. So you'd still have to get some notification sent to your javascript client
A better way of doing client notifications with with websocket or comet, allowing the server to push notifications from a message-queue.
You didn't give much detail about your programming environment, so I'll leave it to you to follow the tag links I gave above, and research the appropriate tools and frameworks for using these general methods.
Re your comment:
For PHP, here's an example "push" chat application:
http://www.aljtmedia.com/blog/websockets-for-php-ratchet-push-chat-application/
Here's an primer on using message queues in general:
http://blog.thecodepath.com/2013/01/06/asynchronous-processing-in-web-applications-part-2-developers-need-to-understand-message-queues/
And here are tutorials for RabbitMQ (one simple option among many MQ solutions usable by PHP), including PHP examples: https://www.rabbitmq.com/getstarted.html

Persistent Connection to Web Server (Like AJAX on Web)

I am wanting to create a program that talks with a Cometd server to allow for pushing of data to the app.
I have done this on the web side using AJAX, but I am a little unsure of the best way to do this with Cocoa.
I can make a standard connection using NSURLRequest and NSURLConnection, but how do I keep this connection alive so I can send data when needed and get the pushed info when needed.
Am I even going about this the correct way?
Thanks in advance
In terms of push notifications, if the http server does not close the close the connection the the NSURLConnection will stay open, and you will keep getting data. Note that if you are designing something like that you must use the asynchronous NSURLConnection methods, as a synchronous connection will not end until the server closes the connection.
As for sending more data, it is really not designed to do that. If you want to push more data in a single http request after you have sent it (which seems like a pretty bad idea to me) you are going to have to roll your http stack of find some opensource component you can use.
Note that NSURLConnection will use keep alive and other things as it deems appropriate, so if you start multiple logical connections to the same host in your app they may end up on the wire using the same keep alive connection, etc.