npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

cloudevents-kafka

v0.2.1

Published

Kafka transport plugin for CloudEvents JS SDK

Downloads

295

Readme

CloudEvents JS SDK Kafka

Kafka transport extension for CloudEvents JS SDK

Description

Allow serialise and deserialise CloudEvents for kafka protocol.

Installation

npm install cloudevents cloudevents-kafka kafkajs
# or
yarn add cloudevents cloudevents-kafka kafkajs

Note: For examples will be used kafkajs, but you can use any other client library.

Usage

Strict CloudEvent

Default CloudEvent constructor do not strictly check input object, for enable strict mode this library have two classes.

import { Version, CloudEvent } from 'cloudevents'
import {CloudEventStrict, CloudEventStrictV1} from "cloudevents-kafka"

// Will throw runtime exceptions about missing `id` field
const ce = new CloudEvent({
    specversion: Version.V1,
    source: 'some-source',
    // id: 'some-id',
    type: 'message.send'
})

// Will show typescript error during compilation about missing `id` field
const ces = new CloudEventStrict({
    specversion: Version.V1,
    source: 'some-source',
    // id: 'some-id',
    type: 'message.send'
})

// Will show typescript error during compilation about missing `id` field
const cev1 = new CloudEventStrictV1({
    source: 'some-source',
    // id: 'some-id',
    type: 'message.send'
})

Receiving Events

If received valid KafkaMessage it will be dedeserialized as CloudEvent

kafka = new Kafka({
    clientId: 'test-app',
    brokers: ['kafka:9092']
})


consumer = kafka.consumer({ groupId: 'test-group' })

await consumer.connect()
await consumer.subscribe({ topic: 'test-topic', fromBeginning: true })

await consumer.run({
    eachMessage: async ({message}: EachMessagePayload) => {
        const receivedEvent = CeKafka.deserialize(message)
        console.log(receivedEvent); // will be valid CloudEvent
    }
})

Emitting Events

CloudEvent will be serialised as KafkaMessage object, which will contain key, value, header and timestamp fields, which yhou can send using any client.

import { Version } from 'cloudevents'
import {Consumer, EachMessagePayload, Kafka, Producer} from 'kafkajs'
import * as CeKafka from "cloudevents-kafka"
const {CloudEventStrict} = CeKafka


kafka = new Kafka({
    clientId: 'test-app',
    brokers: ['kafka:9092']
})

producer = kafka.producer()

await producer.connect()

const ce = new CloudEventStrict({
    specversion: Version.V1,
    source: 'some-source',
    id: 'some-id',
    type: 'message.send'
})
const messsage = CeKafka.structured(ce)

await producer.send({
    topic: 'test-topic',
    messages: [
        messsage,
    ],
})

Patitions Key

For define key property of kafka message you can add partitionkey field to your event

const ce = new CloudEvent({
    specversion: Version.V1,
    source: 'some-source',
    id: 'some-id',
    type: 'message.send',
    partitionkey: 'some-partitionkey'
})

const messsage = CeKafka.structured(ce)
console.log(message.key) // some-partitionkey

Development

Firstly save alias in /etc/hosts

127.0.0.1       localhost kafka

Start kafka

docker-compose up kafka

Install dependencies

yarn

Run tests

yarn test