Configuring a Parse Server - codepath/ios_guides GitHub Wiki

1. Overview

Parse provides a cloud-based backend service to build data-driven mobile apps quickly. Facebook, which acquired the company in 2013, shut down service on January 28, 2017. An open source version was published to enable developers to continue using their apps, along with a migration guide.

While there are many alternate options to Parse, most of them lack either the functionality, documentation, or sample code to enable quick prototyping. For this reason, the open source Parse version is a good option to use with minimal deployment/configuration needed.

1.1 Differences with Open Source Parse

You can review this Wiki to understand the current development progress of this app. There are a few notable differences in the open source version:

  • Authentication: By default, only an application ID is needed to authenticate with open source Parse. The base configuration that comes with the one-click deploy options does not require authenticating with any other types of keys. Therefore, specifying client keys on Android or iOS is not needed.

  • Push notifications: Because of the implicit security issues with allowing push notifications to be sent through Android or iOS directly to other devices, this feature is disabled. Normally in Parse.com you can toggle an option to override this security restriction. For open source Parse, you must implement pre-defined code written in JavaScript that can be called by the clients to execute, otherwise known as Parse Cloud Code.

  • Single app aware: Each Parse Server instance serves a single app. A proposal to make the core server multi-app aware was declined by the maintainers, so if you intend to run many different apps with different datastores, you need to run a separate Parse Server instance for each.

  • File upload limitations: Parse Server supports MongoDB or PostgreSQL as its database; with MongoDB, the default storage layer relies on Mongo's GridFS layer. The default upload limit (the maxUploadSize option) is 20 MB, but if you depend on storing large files, you should really configure the server to use Amazon's Simple Storage Service (S3).

Many of the options need to be configured by tweaking your own configuration. You may wish to fork the code that helps instantiate a Parse server and change them based on your own needs.

2. Setting a new Parse Server

We will be using back4app.com to host our Parse dashboard. Below are the steps for deploying your Parse server on back4app.

If you are interested in hosting your Parse dashboard in another platform (i.e. AWS, Azure, Google's GCP, etc.), check out this guide. NOTE: Most other hosts require a credit card to get started with a Parse dashboard.

Sign up to back4app

Back4app is a platform that helps minimize the workload of setting up the backend. Basically, they set up all the backend for you. Due to the nature of our course, we will mainly focus on how to connect an iOS app to your own backend server rather than teach the nitty gritties of creating a backend from scratch.

  1. Sign Up for Back4app
  2. Create a new Parse App

3. Browsing Parse Data

What's also good about Back4app.com is that it provides the dashboard for you so you can view the Parse data. Here is a screenshot of where your Parse data is stored:

4. Add Parse client to an Xcode project

The Parse Apple SDK is installed with Swift Package Manager, which is built into Xcode. Avoid pod 'Parse': the last release published to CocoaPods is 1.19.4 (October 2022), so a CocoaPods install gives you an outdated SDK.

  1. In Xcode, choose File > Add Package Dependencies... and enter the package URL:

    https://github.com/parse-community/Parse-SDK-iOS-OSX
    
  2. Choose a version-based dependency rule (Apple's recommended package requirement), then add the ParseObjC library product to your app target. Also add ParseLiveQuery if you plan to use Live Queries.

  3. Import the SDK at the top of any file that uses it: import ParseCore in Swift, or @import ParseCore; in Objective-C. (SDK releases before 2.0.0 used import Parse instead.)

4.1 Configure Parse SDK in Xcode Project

  1. From the Back4App dashboard , get your config variables of your parse server (App ID, Client Key) located on Core settings:

  • Copy down your App Id and your ClientKey. They should look something like this:

  1. 👆 Using the App Id and ClientKey variables, connect your app by adding the parse configuration in your AppDelegate.swift file under the function didFinishLaunchingWithOptions like this:

    // AppDelegate.swift
    
    // Don't forget to add the Parse SDK package first!
    import ParseCore
    
    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
    	// Override point for customization after application launch.
    
    	// --- Copy this only
    	
    	let parseConfig = ParseClientConfiguration {
    			$0.applicationId = "YOUR_APP_ID" // <- UPDATE 
    			$0.clientKey = "YOUR_CLIENT_KEY" // <- UPDATE
    			$0.server = "https://parseapi.back4app.com"
    	}
    	Parse.initialize(with: parseConfig)
    	
    	// --- end copy
    
    
    	return true
    }
    
    
    #import "AppDelegate.h"
    @import ParseCore;
    
    @interface AppDelegate ()
    
    @end
    
    @implementation AppDelegate
    
    
    - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(  NSDictionary *)launchOptions {
    
    	ParseClientConfiguration *config = [ParseClientConfiguration  configurationWithBlock:^(id<ParseMutableClientConfiguration> configuration) {
    
    		configuration.applicationId = @"YOUR_APP_ID"; // <- UPDATE 
    		configuration.clientKey = @"YOUR_CLIENT_KEY"; // <- UPDATE
    		configuration.server = @"https://parseapi.back4app.com";
    	}];
    
    	[Parse initializeWithConfiguration:config];
    
    	return YES;
    }
    
    @end
    

🎊 Congrats! You have successfully finished setting up your own parse server and connected it to your iOS App! The rest of the tutorials are optional.

5. (Optional) Adding Support for Live Queries

One of the newer features of Parse is that you can monitor for live changes made to objects in your database (i.e. creation, updates, and deletes). To get started, make sure you have defined the ParseObjects that you want in your NodeJS server. Make sure to define a list of all the objects by declaring it in the liveQuery and classNames listing:

let api = new ParseServer({
  ...,
  // Make sure to define liveQuery AND classNames
  liveQuery: {
    // define your ParseObject names here
    classNames: ['Post', 'Comment']
  }
});

See this guide and this spec for more details. Parse Live Queries rely on the websocket protocol, which creates a bidirectional channel between the client and server and periodically exchanges ping/pong frames to validate the connection is still alive.

Websocket URLs are usually prefixed with the ws:// or wss:// (secure) scheme. On Back4App, enable Live Query from the dashboard instead: go to My Apps > Server Settings, open the settings of the Server URL and Live Query block, turn on Live Query, and select the classes that should support it (see Back4App's Live Query guide). If you self-host Parse Server on a different host (for example, Amazon), you may need to make sure that TCP port 80 or TCP port 443 are available.

6. (Optional) Troubleshooting

  • If you self-host Parse Server instead of using Back4App and the server fails to start, check its configuration: Parse Server throws an error at startup when its options omit appId, masterKey, or serverURL. In the parse-server-example project these come from the APP_ID, MASTER_KEY, and SERVER_URL environment variables, and the master key falls back to an empty string, so set MASTER_KEY explicitly in your host's environment settings.

  • Check out the Help section to troubleshoot your Back4App parse server

    Back4App Help

7. (Optional) Enabling Push Notifications

Check out Back4App's push notifications guide to enable notifications on your App!

Some things to keep in mind when enabling push notifications:

  • On Back4App, you export your push certificates as .p12 files and upload them on your app's iOS Push notification page. Upload both the development certificate and the production certificate: you need the development one for testing and the production one for release.
  • The next three tips apply only if you self-host Parse Server, where push is configured in the push option of the server configuration through the Parse Server push adapter:
    • Make sure your bundle ID matches the topic value in the adapter's ios options. If you get Invalid Token responses, it means that you may have a mismatch issue.
    • If you are using a development certificate, make sure it is marked as production: false in the adapter's ios options.
    • The adapter's default APNs implementation uses @parse/node-apn, a fork of node-apn. Verify you can connect to Apple's APNs service by following node-apn's certificate instructions.
  • Enable network logging on your iOS client by reviewing this Parse guide.