Live Queries
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.
Here is a preview of what we are gonna achive :
To complete this tutorial, we need:
- An app created on Back4App.
- An android app connected to Back4App.
- Note: Follow the Install Parse SDK tutorial to create an Android Studio Project connected to Back4App.
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:
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.
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:
In this project, we will also create a class named Message, which will contains our messages.
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.
The code for initializingLiveQueryClientis the following:
We have a RecyclerView adapter named MessageAdapter. messageAdapter.* functions are trigerring when object added,deleted or updated. Here the our messageAdapter functions.
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.
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)
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.