Run the TypeScript Example
mainnpm install and then execute the start script with npm start. This example demonstrates how to use the CloudEvents SDK within a TypeScript environment.npm install
npm startrepository·main·Indexed 18 days ago
https://github.com/cloudevents/sdk-javascriptA JavaScript SDK for implementing the CloudEvents specification, enabling developers to represent, serialize, deserialize, and transmit CloudEvents across various protocols and formats. The SDK supports Spec v1.0 and v0.3 in both Structured and Binary modes, with provided examples for Express, Kafka, MQTT, TypeScript, and WebSockets. It includes utilities for HTTP transport, custom transport integration (e.g., Axios), and an Emitter singleton for event-driven emission.
npm install and then execute the start script with npm start. This example demonstrates how to use the CloudEvents SDK within a TypeScript environment.npm install
npm startThis example demonstrates using CloudEvents over a WebSocket connection between a server and either a Node.js client or a web browser. The workflow involves a client sending a CloudEvent containing a zip code, and a server responding with a CloudEvent containing weather data.
Install the necessary dependencies using npm:
npm installThe server listens for WebSocket connections and expects incoming messages to be CloudEvents. It extracts a zip code from the event data, fetches weather information, and responds with a new CloudEvent.
Configuration: You must provide an Open Weather API key. You can either:
server.js directly to add your key.OPEN_WEATHER_API_KEY.To start the server, run:
node server.jsThe client prompts for a zip code, sends it as a CloudEvent to the server, and prints the resulting weather CloudEvent data to the console.
To start the client, run:
node client.jsTo use the web-based interface, open the index.html file in your browser. You can enter a zip code into the form field; the browser will send the CloudEvent via WebSocket and display the weather response or error messages on the screen.
To stop any process, use CTRL-C in your terminal.
npm install
node server.js
node client.jsThe CloudEvents SDK requires a current LTS version of Node.js (e.g., Node.js 16.x or 18.x). Install it in your Node.js project using npm:
npm install cloudeventsTo run the MQTT example, you must first install dependencies and compile the project. You also need a running MQTT broker (such as Eclipse Mosquitto) available on port 1883.
Install and compile the project:
npm install
npm run compileStart an MQTT broker using Docker: Run the following command to start an unauthenticated Mosquitto broker:
docker run -it -d -p 1883:1883 eclipse-mosquitto:2.0 mosquitto -c /mosquitto-no-auth.confStart the application:
npm startnpm install
npm run compile
docker run -it -d -p 1883:1883 eclipse-mosquitto:2.0 mosquitto -c /mosquitto-no-auth.conf
npm startTo run the Express example provided in this repository, navigate to the examples/express-ex directory and use the following command:
npm startTo run the Kafka example, you need a Kafka broker and Zookeeper. You can set this up using either individual Docker commands or Docker Compose.
Run Zookeeper first:
docker run -d \
--name zookeeper \
-e ZOOKEEPER_CLIENT_PORT=2181 \
-e ZOOKEEPER_TICK_TIME=2000 \
confluentinc/cp-zookeeper:7.3.2Then run Kafka, linking it to the Zookeeper container:
docker run -d \
--name kafka \
-p 9092:9092 \
-e KAFKA_BROKER_ID=1 \
-e KAFKA_ZOOKEEPER_CONNECT=localhost:2181 \
-e KAFKA_ADVERTISED_LISTENERS=PLAINTEXT://localhost:9092 \
-e KAFKA_OFFSETS_TOPIC_REPLICATION_FACTOR=1 \
-e KAFKA_GROUP_INITIAL_REBALANCE_DELAY_MS=0 \
--link zookeeper:zookeeper \
confluentinc/cp-kafka:7.3.2If you have the docker-compose file available, navigate to its directory and run:
docker compose up -ddocker run -d \
--name zookeeper \
-e ZOOKEEPER_CLIENT_PORT=2181 \
-e ZOOKEEPER_TICK_TIME=2000 \
confluentinc/cp-zookeeper:7.3.2
docker run -d \
--name kafka \
-p 9092:9092 \
-e KAFKA_BROKER_ID=1 \
-e KAFKA_ZOOKEEPER_CONNECT=localhost:2181 \
-e KAFKA_ADVERTISED_LISTENERS=PLAINTEXT://localhost:9092 \
-e KAFKA_OFFSETS_TOPIC_REPLICATION_FACTOR=1 \
-e KAFKA_GROUP_INITIAL_REBALANCE_DELAY_MS=0 \
--link zookeeper:zookeeper \
confluentinc/cp-kafka:7.3.2After setting up Kafka, you can run the example CLI application which demonstrates sending user input as a CloudEvent through a Kafka producer and handling/deserializing it with a consumer in a consumer group.
Prerequisites:
Run the following command to start the producer CLI:
npm run start:producerRun the following command to start the consumer, providing a ${groupId} as an argument:
npm run start:consumer ${groupId}# Start the producer
npm run start:producer
# Start the consumer (replace ${groupId} with your desired group ID)
npm run start:consumer my-consumer-groupThe Emit.send method has been removed. You must now manually handle the transport protocol (e.g., using axios) by first converting your CloudEvent into a transportable message using either HTTP.binary (for binary events) or HTTP.structured (for structured events).
const axios = require('axios').default;
const { HTTP } = require("cloudevents");
const ce = new CloudEvent({ type, source, data });
const message = HTTP.binary(ce); // Or HTTP.structured(ce)
axios({
method: 'post',
url: '...',
data: message.body,
headers: message.headers,
});The Receiver class has been removed in version 4.0.0. To convert incoming HTTP requests into CloudEvent objects, use the HTTP.toEvent method. This method accepts an object containing headers and body extracted from your HTTP framework (e.g., Express.js).
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);
});A Binding is the core interface used to bridge CloudEvents with specific transport protocols (like HTTP, Kafka, or MQTT). It defines how to serialize CloudEvents into messages and how to detect and deserialize messages back into CloudEvents.
When implementing a new protocol, you must provide implementations for:
binary: Serializes a CloudEvent using the binary mode (where event attributes are in the message headers).structured: Serializes a CloudEvent using the structured mode (where the event is the message body).toEvent: Converts a Message back into one or more CloudEventV1 objects.isEvent: A predicate to determine if a Message is a valid CloudEvent.import { Binding, Message, Headers } from "./message";
// Example of the shape of a Binding implementation
const myBinding: Binding<MyMessageType> = {
binary: (event) => { /* ... */ },
structured: (event) => { /* ... */ },
toEvent: (message) => { /* ... */ },
isEvent: (message) => { /* ... */ }
};The SDK provides interfaces for converting between CloudEventV1 objects and Message objects:
Serializer<M>: A function that takes a CloudEventV1<T> and returns a Message of type M.Deserializer: A function that takes a Message and returns either a single CloudEventV1<T> or an array of events CloudEventV1<T>[] (for batch modes).Detector: A predicate function used to check if a Message contains a valid CloudEvent.JavaScript's Number type loses precision for very large integers (e.g., Twitter IDs). To prevent this when parsing CloudEvents, set the environment variable CE_USE_BIG_INT to "true". This enables the json-bigint package. Note that this may slow down parsing speed by approximately 7x.
export CE_USE_BIG_INT=true