Skip to content

Commit add5844

Browse files
committed
📝 doc: update README and API docs
Signed-off-by: Haili Zhang <haili.zhang@outlook.com>
1 parent 797b912 commit add5844

File tree

3 files changed

+501
-6
lines changed

3 files changed

+501
-6
lines changed

README.md

Lines changed: 38 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -196,7 +196,44 @@ Dapr bindings allows you to trigger your applications or services with events co
196196
197197
Asynchronous function introduces Dapr pub/sub to provide a platform-agnostic API to send and receive messages. A typical use case is that you can leverage synchronous functions to receive an event in plain JSON or Cloud Events format, and then send the received event to a Dapr output binding or pub/sub component, most likely a message queue (e.g. Kafka, NATS Streaming, GCP PubSub, MQTT). Finally, the asynchronous function could be triggered from the message queue.
198198
199-
More details would be brought up to you in some quickstart samples, stay tuned.
199+
Async function use below function signature which is quite difference from that of Express style sync function:
200+
201+
```js
202+
function (ctx, data) {}
203+
```
204+
205+
* `ctx`: OpenFunction [context](https://github.com/OpenFunction/functions-framework-nodejs/blob/master/src/openfunction/function_context.ts) object
206+
* `ctx.send(payload, output?)`: Send `payload` to all or one specific `output` of Dapr Output [Binding](https://docs.dapr.io/reference/components-reference/supported-bindings/) or Pub [Broker](https://docs.dapr.io/reference/components-reference/supported-pubsub/)
207+
* Notice that `ctx.send` CAN be invoked where necessary, when you have certain outgoing data to send
208+
* `data`: Data recieved from Dapr Input Binding or Sub Broker
209+
210+
For more details about async function and demo, please check out our [Node.js Async Function Quickstart](https://openfunction-talks.netlify.app/2022/202-node-async/).
211+
212+
#### HTTP Trigger Async Function
213+
214+
Sync functions is triggered by HTTP request, so Dapr is not used in sync function input. Whenever there are functions output requirements, sync functions can also send output to Dapr output binding or pubsub components.
215+
216+
Here is another function sample:
217+
218+
* Users send a HTTP request to a [Knative Sync function](https://github.com/OpenFunction/samples/tree/main/functions/knative/with-output-binding).
219+
* This sync function handles the request and then send its output to Kafka through a Dapr Kafka output binding or pubsub component.
220+
* An [async function](https://github.com/OpenFunction/samples/tree/main/functions/async/bindings/kafka-input) is then triggered by this output event in Kafka (through a Dapr Kafka input binding or pubsub component)
221+
222+
![HTTP Trigger Async Function](https://raw.githubusercontent.com/OpenFunction/samples/main/images/knative-dapr.png)
223+
224+
Node.js Functions Framework also supports such use case, you can switch Express function signature to typical async style as below example indicates:
225+
226+
```js
227+
async function tryKnativeAsync(ctx, data) {
228+
// Receive and handle data from HTTP request's body
229+
console.log('Function should receive request: %o', data);
230+
231+
// Send output in async way via Dapr
232+
await ctx.send(data);
233+
234+
// Use `response` method to prepare data as HTTP response
235+
return ctx.response(data);
236+
```
200237
201238
### Google Cloud Functions
202239
@@ -243,5 +280,3 @@ Contributions to this library are welcome and encouraged. See
243280
[ff_node_unit_link]: https://github.com/openfunction/functions-framework-nodejs/actions?query=workflow%3A"Node.js+Unit+CI"
244281
[ff_node_lint_img]: https://github.com/openfunction/functions-framework-nodejs/workflows/Node.js%20Lint%20CI/badge.svg
245282
[ff_node_lint_link]: https://github.com/openfunction/functions-framework-nodejs/actions?query=workflow%3A"Node.js+Lint+CI"
246-
[ff_node_conformance_img]: https://github.com/openfunction/functions-framework-nodejs/workflows/Node.js%20Conformance%20CI/badge.svg
247-
[ff_node_conformance_link]: https://github.com/openfunction/functions-framework-nodejs/actions?query=workflow%3A"Node.js+Conformance+CI"

0 commit comments

Comments
 (0)