invokeFunction
Updated at:
Use this API to invoke a specified function.
Description
Both function invocation and publishing a message can be used to pass messages between processes (functions). The differences are as follows:
- Function invocation is bi-directional. The caller sends a message to the callee and receives a return message after the callee processes it.
- Publishing a message is one-way. The sender does not need a return message from the callee.
invokeFunction(params, callback)
| Parameter | Type | Description |
| params | object | The parameter object. For the required parameters, see Table params parameter description. |
| callback(err, data) | function | The callback function. It follows standard JavaScript practices. For details, see Table callback parameter description. |
| Parameter | Type | Description |
| serviceName | String | The service name. This is the name of the service to which the function belongs in Alibaba Cloud Function Compute. |
| functionName | String | The function name. This is the name of the function set in Alibaba Cloud Function Compute. |
| invocationType | String | The invocation type.
|
| payload | String|Buffer | The input for the function. |
| Parameter | Type | Description |
| err | Error |
|
| data | object | The return value of the invoked function. |
Invocation examples
- Caller function example
This example shows how to call the helloworld function in the EdgeFC service.
'use strict'; const leSdk = require('linkedge-core-sdk'); const fc = new leSdk.FCClient(); module.exports.handler = function (event, context, callback) { var ctx = { custom: { data: 'Custom context from Invoker', }, }; var invokerContext = Buffer.from(JSON.stringify(ctx)).toString('base64'); var invokeParams = { serviceName: 'EdgeFC', functionName: 'helloworld', invocationType: 'Sync', invokerContext: invokerContext, payload: 'String message from Invoker.', }; fc.invokeFunction(invokeParams, (err, data) => { if (err) { console.log(err); } else { console.log(data.payload); // Message from helloworld } callback(null); }); }; - Callee function example
The following helloworld function code shows how the callee function parses the parameters from the caller and returns a result to the caller.
module.exports.handler = function(event, context, callback) { console.log(event); // String message from Invoker. console.log(context); // { requestId: '4', invokerContext: {custom: { data: 'Custom data from Invoker' }}} console.log(context.invokerContext.custom.data); // Custom data from Invoker console.log('-- hello world'); callback(null, 'Message from helloworld'); // Return the result to Invoker. };
Is this page helpful?