Skip to content

Commit

Permalink
docs: update README with latest API changes (#347)
Browse files Browse the repository at this point in the history
Signed-off-by: Lance Ball <lball@redhat.com>
  • Loading branch information
lance authored Oct 1, 2020
1 parent 76688c4 commit 138de37
Showing 1 changed file with 40 additions and 43 deletions.
83 changes: 40 additions & 43 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,66 +29,63 @@ npm install cloudevents

#### Receiving Events

You can choose almost any popular web framework for port binding. Use a
`Receiver` to process the incoming HTTP request. The receiver accepts
binary and structured events in either the 1.0 or 0.3 protocol formats.
You can choose any popular web framework for port binding. A `CloudEvent`
object can be created by simply providing the `HTTP` protocol binding
the incoming headers and request body.

```js
const app = require("express")();
const {Receiver} = require("cloudevents");
const { HTTP } = require("cloudevents");

app.post("/", (req, res) => {
// body and headers come from an incoming HTTP request, e.g. express.js
const receivedEvent = Receiver.accept(req.headers, req.body);
const receivedEvent = HTTP.toEvent({ headers: req.headers, body: req.body });
console.log(receivedEvent);
});
```

#### Emitting Events

You can send events over HTTP in either binary or structured format.

By default, the `Emitter` will emit events over HTTP POST using the
binary transport protocol. The `Emitter` will examine the `specversion`
of the event being sent, and use the appropriate protocol version. To send
structured events, add `Protocol.HTTPStructured` as a parameter to
`emitter.send()`.
You can send events over HTTP in either binary or structured format
using the `HTTP` binding to create a `Message` which has properties
for `headers` and `body`.

```js
const { CloudEvent, Emitter, Protocol, Version } = require("cloudevents");
const axios = require('axios').default;
const { HTTP } = require("cloudevents");

// With only an endpoint URL, this creates a v1 emitter
const emitter = new Emitter({
url: "https://cloudevents.io/example"
});
const event = new CloudEvent({
type, source, data

const ce = new CloudEvent({ type, source, data })
const message = HTTP.binary(ce); // Or HTTP.structured(ce)

axios({
method: 'post',
url: '...',
data: message.body,
headers: message.headers,
});
```

You may also use the `emitterFor()` function as a convenience.

// By default, the emitter will send binary events
emitter.send(event).then((response) => {
// handle the response
}).catch(console.error);

// To send a structured event, just add that as an option
emitter.send(event, { protocol: Protocol.HTTPStructured })
.then((response) => {
// handle the response
}).catch(console.error);

// To send an event to an alternate URL, add that as an option
emitter.send(event, { url: "https://alternate.com/api" })
.then((response) => {
// handle the response
}).catch(console.error);

// Sending a v0.3 event works the same, If your event has a
// specversion property of Version.V03, then it will be sent
// using the 0.3 transport protocol
emitter.send(new CloudEvent({ specversion: Version.V03, source, type }))
.then((response) => {
// handle the response
}).catch(console.error);
```js
const axios = require('axios').default;
const { emitterFor, Mode } = require("cloudevents");

function sendWithAxios(message) {
// Do what you need with the message headers
// and body in this function, then send the
// event
axios({
method: 'post',
url: '...',
data: message.body,
headers: message.headers,
});
}

const emit = emitterFor(sendWithAxios, { mode: Mode.BINARY });
emit(new CloudEvent({ type, source, data }));
```

## CloudEvent Objects
Expand Down

0 comments on commit 138de37

Please sign in to comment.