Magic with Parse in 10 lines of code

 At a recent Tech Lancaster talk we gave, I was surprised to see that nobody in attendance had heard of Parse. I found myself ranting about my love for them, specifically the amount of code it saved me. The following are simplified examples of my most used features, all coming in roughly 10 lines or less.

majormega-loves-parse-1.gif

More often than not, our projects need done yesterday. On top of that, they often involve campaigns that only last a few weeks at most. The more rapidly we can develop, the more time we can allocate to creative and polish that so often gets sacrificed with tight timelines.

Showcased below are some simplified examples of features I find myself using the most:


 1. New User from Facebook Login

Here we prompt for login via Facebook, then retrieve and store their basic information (name and email).

Parse.FacebookUtils.logIn("email", {
  success: function(user) {
    FB.api('/me', {fields: 'email,first_name,last_name'},
    function(response) {
        user.set('email', response.email);
        user.set('first_name', response.first_name);
        user.set('last_name', response.last_name);
        user.save();
    });
  }
});


 2. Upload File

Using the Parse node module, uploading files is a cinch. The following is what we used on our analog looping piano project to upload the final song:

var mp3 = fs.readFileSync(filepath);
mp3 = Array.prototype.slice.call(new Buffer(mp3), 0);
var parseFile = new Parse.File("song.mp3", mp3);

parseFile.save().then(function() {
    var pianoAudio = new Parse.Object("PianoAudio");
    pianoAudio.set("audio", parseFile);
    pianoAudio.save();
});


 3. Retrieving List of Files

Again using our piano app as an example, getting the list of uploaded songs is just as easy:

var Songs = Parse.Object.extend("PianoAudio");
var query = new Parse.Query(Songs);

query.limit(500);
query.descending('createdAt');
query.find({
  success: function(songs) {
    // display song list
    pianoApp.setupSongs(songs);
  }
}); 


 4. High Score List

When making games, a high score list is mandatory to help drive social interaction. The important thing to note here is that the retrieving scores logic runs in their “cloud code” environment on their server as we’re querying user data, which isn’t allowed client side.

// Setting score
if (score > user.get('highScore')) {
    user.set('highScore', score);
    user.save();
}

// Retrieving Scores (Cloudcode)
var query = new Parse.Query(Parse.User);

query.descending("highScore");
query.limit(5);
query.find({
  success: function(users) {
    // update high score list
  });
});     

Please note for a high traffic game that will be up for long period of time, I recommend obfuscating the high score saving process like one of the many solutions on this stack overflow thread.


 5. Sending a Text Message

Using Parse’s built in Twilio module, you can easily send a text message. Here we send a message automagically when a new song is uploaded from the piano:

 Parse.Cloud.afterSave("PianoAudio", function(request) {

      // Initialize Twilio module
      var client = require('twilio')('ACCOUNT_SID', 'AUTH_TOKEN');

      // Send Text Message
      client.sendSms({
           to:'+5555555555', 
           from: '+5555555555', 
           body: 'A new song was uploaded!' 
      });

 });


 Check out the docs

The easiest way to get started with Parse is to check out their great documentation. With a REST API and SDKs for all major platforms, even some less popular things like Unity, Arduino, etc, it’s incredibly easy to utilize Parse in your next project. I hope you find it as useful as we have!


 
12
Kudos
 
12
Kudos

Now read this

Building an analog looping piano

This is the story of our new product, “Quaver: The Multiplayer Piano”. We turned an old piano into a song writing machine using magnetic pickups and a Raspberry Pi. For a less technical and more visual story, view the photo gallery on... Continue →

Subscribe to MajorMega Blog

Don’t worry; we hate spam with a passion.
You can unsubscribe with one click.

6VKrivsvyyd7oZnUUC4g