Skip to main content
Redhat Developers  Logo
  • Products

    Platforms

    • Red Hat Enterprise Linux
      Red Hat Enterprise Linux Icon
    • Red Hat AI
      Red Hat AI
    • Red Hat OpenShift
      Openshift icon
    • Red Hat Ansible Automation Platform
      Ansible icon
    • View All Red Hat Products

    Featured

    • Red Hat build of OpenJDK
    • Red Hat Developer Hub
    • Red Hat JBoss Enterprise Application Platform
    • Red Hat OpenShift Dev Spaces
    • Red Hat OpenShift Local
    • Red Hat Developer Sandbox

      Try Red Hat products and technologies without setup or configuration fees for 30 days with this shared Openshift and Kubernetes cluster.
    • Try at no cost
  • Technologies

    Featured

    • AI/ML
      AI/ML Icon
    • Linux
      Linux Icon
    • Kubernetes
      Cloud icon
    • Automation
      Automation Icon showing arrows moving in a circle around a gear
    • View All Technologies
    • Programming Languages & Frameworks

      • Java
      • Python
      • JavaScript
    • System Design & Architecture

      • Red Hat architecture and design patterns
      • Microservices
      • Event-Driven Architecture
      • Databases
    • Developer Productivity

      • Developer productivity
      • Developer Tools
      • GitOps
    • Automated Data Processing

      • AI/ML
      • Data Science
      • Apache Kafka on Kubernetes
    • Platform Engineering

      • DevOps
      • DevSecOps
      • Ansible automation for applications and services
    • Secure Development & Architectures

      • Security
      • Secure coding
  • Learn

    Featured

    • Kubernetes & Cloud Native
      Openshift icon
    • Linux
      Rhel icon
    • Automation
      Ansible cloud icon
    • AI/ML
      AI/ML Icon
    • View All Learning Resources

    E-Books

    • GitOps Cookbook
    • Podman in Action
    • Kubernetes Operators
    • The Path to GitOps
    • View All E-books

    Cheat Sheets

    • Linux Commands
    • Bash Commands
    • Git
    • systemd Commands
    • View All Cheat Sheets

    Documentation

    • Product Documentation
    • API Catalog
    • Legacy Documentation
  • Developer Sandbox

    Developer Sandbox

    • Access Red Hat’s products and technologies without setup or configuration, and start developing quicker than ever before with our new, no-cost sandbox environments.
    • Explore Developer Sandbox

    Featured Developer Sandbox activities

    • Get started with your Developer Sandbox
    • OpenShift virtualization and application modernization using the Developer Sandbox
    • Explore all Developer Sandbox activities

    Ready to start developing apps?

    • Try at no cost
  • Blog
  • Events
  • Videos

An introduction to JavaScript SDK for CloudEvents

March 9, 2021
Lucas Holmquist
Related topics:
Node.jsKubernetesEvent-Driven

Share:

    In today's world of serverless functions and microservices, events are everywhere. The problem is that they are described differently depending on the producer technology you use.

    Without a common standard, the burden is on developers to constantly relearn how to consume events. Not having a standard also makes it more difficult for authors of libraries and tooling to deliver event data across environments like SDKs. Recently, a new project was created to help with this effort.

    CloudEvents is a specification for describing event data in common formats to provide interoperability across services, platforms, and systems. In fact, Red Hat OpenShift Serverless Functions uses CloudEvents. For more information about this new developer feature, see Create your first serverless function with Red Hat OpenShift Serverless Functions.

    The CloudEvents specification

    The specification's goal isn’t to create yet another event format and try to force everyone to use it. Rather, we want to define common metadata for events and establish where this metadata should appear in the message being sent.

    It is a simple spec with simple goals. In fact, a CloudEvent requires only four pieces of metadata:

    • type describes what kind of event this might be (e.g., a “create” event).
    • specversion denotes the version of the spec used to create the CloudEvent.
    • source describes where the event came from.
    • id is a unique identifier that is useful for de-duping.

    There are other useful fields, like subject, which when combined with source can add a little more context to where the event originated.

    As I mentioned, the CloudEvents specification is only concerned with the common metadata listed above, and the location where this metadata is placed when sending the event.

    Currently, there are two event formats: Binary, which is the preferred format, and structured. Binary is recommended because it is additive. That is, the binary format only adds some headers to the HTTP request. If there is a middleware that doesn’t understand CloudEvents, it won’t break anything, but if that system is updated to support CloudEvents, it starts working.

    Structured formats are for those who don’t have any format currently defined and are looking for guidance on how things should be structured.

    Here is a quick example of what those two event formats might look like in raw HTTP:

    // Binary
    
    Post  /event HTTP/1.0
    Host: example.com
    Content-Type: application/json
    ce-specversion: 1.0
    ce-type: com.nodeshift.create
    ce-source: nodeshift.dev
    ce-id: 123456
    {
      "action": "createThing",
      "item": "2187"
    }
    
    
    // Structured
    
    Post  /event HTTP/1.0
    Host: example.com
    Content-Type: application/cloudevents+json
    
    {
      "specversion": "1.0"
      "type": "com.nodeshift.create"
      "source": "nodeshift.dev"
      "id": "123456"
      "data": {
        "action": "createThing",
        "item": "2187"
      }
    }
    
    

    JavaScript SDK for CloudEvents

    Of course, we don’t want to have to format these events manually. That is where the JavaScript SDK for CloudEvents comes in. There are three main goals that an SDK should accomplish:

    • Compose an event.
    • Encode an event for sending.
    • Decode an incoming event.

    Installing the JavaScript SDK is like using any other Node module:

    $ npm install cloudevents
    

    Now that we’ve seen what a CloudEvent is and how it is useful let's take a look at an example.

    Create a new CloudEvent

    First, we are going to create a new CloudEvent object:

    const { CloudEvent } = require('cloudevents');
    
    // Create a new CloudEvent
    const ce = new CloudEvent({
     type: 'com.cloudevent.fun',
     source: 'fun-with-cloud-events',
     data: { key: 'DATA' }
    });
    

    If we log this out with the object's built-in toJSON method, we might see something like this:

    console.log(ce.toJSON());
    
    {
     id: '...',
     type: 'com.cloudevent.fun',
     source: 'fun-with-cloud-events',
     specversion: '1.0',
     time: '...',
     data: { key: 'DATA' }
    }
    
    

    Sending the message

    Next, let's look at how to send this over HTTP using the binary format.

    First, we need to create our message in the binary format, which you can do easily with the HTTP.binary method. We will use the CloudEvent from the previous example:

      const message = HTTP.binary(ce);
      //const message = HTTP.structured(ce); // Showing just for completeness
    

    Again, if we log this out, it might look something like this:

     headers: {
       'content-type': 'application/json;',
       'ce-id': '...',
       'ce-type': 'com.cloudevent.fun',
       'ce-source': 'fun-with-cloud-events',
       'ce-specversion': '1.0',
       'ce-time': '...'
     },
     body: { key: 'DATA' }
    }
    

    Now that the message has been formatted properly, we can send it by using a library like Axios.

    Note that the CloudEvents SDK doesn’t handle sending messages; it only handles formatting the message headers and message body. This allows you to use any HTTP library you want to send the message.

    const axios = require('axios')
    
    axios({
     method: 'post',
     url: 'http://localhost:3000/cloudeventy',
     data: message.body,
     headers: message.headers
    }).then((response) => {
     console.log(response.data);
    });
    
    

    We are sending a POST request to the “cloudevent-y” REST endpoint. In this example, I have used a simple Express.js application, but you can use any framework you like.

    Receiving the message

    Once we have the message, we can use the HTTP.toEvent method to convert it back into a CloudEvent object.

    const express = require('express');
    const { HTTP } = require('cloudevents');
    
    const app = express();
    
    app.post('/cloudeventy', (req, res) => {
      const ce = HTTP.toEvent({
                      headers: req.headers, 
                      body: req.body
      });
     console.log(ce.toJSON());
     res.send({key: 'Event Received'});
    });
    
    

    Again, the log output looks similar to what we saw when we output the CloudEvent object:

    {
     id: '...',
     type: 'com.cloudevent.fun',
     source: 'fun-with-cloud-events',
     specversion: '1.0',
     time: '...',
     data: { key: 'DATA' }
    }
    

    Conclusion

    To learn more about the JavaScript SDK for CloudEvents, check out the GitHub project. For more information about the history, development, and design rationale behind the specification, see the CloudEvents Primer.

    Last updated: March 8, 2021

    Recent Posts

    • Why some agentic AI developers are moving code from Python to Rust

    • Confidential VMs: The core of confidential containers

    • Benchmarking with GuideLLM in air-gapped OpenShift clusters

    • Run Qwen3-Next on vLLM with Red Hat AI: A step-by-step guide

    • How to implement observability with Python and Llama Stack

    Red Hat Developers logo LinkedIn YouTube Twitter Facebook

    Products

    • Red Hat Enterprise Linux
    • Red Hat OpenShift
    • Red Hat Ansible Automation Platform

    Build

    • Developer Sandbox
    • Developer Tools
    • Interactive Tutorials
    • API Catalog

    Quicklinks

    • Learning Resources
    • E-books
    • Cheat Sheets
    • Blog
    • Events
    • Newsletter

    Communicate

    • About us
    • Contact sales
    • Find a partner
    • Report a website issue
    • Site Status Dashboard
    • Report a security problem

    RED HAT DEVELOPER

    Build here. Go anywhere.

    We serve the builders. The problem solvers who create careers with code.

    Join us if you’re a developer, software engineer, web designer, front-end designer, UX designer, computer scientist, architect, tester, product manager, project manager or team lead.

    Sign me up

    Red Hat legal and privacy links

    • About Red Hat
    • Jobs
    • Events
    • Locations
    • Contact Red Hat
    • Red Hat Blog
    • Inclusion at Red Hat
    • Cool Stuff Store
    • Red Hat Summit
    © 2025 Red Hat

    Red Hat legal and privacy links

    • Privacy statement
    • Terms of use
    • All policies and guidelines
    • Digital accessibility

    Report a website issue