@@ -14,7 +14,7 @@ The CloudEvents SDK for JavaScript.
1414- Serialize and deserialize CloudEvents in different [ event formats] ( https://github.com/cloudevents/spec/blob/v1.0/spec.md#event-format ) .
1515- Send and recieve CloudEvents with via different [ protocol bindings] ( https://github.com/cloudevents/spec/blob/v1.0/spec.md#protocol-binding ) .
1616
17- _ Note:_ Supports CloudEvent versions 0.3, 1.0
17+ _ Note:_ Supports CloudEvent version 1.0
1818
1919## Installation
2020
@@ -46,9 +46,26 @@ app.post("/", (req, res) => {
4646
4747#### Emitting Events
4848
49- You can send events over HTTP in either binary or structured format
50- using the ` HTTP ` binding to create a ` Message ` which has properties
51- for ` headers ` and ` body ` .
49+ The easiest way to send events is to use the built-in HTTP emitter.
50+
51+ ``` js
52+ const { httpTransport , emitterFor , CloudEvent } = require (" cloudevents" );
53+
54+ // Create an emitter to send events to an to a reciever
55+ const emit = emitterFor (httpTransport (" https://my.receiver.com/endpoint" ));
56+
57+ // Create a new CloudEvent
58+ const ce = new CloudEvent ({ type, source, data });
59+
60+ // Send it to the endpoint - encoded as HTTP binary by default
61+ emit (ce);
62+ ```
63+
64+ If you prefer to use another transport mechanism for sending events
65+ over HTTP, you can use the ` HTTP ` binding to create a ` Message ` which
66+ has properties for ` headers ` and ` body ` , allowing greater flexibility
67+ and customization. For example, the ` axios ` module is used here to send
68+ a CloudEvent.
5269
5370``` js
5471const axios = require (" axios" ).default ;
@@ -87,30 +104,20 @@ const emit = emitterFor(sendWithAxios, { mode: Mode.BINARY });
87104emit (new CloudEvent ({ type, source, data }));
88105```
89106
90- You may also use the ` Emitter ` singleton
107+ You may also use the ` Emitter ` singleton to send your ` CloudEvents ` .
91108
92109``` js
93- const axios = require (" axios" ).default ;
94- const { emitterFor , Mode , CloudEvent , Emitter } = require (" cloudevents" );
110+ const { emitterFor , httpTransport , Mode , CloudEvent , Emitter } = require (" cloudevents" );
95111
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- }
112+ // Create a CloudEvent emitter function to send events to our receiver
113+ const emit = emitterFor (httpTransport (" https://example.com/receiver" ));
107114
108- const emit = emitterFor (sendWithAxios, { mode : Mode . BINARY });
109- // Set the emit
115+ // Use the emit() function to send a CloudEvent to its endpoint when a "cloudevent" event is emitted
116+ // (see: https://nodejs.org/api/events.html#class-eventemitter)
110117Emitter .on (" cloudevent" , emit);
111118
112119...
113- // In any part of the code will send the event
120+ // In any part of the code, calling `emit()` on a `CloudEvent` instance will send the event
114121new CloudEvent ({ type, source, data }).emit ();
115122
116123// You can also have several listeners to send the event to several endpoints
0 commit comments