The CloudEvents SDK for JavaScript.
- Represent CloudEvents in memory
- Serialize and deserialize CloudEvents in different event formats.
- Send and recieve CloudEvents with via different protocol bindings.
Note: Supports CloudEvent version 1.0
The CloudEvents SDK requires a current LTS version of Node.js. At the moment those are Node.js 12.x, Node.js 14.x and Node.js 16.x. To install in your Node.js project:
npm install cloudevents
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.
const app = require("express")();
const { HTTP } = require("cloudevents");
app.post("/", (req, res) => {
// body and headers come from an incoming HTTP request, e.g. express.js
const receivedEvent = HTTP.toEvent({ headers: req.headers, body: req.body });
console.log(receivedEvent);
});
The easiest way to send events is to use the built-in HTTP emitter.
< C5DC div class="highlight highlight-source-js notranslate position-relative overflow-auto" dir="auto" data-snippet-clipboard-copy-content="const { httpTransport, emitterFor, CloudEvent } = require("cloudevents"); // Create an emitter to send events to an to a reciever const emit = emitterFor(httpTransport("https://my.receiver.com/endpoint")); // Create a new CloudEvent const ce = new CloudEvent({ type, source, data }); // Send it to the endpoint - encoded as HTTP binary by default emit(ce);">const { httpTransport, emitterFor, CloudEvent } = require("cloudevents"); // Create an emitter to send events to an to a reciever const emit = emitterFor(httpTransport("https://my.receiver.com/endpoint")); // Create a new CloudEvent const ce = new CloudEvent({ type, source, data }); // Send it to the endpoint - encoded as HTTP binary by default emit(ce);