AtomicKafka is a lightweight NPM Package developed to simplify the process of establishing bidirectional, real-time data streaming with Apache Kafka in your web-app.
Website | Library | Demo Apps | Featured on Medium
AtomicKafka currently supports running Apache Kafka clusters either using a Docker image or by connecting to Confluent Cloud.
Docker:
docker-compose up -d
Confluent Cloud:
Include the following lines in your .env depending on your Kafka environment. Set the PORT variable to the port where AtomicKafkaServer will be initialized in the next step.
PORT=<USER_DEFINED>
API_KEY=
API_SECRET=
KAFKA_BOOTSTRAP_SERVER=localhost:9092
PORT=
API_KEY=<API_ACCESS_KEY>
API_SECRET=<API_ACCESS_SECRET>
KAFKA_BOOTSTRAP_SERVER=<BOOTSTRAP_SERVER>
$ npm install atomic-kafka
Initialize a server instance of your choice (HTTP, Node.js, etc). The example below contemplates a Node.js Express server.
ATTENTION: a Server instance must be created for every remote AtomicKafkaClient.
/* initialize and configure Node.js expressApp according to user specifications
then add the following: */
const AtomicKafkaServer = require('atomic-kafka/server');
const server = expressApp.listen(process.env.PORT, () => {
console.log(`Listening on port ${process.env.PORT}`);
})
const aks = new AtomicKafkaServer(server);
/* AKS_Producer_Init */
aks.newProducer('topic');
aks.globalProduce('produceMessageEvent', 'topic');
/* AKS_Consumer_Init */
aks.newConsumer('group_ID');
aks.socketConsume('group_ID', 'consumeMessageEvent', 'topic');
/* in your React.jsx Component */
import AtomicKafkaClient from 'atomic-kafka/client';
/* in your TypeScript React Component */
declare function require(name:string);
const AtomicKafkaClient = require('atomic-kafka/client').default;
function ConsumerComponent() {
const akc = new AtomicKafkaClient('ATOMIC_KAFKA_SERVER_URI_STRING');
const callback = (payload) => {
/* user-provided data stream processing function definition
that effects state change */
}
/* Throttles message consumption. Interval in milliseconds,
can be any number */
akc.useInterval(() => akc.consumer('consumeMessageEvent', callback), 4000);
}
function ProducerComponent() {
const akc = new AtomicKafkaClient('ATOMIC_KAFKA_SERVER_URI_STRING');
const payload = {
/* Data to be sent to the cluster. Arbitrarily-nested JSON format.
Can be defined anywhere in the app. */
}
akc.producer('produceMessageEvent', payload);
}
We want this open-sourced project to continue to improve. If you would like to make a contribution to AtomicKafka, please fork this repo, add your awesome changes to a well-named feature branch of this repository, and make a pull request. We look forward to your input! And if you want to support AtomicKafka, please click on the ⭐ button for us!
Maintainers
Nikhil Massand
This product is released under the MIT License