Parse Live Query comes to Back4App

live_query

The idea of ensuring that app development proceeds as quickly and efficiently as possible lies at the heart of back4app’s philosophy. Therefore, the recent addition of the Live Query feature to back4app makes app development on this platform an even more attractive option. Access to real-time information on database changes is particularly important to developers. Through activation of Parse Live Query, they can now receive these real-time notifications of relevant database changes.

Parse.Query Usefulness and Limitations

Parse.Query is a central Parse programming feature that lets the programmer setup conditions to retrieve particular Parse objects. It is widely used for creating dashboards, games of strategy and various others types of app, but it has its limitations. One of the major challenges programmers face is the need to development apps with real-time support. A team of developers cannot work together editing a single file simultaneously and easily keep track of all the updates with Parse.Query.

The Advantages of Parse LiveQuery

Parse LiveQuery was developed as a solution to Parse.Query’s limitations. This new tool allows a developer to subscribe to real-time notifications of particular Parse.Query creations and updates. For example, they can set a condition to have the server notify them if changes are made to a specific class. In this way, the developer learns instantly when database changes are made, regardless of their source. They also benefit from resource savings because Parse LiveQuery obviates the need to keep polling the database to find out if anything has changed.

How can I use Parse LiveQuery on Back4App?

 It is very simple. First of all, you need to activate Parse LiveQuery for those classes you want to use real-time. Secondly, you download and install the SDK that supports the SDK pointing to your new API endpoint. Now you can use Parse LiveQuery on your code.

1-Setup Parse LiveQuery on your web-hosting server

The Back4App servers need to know that you are sending a live query request — enable your web-hosting service to send these requests to a different endpoint.
On your app, select View App -> Web Host + LiveQuery

 

webhosting-menu

Select Activate LiveQuery and then select which classes you want to activate the LiveQuery.

web-hosting-live-query

2-Update your SDK

Download and update your App with the latest SDK version that supports LiveQuery. Now your Live query is ready to be used.

3-Use Live Query

To illustrate how to use Live Query I’ve created a Javascript(NodeJS) App and a simple class(Message) on Back4App. Using Parse Dashboard, I created/updated/deleted rows in this class and saw on my App in real-time the messages I’ve placed on ‘text’ field. The main differences between a regular and a live query are the subscribe and unsubscribe methods.

 

var Parse = require('parse/node');

Parse.initialize("Fsk0eOuypmOyQr7jhyl77QhzmtEdjMbJWFNTBZSz", "LQOhZDS5xHLa12kAutp7nIklXfisqdAhqpXHm2JQ");
Parse.serverURL = 'https://livequery.back4app.io';
Parse.liveQueryServerURL = 'ws://livequery.back4app.io';
var Message = Parse.Object.extend('Message');
var query = new Parse.Query(Message);
query.equalTo('username', process.argv[2]);
var subscription = query.subscribe();

subscription.on('create', function (message) {
 console.log('Message created with text: ' + message.get('text'));
});

subscription.on('update', function (message) {
 console.log('Message updated with text: ' + message.get('text'));
});

subscription.on('delete', function (message) {
 console.log('Message deleted with text: ' + message.get('text'));
 subscription.unsubscribe();
});

console.log('Running for user ' + process.argv[2]);

Leave a reply

Your email address will not be published.