The Parse platform provides an extensible mechanism to allow us to process business logic externally. This is where functions come in. This mechanism is called WebHook.
We may have functions running as external processes, outside the Parse platform, and use them in conjunction with Parse's WebHook to perform complex business logic. As in the previous example, we already have the Transfer class. We then define a WebHook for this class to call an external function every time, before each of the Transfer entities is saved. We specify a URL to a FaaS gateway for this WebHook. The HTTP POST method will be made to the specified URL with the JSON as its request body. The request body contains the data of the current Transfer entity.
A WebHook in Parse can be created by going to Core | Webhooks and clicking on the small Create a webhook option:
There are two kinds of WebHook in Parse: Cloud Code functions and triggers. The kind we are using in this chapter is the trigger. A WebHook trigger can hook into many places, such as beforeSave, afterSave, beforeDelete, and afterDelete.
In the example we are discussing in this chapter, the beforeSave trigger will be used as shown in the following screenshot. We choose Transfer as the target class of this hook. Then we need to specify the Webhook URL, a bank routing function deployed to Fn:
We will talk about the bank routing WebHook in the next section, but let's see it in action briefly before we explore its details.
We test the WebHook by sending data by creating a new instance of the Transfer class. In the following screenshot, mobile number +661234567 is not registered yet. So it cannot be looked up, and the WebHook returns an error message:
To see error logs, click on Core | Logs, as shown in the following screenshot. Here is an example of the Webhook returning the following JSON:
{"error": "Could not lookup number: +661234567"}
The specifications of the returning messages are {"success": object} to update data back to the Parse platform, and {"error":"msg"} to display the error message.