Code Monkey home page Code Monkey logo

kafka-adonisjs's Introduction

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

kafka-adonisjs's People

Watchers

 avatar  avatar

kafka-adonisjs's Issues

v6

Heya, first off, thanks for all the work producing a modern kafka plugin for Adonis's, much appreciated!

We started a v6 project and ported your plugin over and made it available as a preview here:
https://github.com/neighbourhoodie/adonis-kafka-v6

I’m wondering if you are interested in having us send a v6 PR against your repo, or if we should continue going on in our separate v5 repo. We’re happy to play it either way.

Thanks again for the hard work!

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.