Android
Real Time
Live Queries
11 min
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 compilesdk 30 , minsdk 21 minsdk 21 and targetsdk 30 targetsdk 30 at any time, you can access the complete project via our github repositories kotlin example repository java example repository 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 tutoria l to create an android studio project connected to back4app a device (or virtual device ) running android 4 1 (jelly bean) or newer 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 dashboard > create a class 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 server settings , then find the “server url and live query” block and click on settings 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 activate your back4app subdomain option, the activate live query 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 2 set up the livequery client parse server’s official github have an implementation of the live query client for android https //github com/parse community/parselivequery 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 app/build gradle file, in the dependencies section and sync your project app/build gradle 1 dependencies { 2 3 // don't forget to change the line below with the latest version of parse sdk for android 4 implementation "com github parse community parse sdk android\ parse 1 26 0" 5 implementation 'com github parse community\ parselivequery android 1 2 2' 6 7 } in this project, we will also create a class named message message , which will contains our messages 3 subscribe to your query to start using live queries, first create a livequeryclient 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 parse initialize(new parse configuration builder(this) 2 applicationid(getstring(r string back4app app id)) 3 clientkey(getstring(r string back4app client key)) 4 server(getstring(r string back4app server url)) 5 build());1 parse initialize(parse configuration builder(this) 2 applicationid(getstring(r string back4app app id)) 3 clientkey(getstring(r string back4app client key)) 4 server(getstring(r string back4app server url)) 5 build()) the code for initializing livequeryclient livequeryclient is the following 1 parselivequeryclient parselivequeryclient = parselivequeryclient factory getclient();1 val parselivequeryclient = parselivequeryclient factory getclient() we have a recyclerview adapter named messageadapter messageadapter messageadapter functions are trigerring when object added,deleted or updated here the our messageadapter messageadapter functions 1 public void additem(parseobject t) { 2 this list add(t); 3 notifyiteminserted(list size() 1); 4 } 5 6 public void removeitem(parseobject object) { 7 for (int i = 0; i < list size(); i++) { 8 if (list get(i) getobjectid() equals(object getobjectid())){ 9 list remove(i); 10 notifyitemremoved(i); 11 notifyitemrangechanged(i, list size()); 12 return; 13 } 14 } 15 } 16 public void updateitem(parseobject object) { 17 for (int i = 0; i < list size(); i++) { 18 if (list get(i) getobjectid() equals(object getobjectid())){ 19 list set(i,object); 20 notifydatasetchanged(); 21 return; 22 } 23 } 24 }1 fun additem(t parseobject?) { 2 list!! add(t!!) 3 notifydatasetchanged() 4 } 5 6 fun removeitem(`object` parseobject) { 7 for (i in list!! indices) { 8 if (list!!\[i] objectid == `object` objectid) { 9 list!! removeat(i) 10 notifyitemremoved(i) 11 notifyitemrangechanged(i, list!! size) 12 return 13 } 14 } 15 } 16 17 fun updateitem(`object` parseobject) { 18 for (i in list!! indices) { 19 if (list!!\[i] objectid == `object` objectid) { 20 list!!\[i] = `object` 21 notifydatasetchanged() 22 return 23 } 24 } 25 } then, you should create a parsequery 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 message objects see more about queries and subscriptions at parse official queries documentation 1 parsequery\<parseobject> parsequery = new parsequery<>("message"); 2 subscriptionhandling = parselivequeryclient subscribe(parsequery); 3 subscriptionhandling handlesubscribe(q > { 4 subscriptionhandling handleevent(subscriptionhandling event create, (query, object) > { 5 mainactivity this runonuithread(() > { 6 messagesadapter additem(object); 7 }); 8 }); 9 subscriptionhandling handleevent(subscriptionhandling event delete, (query, object) > { 10 mainactivity this runonuithread(() > { 11 messagesadapter removeitem(object); 12 }); 13 }); 14 subscriptionhandling handleevent(subscriptionhandling event update, (query, object) > { 15 mainactivity this runonuithread(() > { 16 messagesadapter updateitem(object); 17 }); 18 }); 19 });1 val parsequery = parsequery\<parseobject>("message") 2 subscriptionhandling = parselivequeryclient!! subscribe(parsequery) 3 subscriptionhandling!! handlesubscribe { subscriptionhandling!! handleevent(subscriptionhandling event create 4 ) { parsequery\<parseobject?>?, `object` parseobject? > 5 runonuithread { messagesadapter!! additem(`object`) } 6 } 7 subscriptionhandling!! handleevent(subscriptionhandling event delete 8 ) { parsequery\<parseobject?>?, `object` parseobject? > 9 runonuithread { messagesadapter!! removeitem(`object`!!) } 10 } 11 subscriptionhandling!! handleevent(subscriptionhandling event update 12 ) { parsequery\<parseobject?>?, `object` parseobject? > 13 runonuithread { messagesadapter!! updateitem(`object`!!) } 14 } 15 } 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 https //www back4app com/product/parse server and back4app add ons https //www back4app com/product/addons