Live Query

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:


Web hosting and live query


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


Activate Hosting


Add the subdomain name you wish to have:

Subdomain name

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

Activate Live Query

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

Step 2 – Setting up the LiveQuery client on Android

Parse Server’s official GitHub does not have yet an implementation of the LiveQuery Client. So, it is necessary to use the community implementation, which works nicely. To do so, add the following line to your build.gradle file, in the dependencies section:

dependencies {
    compile 'com.github.tgio:parse-livequery:1.0.3'

Also, it is imperative that you give the App permission to use INTERNET resources. Hence, in your AndroidManifest.xml, add the following line:

<manifest xlmns:android...>
 <uses-permission android:name="android.permission.INTERNET" />
 <application ...

And you should import the LiveQueryClient from tgio package. Add the following imports:

import tgio.parselivequery.BaseQuery;
import tgio.parselivequery.LiveQueryClient;
import tgio.parselivequery.LiveQueryEvent;
import tgio.parselivequery.Subscription;
import tgio.parselivequery.interfaces.OnListener;

And you are set to use the LiveQuery community implementation.

Step 3 – Subscribing and listening to events

It is necessary to initialize the API, which can be done as follows:

// Back4App's Parse setup
Parse.initialize(new Parse.Configuration.Builder(this)

LiveQueryClient.init("wss://", "appIdHere", true);

In order to receive the events, you have to subscribe and filter the messages that are incoming:

// Subscription
final Subscription sub = new BaseQuery.Builder("Class_With_Live_Query")
        .where("some_filter", "some_value")

After that, you have to use the sub to register the actions that will be executed once the events are fired:

sub.on(LiveQueryEvent.CREATE, new OnListener() {
    public void on(JSONObject object) {
        // Action to be executed here when an object that matches
        // The filter you set up

Note that the event that is watched is the CREATE one. There is also the UPDATE and DELETE, as well as other events, however these three will suffice for most use cases of the API. Just change the event accordingly and it should work.

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:

ParseObject poke = new ParseObject("Message");
poke.put("content", "poke");
poke.put("destination", "pokelist");
poke.saveInBackground(new SaveCallback() {
        public void done(ParseException e) {
            Snackbar.make(view, "Poke has been sent!", Snackbar.LENGTH_LONG)
                    .setAction("Action", null).show();

The full source code to the project is in the link below: