Live Query with Swift

After this tutorial, you’ll be able to build a user system like this one:


Before going into the action, make sure you have a working project. You can check out the links below to the Quickstart guide:


In this project, we will create a class named ‘Message’, which will advertise the poke action. This class contains two columns, named each content and destination. After that, we will configure the server side of the app.

Step 1 – Enable Live Query

To enable the Live Query, just go to your Back4app App page and find this option:


Click to see more options and tick a checkbox with label “Activate Hosting”:


Add the subdomain name you wish to have:


Then Activate Live Query and select the classes you want to enable the Live Query:


You are all set with the server! Now, to the code.

Step 2 – Setting up the LiveQuery client on iOS

Parse Server’s github offers a nice implementation of Live Query functionality that can be included in your project in an easy way. We’ll need to use CocoaPods for that. First of all, you’ll need to setup CocoaPods. Google has a nice video on that:

After you have a working CocoaPods setup, you’ll need to add this line to your target on the Podfile:

pod 'ParseLiveQuery'

Don’t forget that swift requires the ‘use_frameworks!’ option to work properly with CocoaPods. After you have done that, run ‘pod install’ and wait for it to complete. Open the newly created workspace and go to your ViewController.swift file. At that file, you should import the Live Query client and right below it add an instance to the Client:

import ParseLiveQuery

let liveQueryClient: Client = ParseLiveQuery.Client(server: "wss://Your API")

Now, it is necessary to make your app subscribe to the channel and listen to the events.

Step 3 – Subscribing and listening to events

First of all, you need a variable that holds the subscription memory, which MUST NOT be at any inner scope of any function. Hence, it is nice for it to be a variable of the class it is inserted. In this example, this variable named ‘subscription’ will be defined inside the ‘ViewController.swift’ file and will be declared as follows:

private var subscription: Subscription<PFObject>!

Also, in order to receive the events, you have to subscribe and filter the messages that are incoming. Add this to the ‘viewDidLoad’ function:

// This message query filters every incoming message that is
// On the class 'Message' and has a 'message' field
var msgQuery = PFQuery(className: "Message").whereKeyExists("destination")
subscription = liveQueryClient.subscribe(msgQuery).handle(Event.created) { _, message in
    // This is where we handle the event

Note that the PFQuery is the responsible to filter the messages incoming and can be changed as it is necessary. Check out the Parse Server API Reference for more filters:

Also, the only event that is tracked is the ‘created’ event. If it is necessary to track the deletion or the change of some row inside a class, you can use the ‘deleted’ and ‘updated’, to do so.

Now, we are able to send messages.

Step 4 – Sending messages

It is pretty straightforward to send messages. You only have to create a ‘PFObject’ object and save it in background, as in this example:

let message: PFObject = PFObject(className: "Message")

message["destination"] = "pokelist"
message["content"] = "poke"

message.saveInBackground { (_, er) in
    if !(er != nil) {
        // Everything went alright
    } else {
        // Something went wrong

And that’s it. Check the source code to the project below: