A RabbitMQ client for TypeScript, with functional programming in mind.

Overview

Banner

RabbitMQ-fp

Lets feed our Rabbit' nicely 🐰

License GitHub Issues GitHub Stars

This repository contains a wrapper over amqplib written in Typescript with an accent of functionnal programming, using fp-ts. It will handle high-level features like RPC without hassle.

Warning This library is still heavily being worked on, but no breaking changes on the API are planned.

Feature highlights:

  • Built-in RPC support
  • Functional typings with fp-ts
  • Automatic error recovery using amqp-connection-manager

Getting started

Install the package from npm:

npm install --save-exact @mansagroup/rabbitmq-fp

This library has fp-ts as peer dependency, to match your project's version.

Create a setup function

This library support automatic recovery on AMQP connection or channel error. However, a newly created channel will not inherit the configuration from the previous one. This means that each new channel must be reconfigured (asserting exchanges, queues, binding queues, etc...).

To solve this, when creating an adapter, you must pass a setup function which takes the created channel and returns this same channel. This can easily represented as a fp-ts's flow method:

import { flow } from 'fp-ts/function';
import * as TE from 'fp-ts/TaskEither';
import { SetupFn } from '@mansagroup/rabbitmq-fp';

const setupFn: SetupFn.Fn = flow(
  SetupFn.assertExchange('my-exchange'),
  SetupFn.assertQueue('my-queue'),
  SetupFn.bindQueue('my-queue', 'my-exchange', 'my-routing-key'),
);

This function will be invoked every time a new channel is created.

Create an adapter

An adapter is the actual brain of this library. It is the high-level bridge between your code and the underlying amqplib library. It will requires your previously created setup function but also a logger:

import { Logger, createRabbitMQAdapter } from '@mansagroup/rabbitmq-fp';

const logger: Logger = {
  info: (msg, extra) => {},
  // This for every log level
};

const adapter = await createRabbitMQAdapter(
  'amqp://username:password@host:port',
  setupFn,
  {
    logger,
  },
)();

Create your consumer

To keep this simple, we will setup a simple event consumer which will print hello {greetings} every time a message is published. A consumer is a function which takes a payload and returns a TaskEither.

Note If the TaskEither is a Left, then the message will be nack, otherwise it will be ack.

import { EventHandler } from '@mansagroup/rabbitmq-fp';
import { pipe } from 'fp-ts/function';
import * as IO from 'fp-ts/IO';

interface Payload {
  greetings: string;
}

const consumer: EventHandler<Payload> = (payload) =>
  pipe(
    `hello ${payload.greetings}`,
    IO.of,
    IO.map(console.log),
    IO.map(TE.right),
  )();

await adapter.consumeEvent('my-queue', consumer)();

Publish your message

Finally, after that your consumer is created and ready, you can publish your first message to see the consumer invoked:

await adapter.publish<Payload>('my-exchange', 'my-routing-key', {
  greetings: 'Bob',
})();

Everything together

Now, if we pull everything together, we could have a flow like the one from the everything-together.ts example.

Examples

License

This project is MIT licensed.

Contributors

Thanks goes to these wonderful people (emoji key):


Jérémy Levilain

💻 📖 🤔

This project follows the all-contributors specification. Contributions of any kind welcome!

Comments
Releases(v0.0.6)
  • v0.0.6(Jul 12, 2022)

    • chore: add node 18 engine 4d56494
    • Update dependency eslint to v8.19.0 (#38) aed9cc6
    • Update jest monorepo (#37) 48e33d3
    • Update commitlint monorepo to v17.0.3 (#34) e0b7645
    • Update dependency testcontainers to v8.11.0 (#33) 1cca04a
    • Update dependency lint-staged to v13.0.3 (#32) adbf601
    • Update dependency np to v7.6.2 (#31) fa3c86f
    • Update dependency @types/jest to v28.1.3 (#30) 53371bd
    • Update dependency eslint to v8.18.0 (#29) 7440c5d
    • Update dependency typescript to v4.7.4 (#28) 4061448
    • Update dependency @types/jest to v28.1.2 (#27) 2f7756b
    • Update dependency lint-staged to v13.0.2 (#26) 501a95b
    • Update dependency prettier to v2.7.1 (#25) 9e7ce55
    • Update dependency ts-jest to v28.0.5 (#24) 04d7311

    https://github.com/MansaGroup/rabbitmq-fp/compare/v0.0.5...v0.0.6

    Source code(tar.gz)
    Source code(zip)
  • v0.0.5(Jun 14, 2022)

  • v0.0.3(Jun 9, 2022)

    • fix: configure swc to export commonjs 7b8c927
    • Update dependency @swc/core to v1.2.197 (#19) 5c59d7a
    • Update dependency @types/jest to v28 (#18) 6a64bbc
    • Update dependency eslint to v8.17.0 (#17) 69d5239
    • Update dependency typescript to v4.7.3 (#16) 298208a
    • Update dependency ts-jest to v28.0.4 (#15) f13ea8b
    • Update dependency amqplib to v0.10.0 (#14) 2ab4555
    • Update dependency @types/jest to v27.5.2 (#13) 5313618
    • Update dependency lint-staged to v13 (#12) bc01b20
    • Update commitlint monorepo to v17.0.2 (#11) b96e78e
    • Update dependency lint-staged to v12.5.0 (#10) cd6ec0d
    • Update actions/setup-node action to v3 (#7) 87a7607
    • Update dependency @swc/core to v1.2.196 (#9) 1edbe72
    • Update typescript-eslint monorepo to v5.27.0 (#8) 49450f8
    • docs: add rpc example 2a4b5d8
    • chore: fix package name 811984e
    • chore: add LICENSE and CODE OF CONDUCT c1cb472

    https://github.com/MansaGroup/rabbitmq-fp/compare/v0.0.2...v0.0.3

    Source code(tar.gz)
    Source code(zip)
  • v0.0.2(May 30, 2022)

    • Update actions/checkout action to v3 (#3) 8b9f837
    • Pin dependency np to 7.6.1 (#4) a4982ae
    • chore: lighten peer dependency on fp-ts (#5) aae9064
    • ci: fix workflow event 5a042e4
    • ci: only run sonarcloud on default node version 22df0e9
    • ci: fix workflow b40261c

    https://github.com/MansaGroup/rabbitmq-fp/compare/v0.0.1...v0.0.2

    Source code(tar.gz)
    Source code(zip)
  • v0.0.1(May 30, 2022)

Owner
MansaGroup
MansaGroup
NestJS implementation of client and strategy hasn't enough features for work with RabbitMQ so i developed this one (basically, just a wrapper for amqp-connection-manager)

NestJS RabbitMQ Client and strategy NestJS implementation of client and strategy hasn't enough features for work with RabbitMQ so i developed this one

ilink 5 Sep 6, 2022
Grupprojekt för kurserna 'Javascript med Ramverk' och 'Agil Utveckling'

JavaScript-med-Ramverk-Laboration-3 Grupprojektet för kurserna Javascript med Ramverk och Agil Utveckling. Utvecklingsguide För information om hur utv

Svante Jonsson IT-Högskolan 3 May 18, 2022
Hemsida för personer i Sverige som kan och vill erbjuda boende till människor på flykt

Getting Started with Create React App This project was bootstrapped with Create React App. Available Scripts In the project directory, you can run: np

null 4 May 3, 2022
Kurs-repo för kursen Webbserver och Databaser

Webbserver och databaser This repository is meant for CME students to access exercises and codealongs that happen throughout the course. I hope you wi

null 14 Jan 3, 2023
Typescript library for functional programming.

Sa Lambda Typescript library for functional programming. Document TODO Either Maybe Iterator Pipe & Flow Task (Promise-Like) some math utils Installat

SoraLib 9 Dec 6, 2022
A template repo that contains a NodeJS app that will consume messages from a RabbitMQ queue and immediately send them to an Azure EventHub.

README.md Summary This repo (RabbitMQ to EventHub Shovel) is a template that contains a NodeJS app that will consume messages from a RabbitMQ queue an

Valtech San Diego 6 Jul 2, 2022
Queue is a node.js package to create background jobs in topic-based RabbitMQ exchanges and process them later.

Queue PLG Works Queue helps with managing subscription and publish critical events using RabbitMQ. All events are published through RabbitMQ, using to

PLG Works 23 Sep 21, 2022
an open-source package to make it easy and simple to work with RabbitMQ's RPC ( Remote Procedure Call )

RabbitMQ Easy RPC (Remote Procedure Call ) The Node.js's RabbitMQ Easy RPC Library rabbitmq-easy-RPC is an easy to use npm package for rabbitMQ's RPC

Ali Amjad 4 Sep 22, 2022
Jargon from the functional programming world in simple terms!

Functional Programming Jargon Functional programming (FP) provides many advantages, and its popularity has been increasing as a result. However, each

hemanth.hm 18.1k Jan 4, 2023
Fun λ functional programming in JS

fp-js JavaScript Functional Programming Motivation This purposed for learning functional programming (just that). Features Auto-Curry Option Tooling s

RiN 6 Feb 4, 2022
Functional Programming with NestJS, Prisma. immutable, pure, stateless

Functional-NestJS Functional Programming with NestJS, Prisma. immutable, pure, stateless. 1. Introduction A production ready typescript backend reposi

y0on2q 40 Dec 6, 2022
Collection of benchmarks of functional programming languages and proof assistants.

Functional Benchmarks This repository contains a collection of benchmarks of functional programming languages and proof assistants. It is split in two

null 22 Dec 12, 2022
When a person that doesn't know how to create a programming language tries to create a programming language

Kochanowski Online Spróbuj Kochanowskiego bez konfiguracji projektu! https://mmusielik.xyz/projects/kochanowski Instalacja Stwórz nowy projekt przez n

Maciej Musielik 18 Dec 4, 2022
Cookbook Method is the process of learning a programming language by building up a repository of small programs that implement specific programming concepts.

CookBook - Hacktoberfest Find the book you want to read next! PRESENTED BY What is CookBook? A cookbook in the programming context is collection of ti

GDSC-NITH 16 Nov 17, 2022
Change the color of an image to a specific color you have in mind.

image-recolor Run it: https://image-recolor.vercel.app/ image.recolor.mov Acknowledgments Daniel Büchele for the algorithm: https://twitter.com/daniel

Christopher Chedeau 21 Oct 25, 2022
Secure-electron-template - The best way to build Electron apps with security in mind.

secure-electron-template A current electron app template with the most popular frameworks, designed and built with security in mind. (If you are curio

reZach 1.4k Dec 29, 2022
A one-of-a-kind resume builder that keeps your privacy in mind. Completely secure, customizable, portable, open-source and free forever. Try it out today!

A free and open source resume builder. Go to App What is this app all about? Reactive Resume is a free and open source resume builder that’s built to

Reactive Resume 9.7k Jan 3, 2023
💬 A Twitch (BTTV and FFZ) emotes and badges parser - built with tmi.js in mind

?? A Twitch (BTTV and FFZ) emotes and badges parser - built with tmi.js in mind

Lucas Fernandes 8 Sep 2, 2022