About Event Handlers

Top  Previous  Next

An event handler is a custom code which responds to an API event. For every API call Backendless generates two kinds of events - "before" and "after". The before event is generated before the default logic of the API implementation is executed and the "after" event is fired right after the default API implementation logic. An event handler can respond to either one of these events. A synchronous 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 the 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:

backendless-incocation-chain

Each Backendless service is responsible for emitting the "before" and "after" events for each API call. For example, the following code demonstrates custom code wrapper for the "before" event of the Publish API call from the Backendless Messaging service:

Backendless.ServerCode.Messaging.beforePublish('default', function(req) {
  // your code goes here
});
There are several important design elements in the code snippet:

Backendless.ServerCode.Messaging identifies the Messaging service for which the event handler will be injecting itself into the API invocation handling chain.
beforePublish indicates the event handler will be invoked before the default logic of the publish method in the Backendless messaging service is executed.
The first argument of the beforePublish method specifies the "asset" or in this case the messaging channel for which the code will be called.

 

CodeRunner Context

The req argument is present in all event handlers. It provides access to the original arguments of the API call (the ones sent by the calling client) as well as a special object representing the context of the API event. The context object is available through req.context. It provides access to the following values:

req.context.userId- objectId of a user logged in in the client application which made the API call. If there is no logged in user, this value is null.
req.context.userToken- value of the user-token representing session of the currently logged in user in the client application which made the API call. If there is no logged in user, this value is null.
req.context.userRoles- an array of security roles Backendless assigned to the request.

 

The table below provides a summary for all available services. The "Asset" column indicates the type of resource that is the target of the API call which triggered the event. It also helps to think of the "asset" as the context of an event and a container of the object which is created/modified/deleted by the API call. For example, for the Data Service, asset is a table and for Messaging Service it the channel.

Service

Server Code Class to Register Handlers

Asset

Available Events (each item has "before" and "after" events)

User Service

 

Backendless.Server.User

 

N/A

Login

Register

Update

Remove

Describe

RestorePassword

Logout

Data Service

 

Backendless.Server.Persistence

Table

Create

FindById

LoadRelations

Remove

Update

Describe

Find

FindFirst

FindLast

Messaging Service

 

Backendless.Server.Messaging

Messaging Channel

Publish

Subscribe

Cancel

Poll

DeviceRegistration

Media Service

 

Backendless.Server.Media

N/A

acceptConnection

publishStarted

publishFinished

streamCreated

streamFinished

File Service

 

Backendless.Server.File

Directory or file in the root of the file storage

MoveToRepositoryDeleteFileOrDirectory

Geo Service

 

Backendless.Server.Geo

Geo category

AddPoint

UpdatePoint

RemovePoint

GetCategories

GetPoints

AddCategory

DeleteCategory

RelativeFind

 


Please let us know how we can improve the documentation by leaving a comment. All technical questions should be posted to the Backendless Support forum. We do not respond to the technical questions on the documentation pages.: