Platform
Real-time database
12 min
how to use parse server live query introduction live queries are meant to be used in real time reactive applications , where just using the traditional query paradigm could cause several problems, like increased response time and high network and server usage live queries should be used in cases where you need to continuously 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 a javascript environment through back4app for this tutorial, as an example, you will build a todo online list using live queries, as shown below see the complete todo online list project at live query todo list project see more about parse sdk at javascript sdk api reference and parse open source documentation for javascript sdk prerequisites to complete this tutorial, you will need a basic javascript app connected with back4app or the live query todo list project note you can use the app created in our javascript install parse sdk tutorial or the live query todo list project sufficient knowledge of javascript parse queries (but not mandatory) 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 the back4app website , and click on dashboard dashboard > create a class create a class , as shown below note here, this class will be called message message now, to enable the live query feature, log in to your account at back4app website , find your app, and click on app settings app settings > server settings server settings look for the “server url and live query” block and click on settings settings the “server url and live query” block looks like this then, you will arrive at a page like the one below on 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 on which you want live query to be activated, as shown below it’s necessary to activate your subdomain to use live queries because it will work as the live server 2 subscribe to your query to start using livequeries, you first need to create a livequeryclient livequeryclient that will manage the websocket connections for you to do this, you will have to provide the application id, its javascript key for verifying purposes, and also a server url that should be the domain with which you did the setup in the previous step here’s the code for livequeryclient livequeryclient livequeryclient js 1 var client = new parse livequeryclient({ 2 applicationid 'your app id here', 3 serverurl 'wss\ //' + 'your domain here', // example 'wss\ //livequerytutorial back4app io' 4 javascriptkey 'your javascript key here' 5 }); 6 client open(); after following the above mentioned steps, you should create a query for the 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 todo todo object see more about queries at parse official queries documentation below is the code for querysubscribe querysubscribe querysubscribe js 1 var query = new parse query('todo'); 2 query ascending('createdat') limit(5); 3 var subscription = client subscribe(query); 3 listen to events with the setup ready, it’s necessary to code what your app will do when an event fires in this part, we are going to show how to listen to these events in a practical example the todo online list example will serve as a guideline for your project because there is little to no boilerplate code used the two main events that you are going to use here are the create create event and the delete delete event the complete list of events can be found here 3 1 the create event the createevent createevent is fired every time a parseobject parseobject is created and fulfills the query constraints you’ve inserted this event returns the created object in the todo online list example, the array of activities is stored in the this todos this todos variable and we will add the new objects of our database in this array, when a create event happens the code for createevent createevent is shown below createevent js 1 subscription on('create', todo => { 2 this todos add(todo); 3 console log('on create event'); 4 }); 3 2 the delete event whenever an existing parseobject parseobject that fulfills your query constraints is deleted from the database, you’ll get this event, which returns the deleted object in the todo online list example, you have to delete an object from the list every time it is deleted from the database look for matching ids between the server and the code, to identify the deleted objects the code for deleteevent deleteevent is the following deleteevent js 1 subscription on('delete', todo => { 2 this todos foreach(t => { 3 if (t id === todo id) { 4 console log('on delete event'); 5 this todos delete(t); 6 } 7 }); 8 }); it’s done! at this point, you know how to use live queries to make real time reactive applications you also know how to do the correct live query setup using back4app and can start by implementing it in your app