Was anybody able to make REST Streaming working on iOS?


#1

I’m trying to get cameras updates via REST Streaming.
Here is my connection Objectvive-C code:

    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:self.eventURL
                                                           cachePolicy:NSURLRequestUseProtocolCachePolicy
                                                       timeoutInterval:self.timeoutInterval];

    NSDictionary *headers = @{ @"accept": @"text/event-stream",
                               @"authorization": @"Bearer <my token>",
                               @"content-type": @"application/json",
                               @"cache-control": @"no-cache"};
    NSDictionary *parameters = @{};
    NSData *postData = [NSJSONSerialization dataWithJSONObject:parameters options:0 error:nil];
    
    [request setAllHTTPHeaderFields:headers];
    [request setHTTPBody:postData];
    [request setHTTPMethod:@"GET"];

    
    NSURLSession *session = [NSURLSession sessionWithConfiguration:[NSURLSessionConfiguration defaultSessionConfiguration]
                                                          delegate:self
                                                     delegateQueue:[NSOperationQueue currentQueue]];
    
    self.eventSourceTask = [session dataTaskWithRequest:request];
    [self.eventSourceTask resume];

It starts connecting and never returns - no errors, no 200 code, just does nothing.
The same for the request from Postman - just “loading”

Will appreciate any advice or suggestion.


#2

Hi Choock -

I wasn’t able to get a simple out-of-box solution working when I updated the Nest API iOS sample app, so left it out of there. I think part of the difficulty is with Apple’s implementation of HTTP libraries (for example, 401 redirects have to be handled differently because they strip the headers after the initial call for security reasons).

Anyway, my suggestion is to try one of the Event Source / SSE implementations people have put on GitHub, one might work for you.


#3

Hi :slight_smile:

Thank you for your reply.
On the EventSource from GitHub – I tried them all. No one does handle redirection. Finally, I just took the URL came with redirection request, put it in my original request and now it works.

So, the solution would be (just like you suggested in the documentation) to break the auto-redirection, catch the URL, cache it and use it further.


#4

Great! Glad you found a way to make it work.