JSPM

  • ESM via JSPM
  • ES Module Entrypoint
  • Export Map
  • Keywords
  • License
  • Repository URL
  • TypeScript Types
  • README
  • Created
  • Published
  • Downloads 8
  • Score
    100M100P100Q38135F
  • License MIT

Kafka Provider for AdonisJs

Package Exports

  • @djpfs/kafka-adonisjs
  • @djpfs/kafka-adonisjs/build/src/index.js

This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (@djpfs/kafka-adonisjs) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.

Readme

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