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.
npm install graphql-kafkajs-subscriptions
const {KafkaPubSub} = require('graphql-kafkajs-subscriptions');
const pubSub = new KafkaPubSub({
global: {
brokers: ['localhost:9092'],
clientId: 'my-client',
},
consumer: {
groupId: 'meow-cat-1',
}
});
const message = {
meow: 'cat',
};
pubSub.publish('topic', JSON.stringify(message));
const resolvers = {
Subscription: {
cats: pubSub.asyncIterator('cats-topic'),
},
};
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: