Coder Social home page Coder Social logo

sidharthv96 / sdk-javascript Goto Github PK

View Code? Open in Web Editor NEW

This project forked from cloudevents/sdk-javascript

0.0 2.0 0.0 1.82 MB

Javascript SDK for CloudEvents

Home Page: https://cloudevents.github.io/sdk-javascript/

License: Apache License 2.0

JavaScript 0.55% TypeScript 99.45%

sdk-javascript's Introduction

JavaScript SDK for CloudEvents

Codacy Badge Codacy Badge Node.js CI npm version vulnerabilities

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

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

// body and headers come from an incoming HTTP request, e.g. express.js
const receivedEvent = receiver.accept(req.headers, 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().

const { CloudEvent, Emitter, Protocol, Version } = require("cloudevents-sdk");

// 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
});

// 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);

CloudEvent Objects

All created CloudEvent objects are read-only. If you need to update a property or add a new extension to an existing cloud event object, you can use the cloneWith method. This will return a new CloudEvent with any update or new properties. For example:

const {
  CloudEvent,
} = require("cloudevents-sdk");

// Create a new CloudEvent
const ce = new CloudEvent({...});

// Add a new extension to an existing CloudEvent
const ce2 = ce.cloneWith({extension: "Value"});

Example Applications

There are a few trivial example applications in the examples folder. There you will find Express.js, TypeScript and Websocket examples.

Supported specification features

Core Specification v0.3 v1.0
CloudEvents Core ✔️ ✔️

Event Formats v0.3 v1.0
AVRO Event Format
JSON Event Format ✔️ ✔️

Transport Protocols v0.3 v1.0
AMQP Protocol Binding
HTTP Protocol Binding ✔️ ✔️
Kafka Protocol Binding
MQTT Protocol Binding
NATS 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

aliok avatar danbev avatar dependabot[bot] avatar fabiojose avatar grant avatar helio-frota avatar kirusi avatar lance avatar lholmquist avatar matejvasek avatar micheleangioni avatar pengsrc avatar snyk-bot avatar

Watchers

 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.