Android
Real Time

Live Queries

11min

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.

Goal

Here is a preview of what we are gonna achive :

Document image


Prerequisites

To complete this tutorial, we need:

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:

Document image


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.

Document image


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:

Document image


It’s necessary to activate WebHosting to use Live Queries, because your domain will work as the live server.

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:

app/build.gradle


In this project, we will also create a class named Message, which will contains our messages.

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.

Java
Kotlin


The code for initializingLiveQueryClientis the following:

Java
Kotlin


We have a RecyclerView adapter named MessageAdapter. messageAdapter.* functions are trigerring when object added,deleted or updated. Here the our messageAdapter functions.

Java
Kotlin


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.

Java
Kotlin


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.