Skip to main content

Web Cloud Function

Web Cloud Function (Web Function)is a type of cloud function, distinct fromEvent Function` which has restrictions on event formats. Web Cloud Function can directly receive and process HTTP requests.

Unlike Event Functions that only require writing a function like export function main (event, context) { / business logic / }, Web Functions require writing complete HTTP service code and listening on port 9000. Frameworks such as Express.jsandKoa.js` can be used to write Web Functions. Web Functions are essentially a form of function-based service hosting.

Because Web Function can directly handle HTTP requests, therefore, Web Function can perceive HTTP requests, support SSE & WebSocket, and provide capabilities such as file upload and download.

Web Functions require writing complete HTTP service code, which is relatively cumbersome. By using the @cloudbase/functions-framework` framework, you can directly use a functional writing style to implement Web Functions, or convert ordinary functions into Web Functions to gain the capabilities of Web Functions.

Conventional Web Function = Runtime environment + User service code (Express.js/Koa.js), while Web Function developed using the @cloudbase/functions-framework = Runtime environment + Function runtime framework (@cloudbase/functions-framework) + User function code.

How to Write Web Function Code?

CloudBase Web Function can be written using the @cloudbase/functions-framework framework. Function-based hosting also relies on this framework, so the coding approach is similar.

Below is a simple example of writing a Web Function using the @cloudbase/functions-framework framework. It includes 3 files:

  • scf_bootstrap # Project bootstrap script file. The cloud function platform starts the function service by executing this file. This file must be packaged and uploaded with the code to the cloud function.
  • package.json # Function-related file
  • index.js # Function-related file

The difference in project file structure between Web Function and Event Function lies in that Web Function requires writing a scf_bootstrap bootstrap script file.

  • scf_bootstrap
#!/bin/bash

node node_modules/@cloudbase/functions-framework/bin/tcb-ff.js --port=9000 --logDirname=/tmp/logs --interceptOutput=false --extendedContextKey=X-Cloudbase-Context
  • package.json
{
"name": "example",
"version": "1.0.0",
"main": "index.js",
"dependencies": {
"@cloudbase/functions-framework": "^1.14.0"
}
}
  • index.js
exports.main = async function main (event, context) {
// Switch to SSE mode
const sse = context.sse()
console.log(sse, 'sse')

sse.on('close', () => {
console.log('sse closed')
})

// Send events to the client; check if the connection is closed before sending, and send only if it is not closed.
if (!sse.closed) {
sse.send({ data: 'hello from sse function, abcedfg..., €€€€€⭐⭐⭐⭐⭐' })
sse.send({ data: 'a\n\n\n\nb\r\r\r\rc\n\r\nd' })
sse.send({ data: { a: 1, b: 2, c: { a:1,b:2 } } })
sse.send({ data: JSON.stringify({ a: 1, b: 2, c: { a:1,b:2 } }) })
sse.send({ data: Buffer.from('this is a message in buffer.') })
}

console.log('function end...')

return ''
}

As shown above, the main difference in function code compared to Event Function is that it requires a scf_bootstrap bootstrap script, while the coding approach is not significantly different from Event Function.

Note: Although the coding approach remains functional, the operational mechanism of Web Function differs from Event Function. Therefore, there are differences in the function input parameters event and context, as well as in environment variables.

For a more complete example, refer to: https://github.com/TencentCloudBase/func-v2-template/tree/webfunc

How to Invoke a Web Function?

You can invoke a Web Function via the cloud function's HTTP API. For details, refer to the documentation: Cloud Function HTTP API.

Invoking the HTTP API of a Web Function requires including an Authorization header to pass the Access Token using the Bearer scheme. For how to obtain the Access Token, refer to the documentation: Access Token.

Invoking a Web Function sample:

curl -L 'https://{your-envId}.api.tcloudbasegateway.com/v1/functions/{name}?webfn=true' \
-H 'Content-Type: application/json' \
-H 'Accept: application/json' \
-H 'Authorization: Bearer <TOKEN>' \
-d '{}'

Note: The parameter webfn=true must be specified; otherwise, the invocation will fail.