Skip to content

Instantly share code, notes, and snippets.

@davimacedo
Created November 25, 2016 22:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save davimacedo/ac7e8666f6fb612df953a99607d565ac to your computer and use it in GitHub Desktop.
Save davimacedo/ac7e8666f6fb612df953a99607d565ac to your computer and use it in GitHub Desktop.
Live Query Example
// In order to use it:
// Create an app in back4app, create a class called Message with a column called text, activate web hosting + live query for this class
// Create a new folder and go to it
// Place curent code in a file called livequery.js
// run: npm install parse
// run: node ./livequery
// Then you can create, update and delete entries in dashboard and see the results in your console
var Parse = require('parse/node');
// Replace here your app id and your client key
Parse.initialize("Fsk0eOuypmOyQr7jhyl77QhzmtEdjMbJWFNTBZSz", "LQOhZDS5xHLa12kAutp7nIklXfisqdAhqpXHm2JQ");
// Replace here your https://customdomain.back4app.io
Parse.serverURL = 'https://livequery.back4app.io';
var Message = Parse.Object.extend('Message');
var query = new Parse.Query(Message);
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(); // Just to exemplify the unsubscribe method, this code will unsubscrive and not receive notification anymore when an entry is deleted
});
console.log('Running');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment