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 🙏

© 2024 – Pkg Stats / Ryan Hefner

@indoomni/strapi-plugin-kafkajs

v1.2.11

Published

Wrapper for KafkaJS library to be used with Strapi apps.

Downloads

186

Readme

🚀 Getting started with Strapi

Wrapper for tulios' KafkaJS library to be used with Strapi apps. You are going to need a live Kafka instance/cluster to be running. The Strapi app will be able to act as publisher and/or subscriber. As added bonus, you can setup multiple publishers as well as subscribers within the same Strapi app; e.g., one for logging, one for notifications, one for... You name it.

Installation

Add the library to your Strapi project. Learn more


yarn add @indoomni/strapi-plugin-kafkajs
yarn install
yarn build

Configuration

Add the following configuration attributes to your server.js.


# config/server.js or config/env/**/server.js
# -------------------------------------------

module.exports = ({ env }) => ({
  host: env('HOST'),
  port: env.int('PORT'),
  app: {
    env: env('ENV'),
    name: env('APP_NAME'),
    keys: env.array('APP_KEYS'),
  },
  // ...
  kafkajs: {
    enabled: true,
    config: {
      publishers: [
        {
          enabled: true,
          clientId: `${env('APP_NAME')}_${env('ENV')}_log_pub`,
          topic: `Ngapi_${env('ENV')}_logs`,
          brokers: ['kafka:9092'],
        },
      ],
      subscribers: [
        {
          enabled: true,
          clientId: `${env('APP_NAME')}_${env('ENV')}_log_sub`,
          topic: `Ngapi_${env('ENV')}_logs`,
          brokers: ['kafka:9092'],
          handler: 'kafka-logger.js',
        },
      ],
    },
  },
  // ...
});

Notice the publishers attribute. Here, you can add multiple producers where each one will be identified by its clientId. When connection to each broker(s) is established, you will be able to publish messages/jobs into the said topic.

Now, notice the subscribers attribute. Here, you can add multiple consumers where each one will be identified also by its clientId. When connection to each broker(s) is established, the subscriber will merge into available consumer groups managed by the broker and its Zookeeper, and will subscribe (or "listen" if that's your thing) to the said topic. The library will automatically poll for new messages/jobs; any incoming message/job will be fed into an asynchronous "eachMessage" function in the Javascript file pointed by the handler attribute. We'll discuss this along.

How to publish messages or jobs?

Anywhere in your code (for example in a middleware to stream access logs to the some logger on the other side), write as in the following snippet. Refer to the publisher controller inside the library, and call publish function. Keep note of the set client ID and topic set up in the Configuration step.


# src/middlewares/kafkaLogger.js
# ------------------------------

// ...
setTimeout(async () => {
  const log = {
    uuid: ctx.uuid,
    timestamp: ctx.timestamp,
    timer: Math.ceil(Date.now() - start) + 'ms',
    request,
    response,
    stack: ctx.stack,
  };
  const clientId = `${strapi.appName}_${strapi.appEnv}_log_pub`;
  const topic = `Ngapi_${strapi.appEnv}_logs`;
  const message = JSON.stringify(log);
  await strapi
    .plugin('kafkajs')
    .controller('publisher')
    .publish(clientId, topic, message);
}, 0);
// ...

How to subscribe to messages or jobs?

Remember the handler attribute from the Configuration step? In the example configuration, it should point to your own implementation, named .../src/handler.js. If you put the JS file somewhere else, just modify the path. The path is relative to folder src in your code.


# src/handler.js
# --------------

'use strict';

module.exports = {
  eachMessage: async ({
    topic,
    partition,
    offset,
    message,
  }) => {
    const payload = message.value.toString();
    strapi.log.info(
      `I am sample handler file ${__filename}, edit me..`,
    );
    strapi.log.info(
      `Received message from topic: ${topic} partition: ${partition} offset: ${offset}`,
    );
    try {
      let object;
      if ((object = JSON.parse(payload))) {
        strapi.log.info('Do something...');
        // Do something..
        strapi.log.info('Done!');
      }
    } catch (err) {
      strapi.log.info('Pass!');
    }
  },
};

📚 Learn more


Feel free to check out my GitHub repository. Your feedback and contributions are welcome!