Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

kafkajs-stream

Package Overview
Dependencies
Maintainers
1
Versions
12
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

kafkajs-stream

Stream for kafkajs

  • 0.0.1
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
38
decreased by-73.43%
Maintainers
1
Weekly downloads
 
Created
Source

Stream for Kafka.js in Node.js

tested in Node.js v12

import fs from "fs";
import http from "http";
import gracefulShutdown from "http-graceful-shutdown";
import { CompressionCodecs, CompressionTypes, Kafka } from "kafkajs";
import SnappyCodec from "kafkajs-snappy";
import { times } from "lodash";
import { from } from "rxjs";
import { rxToStream } from "rxjs-stream";
import app from "./app";

import { ConsumerStream, ProducerStream } from "kafkajs-stream";

const { PORT = 4000 } = process.env;

CompressionCodecs[CompressionTypes.Snappy] = SnappyCodec;
const kafka = new Kafka({
  clientId: "my-app",
  brokers: ["bitnami-kafka:9092"]
});

// Producing
const producerStream = new ProducerStream(kafka, { topic: "test-topic" });
const number$ = from(times(200000, i => `${i.toString()},`));
rxToStream(number$).pipe(producerStream);

// Consuming
const consumerStream = new ConsumerStream(kafka, {
  config: { groupId: "test-group" },
  topic: { topic: "test-topic", fromBeginning: true }
});
const producer2Stream = new ProducerStream(kafka, { topic: "test-topic-2" });
consumerStream.pipe(producer2Stream);

const consumerStream2 = new ConsumerStream(kafka, {
  config: { groupId: "test-group-2" },
  topic: { topic: "test-topic-2", fromBeginning: true }
});
const writeStream = fs.createWriteStream("./testWrite.txt");
consumerStream2.pipe(writeStream);

(async () => {
  const server = http.createServer(app);
  server.listen(PORT);

  // register graceful shutdown
  gracefulShutdown(server, {
    onShutdown: async () => {
      await new Promise(resolve => producerStream.end(resolve));
      consumerStream.destroy();
      await new Promise(resolve => producer2Stream.end(resolve));
      consumerStream2.destroy();
      await new Promise(resolve => writeStream.end(resolve));
    }
  });

  server.on("listening", () => {
    console.log(`application is listening on port ${PORT}`);
  });
})();

Keywords

FAQs

Package last updated on 13 Dec 2019

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc