JavaScript SDK for CloudEvents
WARNING: This module has changed its name to 'cloudevents'.
PLEASE CONSIDER UPGRADING YOUR DEPENDENCY
npm install cloudevents
The CloudEvents SDK for JavaScript.
Features
- Represent CloudEvents in memory
- Serialize and deserialize CloudEvents in different event formats.
- Send and recieve CloudEvents with via different protocol bindings.
Note: Supports CloudEvent versions 0.3, 1.0
Installation
The CloudEvents SDK requires a current LTS version of Node.js. At the moment
those are Node.js 10.x and Node.js 12.x. To install in your Node.js project:
npm install cloudevents-sdk
Receiving and Emitting Events
Receiving Events
You can choose almost any popular web framework for port binding. Use an
HTTPReceiver
to process the incoming HTTP request. The receiver accepts
binary and structured events in either the 1.0 or 0.3 protocol formats.
const {
CloudEvent,
HTTPReceiver
} = require("cloudevents-sdk");
const receiver = new HTTPReceiver();
const receivedEvent = receiver.accept(req.body, req.headers);
console.log(receivedEvent.format());
Emitting Events
To emit events, you'll need to decide whether the event should be sent in
binary or structured format, and determine what version of the CloudEvents
specification you want to send the event as.
By default, the HTTPEmitter
will emit events over HTTP POST using the
1.0 specification, in binary mode. You can emit 0.3 events by providing
the specication version in the constructor to HTTPEmitter
. To send
structured events, add that string as a parameter to emitter.sent()
.
const { CloudEvent, HTTPEmitter } = require("cloudevents-sdk");
const v1Emitter = new HTTPEmitter({
url: "https://cloudevents.io/example"
});
const event = new CloudEvent({
type, source, data
});
v1Emitter.send(event).then((response) => {
}).catch(console.error);
v1Emitter.send(event, { mode: "structured" })
.then((response) => {
}).catch(console.error);
v1Emitter.send(event, { url: "https://alternate.com/api" })
.then((response) => {
}).catch(console.error);
const v03Emitter = new HTTPEmitter({
url: "https://cloudevents.io/example",
version: "0.3"
});
v3Emitter.send(event)
.then((response) => {
}).catch(console.error);
Supported specification features
| v0.3 | v1.0 |
---|
CloudEvents Core | :heavy_check_mark: | :heavy_check_mark: |
AMQP Protocol Binding | :x: | :x: |
AVRO Event Format | :x: | :x: |
HTTP Protocol Binding | :heavy_check_mark: | :heavy_check_mark: |
JSON Event Format | :heavy_check_mark: | :heavy_check_mark: |
Kafka Protocol Binding | :x: | :x: |
NATS Protocol Binding | :x: | :x: |
STAN Protocol Binding | :x: | :x: |
Contributing
We love contributions from the community! Please check the
Contributor's Guide
for information on how to get involved.