Defining Cloud Functions

Defining Cloud Functions

You can define Cloud Functions either from the Telerik Platform portal or by using the Backend Services Metadata API.

Structure

The most important property of a cloud function is its name. The name becomes a part of the RESTful endpoint that you use to execute the cloud function. It must be unique within the app.

Cloud are defined in the following manner:

Everlive.CloudFunction.onRequest(function (request, response, done) {});

The request object contains information about the cloud function request, such as the HTTP method that was used, the query string, and the principal of the client who executed it, along with any posted data and headers.

You can set a custom response using the response object using a set of predefined properties, including setting a status code, custom headers, and a custom result.

The result of the cloud function is set using the result property of the response object like so:

response.body = 'My custom result';

The status code of the function can be set using the statusCode property:

response.statusCode = 200;

The response headers can be set using the headers property:

response.headers = {
    "HTTP-header-name": "header-value"
}

Each new cloud function is automatically assigned the following default cloud code:

Everlive.CloudFunction.onRequest(function (request, response, done) {
    done();
});

On executing the default cloud function you will receive an empty JSON object as a response, with a 200 status code.

Example

For example, if you need to format your response as HTML instead of the default JSON, write your cloud function like this:

Everlive.CloudFunction.onRequest(function(request, response, done) { 
    response.body = "<html><head></head><body><h2>Sample body</h2></body></html>";
    response.headers = {        
        "Content-type": "text/html"    
    };

    done();
});

Parameters

You can pass parameters to the cloud function as URL parameters in the query string.

https://api.everlive.com/v1/your-app-id/functions/MyCloudFunction?a=1&b=2&c=3

The query string can be accessed in the request.queryString object.

Everlive.CloudFunction.onRequest(function (request, response, done) {

    if (request.queryString) {
        response.body = request.queryString;
        done();
    } else {
        done();
    }
});

The result of the above function is the following:

{"a":"1","b":"2","c":"3"}

From the body of a cloud function you can access your content types, send push notifications, send SMSs, make external requests and use the other pieces of functionality supported by the Cloud Code API.

For instance, you can call your Cloud Function with parameters and delete items from a given content type. The following code deletes from the content type "Messages" all items whose "Message" field has a value from the values contained ("is in") in the array in the Delete key of the query string.

Use this code for the Cloud Function:

Everlive.CloudFunction.onRequest(function (request, response, done) {

        if (request.queryString && request.queryString.Delete) {
            var filterCondition = JSON.parse(request.queryString.Delete);

            var query = new Everlive.Sdk.Query();
            query.where().isin("Message", filterCondition);

            var data = Everlive.Sdk.$.data('Messages');

            data.destroy(query, function (data) {
                console.log("Deleted items: " + data.result);
                done();
            }, function (err) {
                console.log("Error occurred: " + err.message);
            });
        } else {
            done();
        }
    });

Then, call the cloud function like this:

https://api.everlive.com/v1/your-app-id/functions/MyCloudFunction?Delete=["MessageText1", "MessageText2", "MessageText3"]``

Permissions

Each cloud function has permissions that control who can execute the function. You can read more about permissions in the security section.

Start a free trial Request a demo
Contact us: +1-888-365-2779
sales@telerik.com
Copyright © 2016-2017, Progress Software Corporation and/or its subsidiaries or affiliates. All rights reserved.