1.0.0 • Published 3 years ago

@shirayukikitsune/graphql-kafkajs-subscriptions v1.0.0

Weekly downloads
-
License
BSD-2-Clause
Repository
github
Last release
3 years ago

Graphql KafkaJS Subscription

Code Style: Google

An implementation for the Apollo PubSubEngine using the KafkaJS as backend.

This project was inspired on graphql-kafka-subscriptions, but forked to use the KafkaJS library instead of node-rdkafka.

The main reason is because node-rdkafka uses librdkafka behind the scenes, needing to compile an external library that does not run as expected in all environments. KafkaJS in other hand, is fully implemented in NodeJS and all features should work where they are supposed to.

Installation

npm install graphql-kafkajs-subscriptions

Usage

Creating a PubSub

const {KafkaPubSub} = require('graphql-kafkajs-subscriptions');

const pubSub = new KafkaPubSub({
  global: {
    brokers: ['localhost:9092'],
    clientId: 'my-client',
  },
  consumer: {
    groupId: 'meow-cat-1',
  }
});

Publishing messages

const message = {
  meow: 'cat',
};

pubSub.publish('topic', JSON.stringify(message));

Using as a subscription async iterator

const resolvers = {
  Subscription: {
    cats: pubSub.asyncIterator('cats-topic'),
  },
};

Configuration

When creating the KafkaPubSub instance, you may pass all configurations to KafkaJS using the configuration keys:

const pubSub = new KafkaPubSub({
  global: {}, // This is the client configuration
  producer: {}, // This is the producer options
  consumer: {} // This is the consumer options
});

See KafkaJS reference for: