Back4App

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:

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 the Back4App Website, and click on Dashboard > Create a class, as shown below:

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 “Web Hosting and Live Query” block and click on SETTINGS.
The “Web Hosting and Live Query” block looks like this:

Then, you will arrive at a page like the one below. At this page you will need to check the Activate Hosting option, the 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 WebHosting to use Live Queries, because your domain will work as the live server.

Step 2 - Subscribe to your Query

To start using LiveQueries, you first need to create a LiveQueryClient that will manage the WebSocket connections for you. To do this, you will have to provide the application ID, its JavaScript Key, its Master 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.js

1
2
3
4
5
6
7
var client = new Parse.LiveQueryClient({
    applicationId: 'Your app Id here',
    serverURL: 'wss://' + 'Your domain here', // Example: 'wss://livequerytutorial.back4app.io'
    javascriptKey: 'Your JavaScript key here',
    masterKey: 'Your master key here'
});
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 object.

See more about queries at Parse Official Queries Documentation.

Below is the code for querySubscribe:

querySubscribe.js

1
2
3
var query = new Parse.Query('Todo');
query.ascending('createdAt').limit(5);
var subscription = client.subscribe(query);

Step 3 - Listen to events

With the setup ready, it’s necessary to code what your app will actually 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 to 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 event and the delete event. The complete list of events can be found here.

Step 3.1 - The Create Event

The createEvent is fired every time a 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 variable and we will add the new objects of our database in this array, when a create event happens.

The code for createEvent is shown below:

createEvent.js

1
2
3
4
subscription.on('create', todo => {
    this.todos.add(todo);
    console.log('On create event');
});

Step 3.2 - The Delete Event

Whenever an existing 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 one is deleted from the database. Look for matching IDs between the server and the code, to identify the deleted objects.

The code for deleteEvent is the following:

deleteEvent.js

1
2
3
4
5
6
7
8
subscription.on('delete', todo => {
    this.todos.forEach(t => {
        if (t.id === todo.id) {
            console.log('On delete event');
            this.todos.delete(t);
        }
    });
});

It’s done!

At this point, you have the knowledge of 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 own app.