Skip to content

Commit ef31b68

Browse files
committed
squash: update guide with more examples
Signed-off-by: Lucas Holmquist <[email protected]>
1 parent f690926 commit ef31b68

File tree

1 file changed

+29
-2
lines changed

1 file changed

+29
-2
lines changed

API_TRANSITION_GUIDE.md

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,6 @@ app.post("/", (req, res) => {
2828

2929
#### Emitter
3030

31-
The `Emitter` class has been changed with the removal of `Emitter.send()` and the addition of `Emitter.emit()`. This change puts the responsibility for network transport on the developer. See the example below.
32-
3331
`Emit.send` should be transitioned to `HTTP.binary` for binary events and `HTTP.structured` for structured events
3432

3533
`Emit.send` would use axios to emit the events. Since this now longer available, you are free to choose your own transport protocol.
@@ -73,3 +71,32 @@ function sendWithAxios(message) {
7371
const emit = emitterFor(sendWithAxios, { mode: Mode.BINARY });
7472
emit(new CloudEvent({ type, source, data }));
7573
```
74+
75+
You may also use the `Emitter` singleton
76+
77+
```js
78+
const axios = require("axios").default;
79+
const { emitterFor, Mode, CloudEvent, Emitter } = require("cloudevents");
80+
81+
function sendWithAxios(message) {
82+
// Do what you need with the message headers
83+
// and body in this function, then send the
84+
// event
85+
axios({
86+
method: "post",
87+
url: "...",
88+
data: message.body,
89+
headers: message.headers,
90+
});
91+
}
92+
93+
const emit = emitterFor(sendWithAxios, { mode: Mode.BINARY });
94+
// Set the emit
95+
Emitter.on("cloudevent", emit);
96+
97+
...
98+
// In any part of the code will send the event
99+
new CloudEvent({ type, source, data }).emit();
100+
101+
// You can also have several listener to send the event to several endpoint
102+
```

0 commit comments

Comments
 (0)