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 🙏

© 2026 – Pkg Stats / Ryan Hefner

@tamasha/kafka-connection-v2

v1.0.62

Published

Minimal KafkaJS connection helper with clean lifecycle management

Downloads

1,504

Readme

@tamasha/kafka-connection-v2

Minimal KafkaJS helper that focuses on the essentials: connect quickly, enforce per-action schemas, and subscribe with clean teardown semantics. Inspired by the original @tamasha/kafka-connection, this version keeps only the primitives needed for simple services.

Highlights

  • Lightweight wrapper around kafkajs with lazy producer and consumer creation.
  • Built-in topic schema registry keyed by action (one topic per microservice, many actions per topic).
  • Generated TypeScript payload unions for every topic/action combination.
  • Explicit lifecycle methods (connect, disconnect) to simplify graceful shutdown.
  • No external dependencies beyond kafkajs.

Installation

npm install @tamasha/kafka-connection-v2

Quick start

import {
  KafkaConnectionV2,
  USER_EVENTS_TOPIC,
  UserEventMessage,
} from "@tamasha/kafka-connection-v2";

const connection = new KafkaConnectionV2({
  clientId: "example-service",
  brokers: ["localhost:9092"],
});

await connection.connect();

const loginEvent: UserEventMessage = {
  payload: {
    action: "login",
    userId: "u-1",
    timestamp: Date.now(),
    service: "auth-service",
    sessionId: "session-xyz",
  },
};

await connection.produce({
  topic: USER_EVENTS_TOPIC,
  messages: [
    {
      value: JSON.stringify(loginEvent),
      // key will automatically be set to loginEvent.payload.userId if omitted
    },
  ],
});

await connection.subscribe(
  { groupId: "example-service-consumer" },
  USER_EVENTS_TOPIC,
  async ({ message }) => {
    const payload = JSON.parse(message.value?.toString("utf-8") ?? "{}") as UserEventMessage;
    console.log("Received:", {
      key: message.key?.toString(),
      payload,
    });
  }
);

// Later, during shutdown
await connection.disconnect();

API

new KafkaConnectionV2(config)

Creates a new connection manager. config is forwarded to the underlying Kafka constructor, with brokers required. Built-in schemas are loaded automatically; use getSchemaRegistry() if you need introspection.

connect()

Creates the internal Kafka clients (producer and consumer) lazily and verifies the brokers can be reached. Calling produce or subscribe will auto-connect if needed, but connect is useful for startup health checks.

produce(record)

Sends a single ProducerRecord. Ensures the producer is connected before sending and validates each message payload against the configured topic schema.

subscribe(consumerConfig, topics, handler, options?)

Initialises a consumer (one per connection) and starts consuming the provided topics. The handler receives the standard EachMessagePayload. Set options.useBatch = true to receive EachBatchPayload instead. Incoming messages are validated prior to invoking the handler.

disconnect()

Stops and disconnects the managed consumer and producer.

Built-in schemas & types

  • DEFAULT_TOPIC_SCHEMAS exposes the runtime registry used for validation.
  • UserEventMessage, OrderEventMessage, and KafkaEventMessage describe the allowed JSON payloads per topic/action.
  • connection.getSchemaRegistry() returns the TopicSchemaRegistry instance, useful for debugging or listing available topics/actions.
  • Partition keys derive automatically from each topic's schema (payload.userId, payload.orderId, etc.).

License

MIT © Tamasha Team