Cloud Functions are an incredibly flexible tool. Fetch any kind of data from the net, process it, store it in your Cloud Database or return it to your mobile application – Cloud Functions cover all requirements.
Did you know that Cloud Functions can be executed as triggers on collections?
Just tell cloudbase.io that you want to attach the function to a collection as a trigger. Cloud Functions can be executed on update and insert for documents in any collection in your Cloud Database. The cloudbase.io servers will automatically run your Cloud Function for each record sent in a request.
A Cloud Database insert API call can post multiple records at the same time. Your Cloud Function will be executed for each record in the request.
The Cloud Functions documentation explain that all parameters sent to a function are stored in the $params variable, available within the body of your Cloud Function. When a function is executed as a trigger the document data from the Cloud Database is sent to the Cloud Function using the same array.
If the request was an insert the “inserted” property of the array will contain the new record received by the data APIs. In the case of an update the “updated” property will be populated and will contain two sub-object: old, and new – each object will contain the document before and after it was updated.
// create the Pubnub object
$pubnub = new Pubnub(
"demo", // PUBLISH_KEY
"demo", // SUBSCRIBE_KEY
"", // SECRET_KEY
"", // CIPHER KEY
true, // SSL_ON
"demo.pubnub.com" // ORIGIN
// read the new updated record
$data = $params["updated"]["new"];
// if we were doing an insert then we would look for:
$data = $params["inserted"];
// push the message to a channell
$info = $pubnub->publish(array(
'channel' => $data["channel"],
'message' => $data["message"]
Cloud Functions can also be scheduled for execution or called on-demand from your mobile application. How are you planning to use them?