Skip to content

Commit 3876add

Browse files
committed
docs: add Emitter logic example
1 parent e32f292 commit 3876add

File tree

1 file changed

+58
-28
lines changed

1 file changed

+58
-28
lines changed

README.md

Lines changed: 58 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# JavaScript SDK for CloudEvents
22

3-
[![Codacy Badge](https://api.codacy.com/project/badge/Grade/bd66e7c52002481993cd6d610534b0f7)](https://www.codacy.com/app/fabiojose/sdk-javascript?utm_source=github.com&utm_medium=referral&utm_content=cloudevents/sdk-javascript&utm_campaign=Badge_Grade)
4-
[![Codacy Badge](https://api.codacy.com/project/badge/Coverage/bd66e7c52002481993cd6d610534b0f7)](https://www.codacy.com/app/fabiojose/sdk-javascript?utm_source=github.com&utm_medium=referral&utm_content=cloudevents/sdk-javascript&utm_campaign=Badge_Coverage)
3+
[![Codacy Badge](https://api.codacy.com/project/badge/Grade/bd66e7c52002481993cd6d610534b0f7)](https://www.codacy.com/app/fabiojose/sdk-javascript?utm_source=github.com&utm_medium=referral&utm_content=cloudevents/sdk-javascript&utm_campaign=Badge_Grade)
4+
[![Codacy Badge](https://api.codacy.com/project/badge/Coverage/bd66e7c52002481993cd6d610534b0f7)](https://www.codacy.com/app/fabiojose/sdk-javascript?utm_source=github.com&utm_medium=referral&utm_content=cloudevents/sdk-javascript&utm_campaign=Badge_Coverage)
55
![Node.js CI](https://github.com/cloudevents/sdk-javascript/workflows/Node.js%20CI/badge.svg)
66
[![npm version](https://img.shields.io/npm/v/cloudevents.svg)](https://www.npmjs.com/package/cloudevents)
77
[![vulnerabilities](https://snyk.io/test/github/cloudevents/sdk-javascript/badge.svg)](https://snyk.io/test/github/cloudevents/sdk-javascript)
@@ -10,9 +10,9 @@ The CloudEvents SDK for JavaScript.
1010

1111
## Features
1212

13-
* Represent CloudEvents in memory
14-
* Serialize and deserialize CloudEvents in different [event formats](https://github.com/cloudevents/spec/blob/v1.0/spec.md#event-format).
15-
* Send and recieve CloudEvents with via different [protocol bindings](https://github.com/cloudevents/spec/blob/v1.0/spec.md#protocol-binding).
13+
- Represent CloudEvents in memory
14+
- Serialize and deserialize CloudEvents in different [event formats](https://github.com/cloudevents/spec/blob/v1.0/spec.md#event-format).
15+
- Send and recieve CloudEvents with via different [protocol bindings](https://github.com/cloudevents/spec/blob/v1.0/spec.md#protocol-binding).
1616

1717
_Note:_ Supports CloudEvent versions 0.3, 1.0
1818

@@ -51,16 +51,15 @@ using the `HTTP` binding to create a `Message` which has properties
5151
for `headers` and `body`.
5252

5353
```js
54-
const axios = require('axios').default;
54+
const axios = require("axios").default;
5555
const { HTTP } = require("cloudevents");
5656

57-
58-
const ce = new CloudEvent({ type, source, data })
57+
const ce = new CloudEvent({ type, source, data });
5958
const message = HTTP.binary(ce); // Or HTTP.structured(ce)
6059

6160
axios({
62-
method: 'post',
63-
url: '...',
61+
method: "post",
62+
url: "...",
6463
data: message.body,
6564
headers: message.headers,
6665
});
@@ -69,16 +68,16 @@ axios({
6968
You may also use the `emitterFor()` function as a convenience.
7069

7170
```js
72-
const axios = require('axios').default;
71+
const axios = require("axios").default;
7372
const { emitterFor, Mode } = require("cloudevents");
7473

7574
function sendWithAxios(message) {
7675
// Do what you need with the message headers
7776
// and body in this function, then send the
7877
// event
7978
axios({
80-
method: 'post',
81-
url: '...',
79+
method: "post",
80+
url: "...",
8281
data: message.body,
8382
headers: message.headers,
8483
});
@@ -88,9 +87,38 @@ const emit = emitterFor(sendWithAxios, { mode: Mode.BINARY });
8887
emit(new CloudEvent({ type, source, data }));
8988
```
9089

90+
You may also use the `Emitter` singleton
91+
92+
```js
93+
const axios = require("axios").default;
94+
const { emitterFor, Mode, CloudEvent, Emitter } = require("cloudevents");
95+
96+
function sendWithAxios(message) {
97+
// Do what you need with the message headers
98+
// and body in this function, then send the
99+
// event
100+
axios({
101+
method: "post",
102+
url: "...",
103+
data: message.body,
104+
headers: message.headers,
105+
});
106+
}
107+
108+
const emit = emitterFor(sendWithAxios, { mode: Mode.BINARY });
109+
// Set the emit
110+
Emitter.getSingleton().on("event", emit);
111+
112+
...
113+
// In any part of the code will send the event
114+
new CloudEvent({ type, source, data }).emit();
115+
116+
// You can also have several listener to send the event to several endpoint
117+
```
118+
91119
## CloudEvent Objects
92120

93-
All created `CloudEvent` objects are read-only. If you need to update a property or add a new extension to an existing cloud event object, you can use the `cloneWith` method. This will return a new `CloudEvent` with any update or new properties. For example:
121+
All created `CloudEvent` objects are read-only. If you need to update a property or add a new extension to an existing cloud event object, you can use the `cloneWith` method. This will return a new `CloudEvent` with any update or new properties. For example:
94122

95123
```js
96124
const {
@@ -112,24 +140,26 @@ There you will find Express.js, TypeScript and Websocket examples.
112140

113141
## Supported specification features
114142

115-
| Core Specification | [v0.3](https://github.com/cloudevents/spec/blob/v0.3/spec.md) | [v1.0](https://github.com/cloudevents/spec/blob/v1.0/spec.md) |
116-
| ----------------------------- | --- | --- |
117-
| CloudEvents Core | :heavy_check_mark: | :heavy_check_mark: |
143+
| Core Specification | [v0.3](https://github.com/cloudevents/spec/blob/v0.3/spec.md) | [v1.0](https://github.com/cloudevents/spec/blob/v1.0/spec.md) |
144+
| ------------------ | ------------------------------------------------------------- | ------------------------------------------------------------- |
145+
| CloudEvents Core | :heavy_check_mark: | :heavy_check_mark: |
146+
118147
---
119148

120-
| Event Formats | [v0.3](https://github.com/cloudevents/spec/tree/v0.3) | [v1.0](https://github.com/cloudevents/spec/tree/v1.0) |
121-
| ----------------------------- | --- | --- |
122-
| AVRO Event Format | :x: | :x: |
123-
| JSON Event Format | :heavy_check_mark: | :heavy_check_mark: |
149+
| Event Formats | [v0.3](https://github.com/cloudevents/spec/tree/v0.3) | [v1.0](https://github.com/cloudevents/spec/tree/v1.0) |
150+
| ----------------- | ----------------------------------------------------- | ----------------------------------------------------- |
151+
| AVRO Event Format | :x: | :x: |
152+
| JSON Event Format | :heavy_check_mark: | :heavy_check_mark: |
153+
124154
---
125155

126-
| Transport Protocols | [v0.3](https://github.com/cloudevents/spec/tree/v0.3) | [v1.0](https://github.com/cloudevents/spec/tree/v1.0) |
127-
| ----------------------------- | --- | --- |
128-
| AMQP Protocol Binding | :x: | :x: |
129-
| HTTP Protocol Binding | :heavy_check_mark: | :heavy_check_mark: |
130-
| Kafka Protocol Binding | :x: | :x: |
131-
| MQTT Protocol Binding | :x: | :x: |
132-
| NATS Protocol Binding | :x: | :x: |
156+
| Transport Protocols | [v0.3](https://github.com/cloudevents/spec/tree/v0.3) | [v1.0](https://github.com/cloudevents/spec/tree/v1.0) |
157+
| ---------------------- | ----------------------------------------------------- | ----------------------------------------------------- |
158+
| AMQP Protocol Binding | :x: | :x: |
159+
| HTTP Protocol Binding | :heavy_check_mark: | :heavy_check_mark: |
160+
| Kafka Protocol Binding | :x: | :x: |
161+
| MQTT Protocol Binding | :x: | :x: |
162+
| NATS Protocol Binding | :x: | :x: |
133163

134164
## Community
135165

0 commit comments

Comments
 (0)