Git Product home page Git Product logo

sdk-javascript's Introduction

JavaScript SDK for CloudEvents

Codacy Badge Codacy Badge Build Status npm version vulnerabilities licence

The CloudEvents SDK for JavaScript.

This module will help you to:

Note: Supported CloudEvents specification: 0.3, 1.0

A Note on Versioning

The CloudEvents protocol version is distinct from this module's version number. For example, this module may be versioned as v2.0.0 but support the v0.3 and v1.0 versions of the CloudEvent specification.

Usage

See the full working example: here.

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 --save 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,
  HTTPReceiever
} = require("cloudevents-sdk");

// Create a receiver to accept events over HTTP
const receiver = new HTTPReceiver();

// body and headers come from an incoming HTTP request, e.g. express.js
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");

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

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

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

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

// Sending a v0.3 event works the same, just let the emitter know when
// you create it that you are working with the 0.3 spec
const v03Emitter = new HTTPEmitter({
  url: "https://cloudevents.io/example",
  version: "0.3"
});

// Again, the default is to send binary events
// To send a structured event or to an alternate URL, provide those
// as parameters in a options object as above
v3Emitter.send(event)
  .then((response) => {
    // handle the response
  }).catch(console.error);

Supported specification features

v0.3 v1.0
CloudEvents Core ✔️ ✔️
AMQP Protocol Binding
AVRO Event Format
HTTP Protocol Binding ✔️ ✔️
JSON Event Format ✔️ ✔️
Kafka Protocol Binding
NATS Protocol Binding
STAN Protocol Binding

Community

Contributing

We love contributions from the community! Please check the Contributor's Guide for information on how to get involved.

sdk-javascript's People

Contributors

fabiojose avatar lance avatar danbev avatar grant avatar helio-frota avatar micheleangioni avatar pengsrc avatar kirusi avatar

Watchers

James Cloos avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.