Socket
Book a DemoInstallSign in
Socket

@djpfs/kafka-adonisjs

Package Overview
Dependencies
Maintainers
1
Versions
21
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@djpfs/kafka-adonisjs

Kafka Provider for AdonisJs

1.1.1
latest
Source
npmnpm
Version published
Maintainers
1
Created
Source

A Kafka provider for AdonisJS v5

Adonis Kafka provides an easy way to start using Kafka.


Installation

npm i @djpfs/kafka-adonisjs

Setup

node ace configure @djpfs/kafka-adonisjs

Configuration

Edit the .env file to match your Kafka configuration.

Edit the config/kafka.js file to edit the default configuration.

Usage

List topics

// file: start/kafka.js
import Kafka from '@ioc:Message/Kafka'

Kafka.admin.listTopics().then((topics: any[]) => {
  console.log('topics', topics);
});

Create topic

// file: start/kafka.js
import Kafka from '@ioc:Message/Kafka'

Kafka.admin.createTopics({
  topics: [
    {
      topic: 'messages',
      numPartitions: 1,
      replicationFactor: 1,
    },
  ],
  waitForLeaders: true,
}).then((result: any) => {
  console.log('result', result);
});

Create Consumer

Create your consumer in `start/kafka.js`. Ex:
import Kafka from '@ioc:Message/Kafka'

Kafka.on('messages', (data: any, commit: any) => {
      console.log(data)
      // commit(false) // For error transaction
      commit() // For successful transaction
});

Create Producer

Create your producer in `app/Controllers/Http` for exemple, or in any other place. Ex:
import Kafka from "@ioc:Message/Kafka";
import { HttpContextContract } from '@ioc:Adonis/Core/HttpContext'

export default class UserController {

    public async show({ params }: HttpContextContract) {
        return Kafka.send('messages', { user_id: params.id })
    }
}

To another commands

This package uses KafkaJS, so you can use all commands from KafkaJS. Ex:
import Kafka from '@ioc:Message/Kafka'

Kafka.admin.describeCluster().then((result: any) => {
  console.log('result', result);
});

Demo

You can find a demo project here.

Based on

Keywords

kafka

FAQs

Package last updated on 26 Mar 2023

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

About

Packages

Stay in touch

Get open source security insights delivered straight into your inbox.

  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc

U.S. Patent No. 12,346,443 & 12,314,394. Other pending.