Skip to content
This repository has been archived by the owner on Jun 13, 2023. It is now read-only.

Subscription callbacks not firing (Obj-C) #42

Closed
vespakoen opened this issue Jun 8, 2016 · 2 comments
Closed

Subscription callbacks not firing (Obj-C) #42

vespakoen opened this issue Jun 8, 2016 · 2 comments

Comments

@vespakoen
Copy link

vespakoen commented Jun 8, 2016

Related

#39
#36
#28
#36
#41
#7

I have been pulling my hair out for hours to debug this (dippin toes in Swift)

Here are my recollections of my memory of this trip down the rabbit hole for others:

1) Could not build module Parse

screen shot 2016-06-08 at 05 29 33

Downloading Parse and ParseLiveQuery was ok, but whenever I build the project I get the error as in the title.

It seems to go away (for 1 build, or not at all) when doing a (super) clean build, it seems I can ignore it, because the app builds fine.

2) Invalid Sec-WebSocket-Accept response

Now I had a connection problem, the error I got was

Error: Error Domain=SRWebSocketErrorDomain Code=2133 "Invalid Sec-WebSocket-Accept response" UserInfo={NSLocalizedDescription=Invalid Sec-WebSocket-Accept response}

After a lot of messing with my nginx configuration I figured out it had to do with my proxy setup.

There are a couple of issues about it over at SocketRocket 1 2 3

I forked ParseLiveQuery and updated the SocketRocket dependency, which made the above error go away, but gave me another one.

Updating Parse and ParseLiveQuery to master seemed to have resolved this.

3) No errors ?

Sometimes errors only showed up when stepping through the code, this might have to do with the Obj-C >< Swift interop?

After a while, I got the error to show that I was missing a where clause on the query

4) Similarity to Parse-JS

  • The where clause on a query is not obligatory in Parse-JS

screen shot 2016-06-08 at 14 42 41

- The `sessionToken` is always sent (https://github.com/ParsePlatform/ParseLiveQuery-iOS-OSX/blob/3daca2f89a3869d026b1005dda57852012321836/Sources/ParseLiveQuery/Internal/Operation.swift#L21) from Parse-JS, this is only sent when logged in, this shouldn't matter though, just putting it out there. ### 5) Moar logging!

I started adding log statements to the ParseLiveQuery library, and saw that it was getting a connection, and sent subscriptions to the websocket connection (please add a flag for verbose logging!)

Because of the logs I noticed that I was querying an object that wasn't setup for live query (e.g. wasn't added to the liveQuery config for the parse server)

So I changed that and finally got responses back from the server

6) Callbacks don't fire

This is the real issue, I see the data is coming in when logging raw websocket data (or checking the Network traffic)
But the registered callback is not firing:

[Parse initializeWithConfiguration:[ParseClientConfiguration configurationWithBlock:^(id<ParseMutableClientConfiguration> configuration) {
    configuration.applicationId = @"*****";
    configuration.clientKey = @"*****";
    configuration.server = @"https://devparse.*****";
    configuration.localDatastoreEnabled = NO;
    NSLog(@"Connecting to: %@", configuration.server);
}]];

self.client = [[PFLiveQueryClient alloc] initWithServer:@"https://devparse.*****" applicationId:@"*****" clientKey:@"*****"];
PFQuery *query = [PFQuery queryWithClassName:@"Messages"]; // <-- make sure you register the object in the parse-server config
[query whereKey:@"objectId" notEqualTo:@"whut"]; // <-- make sure you add a where clause

Test 1:

self.subscription = [self.client subscribeToQuery:query];
[self.subscription addCreateHandler:^(PFQuery * query, PFObject * message) {
    NSLog(@"LQ!!");
}];

Test 2:

self.subscription = [self.client subscribeToQuery:query];
[self.subscription addCreateHandler:^(PFQuery * _Nonnull query, PFObject * message) {
    NSLog(@"LQ!!");
}];

At this moment, my versions of things are:

  • Parse, ParseLiveQuery, CocoaPods -> master branch
  • XCode -> 7.3.1

7) Possible cause?

944065d

8) Working work around

I had seen enough off the code after this debug session to come up with the following, which seems to work:

@import Foundation;
@import Parse;
@import ParseLiveQuery;

#import "AppDelegate.h"

@interface UCMessageHandler : NSObject <PFLiveQuerySubscriptionHandling>
@end

@implementation UCMessageHandler
- (void)liveQuery:(PFQuery *)query didRecieveEvent:(PFLiveQueryEvent *)event inClient:(PFLiveQueryClient *) client {
    NSLog(@"CREATED EVENT TYPE: %d", PFLiveQueryEventTypeCreated);
    NSLog(@"UPDATED EVENT TYPE: %d", PFLiveQueryEventTypeUpdated);
    NSLog(@"REMOVED EVENT TYPE: %d", PFLiveQueryEventTypeDeleted);
    NSLog(@"EVENT: %@", event);
    NSLog(@"EVENT TYPE: %d", event.type);
    NSLog(@"EVENT OBJECT: %@", event.object);
//    NSLog(@"LQ!!!!!!!!!!!!!!!!!");
}
@end

@interface AppDelegate ()
@property (nonatomic, strong) PFLiveQueryClient *client;
@property (nonatomic, strong) PFQuery *query;
@property (nonatomic, strong) UCMessageHandler *handler;
@property (nonatomic, strong) PFLiveQuerySubscription *subscription;
@end

@implementation AppDelegate

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    // Override point for customization after application launch.
    [Parse initializeWithConfiguration:[ParseClientConfiguration configurationWithBlock:^(id<ParseMutableClientConfiguration> configuration) {
        configuration.applicationId = @"****";
        configuration.clientKey = @"****";
        configuration.server = @"https://devparse.******.***";
        configuration.localDatastoreEnabled = NO;
        NSLog(@"Connecting to: %@", configuration.server);
    }]];

    // Setup livequery client
    self.client = [[PFLiveQueryClient alloc] initWithServer:@"https://devparse.******.***" applicationId:@"****" clientKey:@"****"];

    PFQuery *query = [PFQuery queryWithClassName:@"Messages"]; // <-- make sure to get a type that is enable in parse server liveQuery config
    [query whereKey:@"objectId" notEqualTo:@"whut"]; // <-- make sure to add a where clause
    self.handler = [[UCMessageHandler alloc] init]; // <-- make sure to create a handler yourself by using the PFLiveQuerySubscriptionHandling protocol
    self.subscription = [self.client subscribeToQuery:query withHandler:self.handler]; // <-- make sure to use the custom object that implements the PFLiveQuerySubscriptionHandling protocol

    return YES;
}
@end

Looking forward to the responses!

@richardjrossiii care to take a look at this?

@vespakoen
Copy link
Author

More specifically, this is the last place where the event is still around before it is "sent into a blackhole":

https://github.com/ParsePlatform/ParseLiveQuery-iOS-OSX/blob/6febfb59f75d5877226054bf3a3daa78abc168bc/Sources/ParseLiveQuery/ObjCCompat.swift#L263

screen shot 2016-06-10 at 14 55 57

I cannot "step in" because I guess the handler does not want to respond to "didReceiveEvent" or something, or maybe the Event cannot be converted? in any case, something is hairy there

@jpgupta
Copy link

jpgupta commented Feb 17, 2018

Thought I'd add to this - @richardjrossiii's solution works for me, BUT

Events don't seem to fire if you're doing CRUD requests via the Data Browser. If you make them through a client, then the web socket events DO work

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants