WebHook

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:

Figure 8.6: A screen for listing the existing WebHooks

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:

Figure 8.7: A dialog for defining a new WebHook; beforeSave for class Transfer

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:

Figure 8.8: An error message sent back from the WebHook will pop up on the bottom right

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"}
Figure 8.9: Info-level log screen displaying all kind of logs, including errors

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.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset