Search code examples
iosswiftparse-platformparse-cloud-codelivequery

Parse LiveQuery in Cloud Code


I have an iOS and android application, deployed in a Parse Server. Now, I want to use the LiveQuery SDK, from a cloud function!

Here's my cloud code:

Parse.Cloud.define("subscribeQueryFunction", function(request, response) {
    var query = new Parse.Query("Message");
    var subscription = query.subscribe();

    subscription.on("create", (object) => {
        response.success(object);
    });
});

And my Swift code:

    override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view, typically from a nib.

    PFCloud.callFunction(inBackground: "subscribeQueryFunction", withParameters: nil) { (res: Any?, e: Error?) in
        print(res)
        print(e)
    }
}

The thing is, that the client doesn't get a response! Although, I checked the logs and the subscription seems to be established:

2017-05-14T16:49:12.442Z - Ran cloud function subscribeQueryFunction for user D7FP3cts5p with: Input: {} Result: {"text":"hey","createdAt":"2017-05-14T16:49:12.412Z","updatedAt":"2017-05-14T16:49:12.412Z","objectId":"DXCiumOTal","__type":"Object","className":"Message"} 2017-05-14T16:49:11.101Z - Ran cloud function subscribeQueryFunction for user D7FP3cts5p with: Input: {} Result: {"text":"hey","createdAt":"2017-05-14T16:49:11.044Z","updatedAt":"2017-05-14T16:49:11.044Z","objectId":"qkXS6csCKJ","__type":"Object","className":"Message"} 2017-05-14T16:49:09.099Z - Ran cloud function subscribeQueryFunction for user D7FP3cts5p with: Input: {} Result: {"text":"hey","createdAt":"2017-05-14T16:49:09.080Z","updatedAt":"2017-05-14T16:49:09.080Z","objectId":"ydnrDHO9AP","__type":"Object","className":"Message"} 2017-05-14T16:47:45.183Z - Create new client: 0

Any ideas?


Solution

  • Live Query is not made to use on the server side. Live Query use web sockets, to interact in real time with your client. But when you app is close the socket is close too ... So want you need is push notifications

    If you want to make a real time messaging application with parse your need to:

    • Implement Parse Live Query to get new messages with the web sockets when you app is open
    • Use push notifications, so when you app is close, you get the new messages