Real time application using Live Queries
Introduction
Live queries are meant to be used in real-time reactive applications, where just using the traditional query paradigm would come with some problems, like increased response time and high network and server usage. Live queries should be used in cases where you need to continuous update a page with fresh data coming from the database, which often happens in, but is not limited to, online games, messaging clients and shared to do lists.
This section explains how to use Back4App’s Live Query in an Android environment through Back4App.
This tutorial uses a basic app created in Android Studio Arctic Fox 2020.3.1 Patch 1 with compileSdk 30
, minSdk 21
and targetSdk 30
At any time, you can access the complete Project via our GitHub repositories.
See more about Parse SDK at Android SDK API Reference and Parse open source documentation for Android SDK.
Goal
Here is a preview of what we are gonna achive :
Prerequisites
To complete this tutorial, we need:
- Android Studio.
- An app created on Back4App.
- Note: Follow the New Parse App tutorial to learn how to create a Parse App on Back4App.
- An android app connected to Back4App.
- Note: Follow the Install Parse SDK tutorial to create an Android Studio Project connected to Back4App.
- A device (or virtual device) running Android 4.1 (Jelly Bean) or newer.
Step 1 - Enable Live Query
Before you start coding, it’s necessary to have a class in your database to enable Live Query. To do that, simply find your app at Back4App website, and click on Dashboard
> Create a class
, as shown here:

Note: Here, this class will be called
Message
.
Now, to enable Live Query feature, log in to your account at Back4App website, find your app and click on Server Settings
, then find the “Server URL and Live Query” block and click on SETTINGS
.

Then, you will arrive at a page like the one below. At this page you will need to check the Activate your Back4App subdomain
option, the Activate Live Query
option and all the classes you want Live Query to be activated, as shown below:

It’s necessary to activate WebHosting to use Live Queries, because your domain will work as the live server.
- Note: To know more about WebHosting look at Back4App WebHosting Tutorial
Step 2 - Set up the LiveQuery client
Parse Server’s Official GitHub have an implementation of the Live Query Client for Android. It is necessary to implement the official Live Query client, which works nicely. To do so, add the following lines to your app app/build.gradle
file, in the dependencies section and sync your project:
1
2
3
4
5
6
7
dependencies {
...
// Don't forget to change the line below with the latest version of Parse SDK for Android
implementation "com.github.parse-community.Parse-SDK-Android:parse:1.26.0"
implementation 'com.github.parse-community:ParseLiveQuery-Android:1.2.2'
...
}
In this project, we will also create a class named Message
, which will contains our messages.
Step 3 - Subscribe to your query
To start using Live Queries, first create a LiveQueryClient
that will manage the WebSocket connections for you. To do this, you will have to provide the Application ID, it’s JavaScript Key and also a server URL of Live Query which you did the setup in the first step.
1
2
3
4
5
Parse.initialize(new Parse.Configuration.Builder(this)
.applicationId(getString(R.string.back4app_app_id))
.clientKey(getString(R.string.back4app_client_key))
.server(getString(R.string.back4app_server_url))
.build());
1
2
3
4
5
Parse.initialize(Parse.Configuration.Builder(this)
.applicationId(getString(R.string.back4app_app_id))
.clientKey(getString(R.string.back4app_client_key))
.server(getString(R.string.back4app_server_url))
.build())
The code for initializing LiveQueryClient
is the following:
1
ParseLiveQueryClient parseLiveQueryClient = ParseLiveQueryClient.Factory.getClient();
1
val parseLiveQueryClient = ParseLiveQueryClient.Factory.getClient()
We have a RecyclerView adapter named MessageAdapter. messageAdapter.*
functions are trigerring when object added,deleted or updated. Here the our messageAdapter
functions.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
public void addItem(ParseObject t) {
this.list.add(t);
notifyItemInserted(list.size() - 1);
}
public void removeItem(ParseObject object) {
for (int i = 0; i < list.size(); i++) {
if (list.get(i).getObjectId().equals(object.getObjectId())){
list.remove(i);
notifyItemRemoved(i);
notifyItemRangeChanged(i, list.size());
return;
}
}
}
public void updateItem(ParseObject object) {
for (int i = 0; i < list.size(); i++) {
if (list.get(i).getObjectId().equals(object.getObjectId())){
list.set(i,object);
notifyDataSetChanged();
return;
}
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
fun addItem(t: ParseObject?) {
list!!.add(t!!)
notifyDataSetChanged()
}
fun removeItem(`object`: ParseObject) {
for (i in list!!.indices) {
if (list!![i].objectId == `object`.objectId) {
list!!.removeAt(i)
notifyItemRemoved(i)
notifyItemRangeChanged(i, list!!.size)
return
}
}
}
fun updateItem(`object`: ParseObject) {
for (i in list!!.indices) {
if (list!![i].objectId == `object`.objectId) {
list!![i] = `object`
notifyDataSetChanged()
return
}
}
}
Then, you should create a ParseQuery
for what type of object you want to subscribe. A subscription is an event emitter, which will fire events when changes happen to an object that satisfies your query.
In this example, you will make a basic query and will subscribe all changes done to the Message
objects.
See more about queries and subscriptions at Parse Official Queries Documentation.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
ParseQuery<ParseObject> parseQuery = new ParseQuery<>("Message");
subscriptionHandling = parseLiveQueryClient.subscribe(parseQuery);
subscriptionHandling.handleSubscribe(q -> {
subscriptionHandling.handleEvent(SubscriptionHandling.Event.CREATE, (query, object) -> {
MainActivity.this.runOnUiThread(() -> {
messagesAdapter.addItem(object);
});
});
subscriptionHandling.handleEvent(SubscriptionHandling.Event.DELETE, (query, object) -> {
MainActivity.this.runOnUiThread(() -> {
messagesAdapter.removeItem(object);
});
});
subscriptionHandling.handleEvent(SubscriptionHandling.Event.UPDATE, (query, object) -> {
MainActivity.this.runOnUiThread(() -> {
messagesAdapter.updateItem(object);
});
});
});
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
val parseQuery = ParseQuery<ParseObject>("Message")
subscriptionHandling = parseLiveQueryClient!!.subscribe(parseQuery)
subscriptionHandling!!.handleSubscribe { subscriptionHandling!!.handleEvent(SubscriptionHandling.Event.CREATE
) { _: ParseQuery<ParseObject?>?, `object`: ParseObject? ->
runOnUiThread { messagesAdapter!!.addItem(`object`) }
}
subscriptionHandling!!.handleEvent(SubscriptionHandling.Event.DELETE
) { _: ParseQuery<ParseObject?>?, `object`: ParseObject? ->
runOnUiThread { messagesAdapter!!.removeItem(`object`!!) }
}
subscriptionHandling!!.handleEvent(SubscriptionHandling.Event.UPDATE
) { _: ParseQuery<ParseObject?>?, `object`: ParseObject? ->
runOnUiThread { messagesAdapter!!.updateItem(`object`!!) }
}
}
- Note: We are triggered all this events in the app. Also you can trigger this create,update and delete events from the Back4App (From table or javascript console).
It’s done!
At this point, you have the knowledge in how to use Live Queries to make real-time reactive applications in an Android environment and also how to setup Live Query using Back4App. Now you can start by implementing it in your own app.
You are now ready to explore Parse Server core features and Back4App add-ons.