Giter Site home page Giter Site logo

benvp / graphql-kafkajs-subscriptions Goto Github PK

View Code? Open in Web Editor NEW

This project forked from tomasalabes/graphql-kafkajs-subscriptions

0.0 0.0 0.0 774 KB

Apollo graphql subscriptions using kafkajs

License: MIT License

Shell 1.13% JavaScript 0.52% TypeScript 98.36%

graphql-kafkajs-subscriptions's Introduction

graphql-kafkajs-subscriptions

Apollo graphql subscriptions over Kafka, using kafkajs. Inspired on graphql-kafka-subscriptions.

Communication is done through 1 kafka topic specified in the KafkaPubSub create function. Then channels are used to identify the right subscription.

Installation

  npm install graphql-kafkajs-subscriptions
  yarn add graphql-kafkajs-subscriptions

Usage

Kafka Pub Sub

import { Kafka } from 'kafkajs';
import { KafkaPubSub } from 'graphql-kafkajs-subscriptions'

export const pubsub = KafkaPubSub.create({
  topic: 'my-topic',
  kafka: new Kafka({/* ... */})
  groupIdPrefix: "my-group-id-prefix", // used for kafka pub/sub,
  producerConfig: {}, // optional kafkajs producer configuration
  consumerConfig: {} // optional kafkajs consumer configuration
})

Subscription Resolver

{
    collaboration: {
      resolve: (payload: KafkaMessage) => {
        // payload.value will be whatever you sent
        return payload.value;
      },
      subscribe: (_, args) => {
        return pubsub.asyncIterator<YourType>("my channel");
      }
    }
  };

You can also use the subscription payload for the channel.

{
    collaboration: {
      resolve: (payload: KafkaMessage) => {
        // what you publish will end up passing through here and to the client
        return payload.value;
      },
      subscribe: (_, args) => {
        // this is called from the client
        return pubsub.asyncIterator<YourType>(`channel-${args.myId}`);
      }
    }
  };

Publication

Somewhere in your code, you call this:

pubsub.publish("my channel", {
  /* your event data */
});

Use the rest of the kafkajs options:

const event = {/* ... */};
const headers = {
  header1: "value"
};
const producerOptions = { /* options from kafka.js.org/docs/producing: acks, timeout, etc */ };

pubsub.publish("my channel", event, headers, producerOptions);

This ends up publishing the event to kafka (to the topic you used to create the kafkaPubSub) and received by all consumers. The consumer which is listening to my channel will send it to the client.

graphql-kafkajs-subscriptions's People

Contributors

ancashoria avatar benvp avatar ddeath avatar dependabot[bot] avatar github-actions[bot] avatar sertac-scotty avatar sgil-carecloud avatar tbrannam avatar tomasalabes avatar

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.