npm package: @salo/graphql-amqp-subscriptions
Description: GraphQL AMQP Subscriptions
Author: https://github.com/Surnet/graphql-amqp-subscriptions/graphs/contributors
Latest version: 1.1.3

# graphql-amqp-subscriptions

This package implements the PubSubEngine Interface from the [graphql-subscriptions](https://github.com/apollographql/graphql-subscriptions) package.
It allows you to connect your subscriptions manager to a AMQP PubSub mechanism.

This package is influenced by [graphql-redis-subscriptions](https://github.com/davidyaha/graphql-redis-subscriptions) and [graphql-rabbitmq-subscriptions](https://github.com/cdmbase/graphql-rabbitmq-subscriptions).

[![npm Version](https://img.shields.io/npm/v/@salo/graphql-amqp-subscriptions.svg)](https://www.npmjs.com/package/@salo/graphql-amqp-subscriptions)
[![npm Downloads](https://img.shields.io/npm/dm/@salo/graphql-amqp-subscriptions.svg)](https://www.npmjs.com/package/@salo/graphql-amqp-subscriptions)
[![Known Vulnerabilities](https://snyk.io/test/github/salo-creative/graphql-amqp-subscriptions/badge.svg)](https://snyk.io/test/github/salo-creative/graphql-amqp-subscriptions)

## Installation

### npm

`npm i @salo/graphql-amqp-subscriptions`

### yarn

`yarn add @salo/graphql-amqp-subscriptions`

## Usage

### Basic

```javascript
import { AMQPPubSub } from '@salo/graphql-amqp-subscriptions';
import amqp from 'amqplib';

amqp.connect('amqp://guest:guest@localhost:5672?heartbeat=30')
.then(conn => {
  const pubsub = new AMQPPubSub({
    connection: conn
    /* exchange: 'graphql_subscriptions' */
  });
  // Use the pubsub instance from here on
})
.catch(err => {
  console.error(err);
});
```

### Example

```javascript
import { AMQPPubSub } from '@salo/graphql-amqp-subscriptions';
import amqp from 'amqplib';

const queue = [];

// eslint-disable-next-line import/no-mutable-exports
export let pubsub = {
  publish: (key, content) => {
    queue.push({ key, content });
    console.log('🐰 RabbitMQ connection not ready yet');
  },
  asyncIterator: () => console.log('🐰 RabbitMQ connection not ready yet')
};

amqp
  .connect(`amqp://${ user }:${ password }@${ host }:${ port }?heartbeat=30`)
  .then((connection) => {
    pubsub = new AMQPPubSub({
      connection,
      exchange: 'main',
      exchangeType: 'direct',
      queueName: 'queuey mcqueueface',
      queueOptions: {
        durable: true,
        passive: false,
        exclusive: false,
        autoDelete: false
      }
    });

    console.log('🐰 RabbitMQ ready to hop');
    
    // Go through items queued while waiting
    queue.forEach(({ key, content }) => {
      pubsub.publish(key, content);
    });
    if (queue.length) {
      console.log(`🐰 Processed ${ queue.length } queued messages`);
      queue.length = 0;
    }
  })
  .catch((error) => {
    console.error('🐰 Error connecting to RabbitMQ:', error);
  });

```

## Benefits

* Reusing existing [amqplib](https://github.com/squaremo/amqp.node) Connection.
* Reusing channels (one for subscriptions, one for publishing).
* Performance/Ressource-usage benefits on AMQP (RabbitMQ) because of the aforementioned reasons [more info](https://www.cloudamqp.com/blog/2018-01-19-part4-rabbitmq-13-common-errors.html).
* Using Topic Exchange (e.g. you publish to `agreements.eu.berlin.headstore` and subscribe to `agreements.eu.#`) [more info](https://www.cloudamqp.com/blog/2015-09-03-part4-rabbitmq-for-beginners-exchanges-routing-keys-bindings.html).

### What the fork?

On top of the original repo this fork adds:

* More lenient engine definition in package.json (able to be used in Node >= 10).
* Additional configuration that you can pass to a queue and exchange.
* Upstream bugfixes.


## Debug

This package uses Debug.

To show the logs run your app with the environment variable `DEBUG=AMQPPubSub`