What is a Codeless Event Handler¶
About Event Handlers¶
An event handler is a custom, server-side code which responds to an API event. For every API call Backendless generates two types of events - "before" and "after". The before event is fired before the default logic of the API implementation is executed and the "after" event is triggered right after the default API implementation logic. An event handler can respond to either one of these events. A synchronous (blocking) event handler participates in the API invocation chain and can modify the objects in the chain's flow. For example, the "before" event handlers can modify arguments of the API calls, so the default logic gets the modified objects. Similarly, an "after" handler can modify the return value (or exception) so the client application which made the API request receives the modified value. The diagram below illustrates the flow:
All built-in Backendless services emit the "before" and "after" events for every API call. The only exception to this rule is when an API is invoked from server code (for example in a beforeFind
handler your code makes an Update API call). This limitation is in place to prevent infinite recursive invocations which may drain the computing resources.
What is a Codeless Event Handler¶
A Codeless event handler is implemented using the Backendless Codeless system. There are no restrictions on the type of logic one could add into a Codeless event handler. Most common use-cases are:
- Validating data before it is saved in the database. This applies to user registrations, creating new data objects or updating existing ones.
- Enforcing a login policy.
- Monitoring data object changes.
- Injecting dynamic properties into objects before they are saved in the database.
- Injecting dynamic properties into server responses.
- Sending out push notifications based on a condition.
- Integrating with a 3rd party service.
Consider the following example. It is an event handler for the "user registration" event. The logic runs every time a user registers with the application. It checks if the user's email address ends with "yahoo.com" and if so, returns an error. This is the example which is reviewed in the Quick Start Guide: