npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

blockbid-message

v6.0.1

Published

This library makes it easy to send messages in a distributed network transparent way via various brokers but initially via RabbitMQ.

Downloads

2

Readme

RxJS wrapper for messaging systems

This library makes it easy to send messages in a distributed network transparent way via various brokers but initially via RabbitMQ.

Roadmap

At a later point we should have plugins to make it work with various messaging paradigms:

  • [x] AMQP
  • [ ] Kafka
  • [ ] Communicate locally between threads / workers
  • [ ] socket.io (browser/server)

Principles:

  • Declarative over imperative.
  • Functions over classes.
  • Simplicity over complexity.
  • Immutable over mutable.
  • Flexible and composable over fixed heirarchy.
  • Pure over impure.
  • Minmalistic sensible defaults over boilerplate.
  • Idiomatic API's over reinventing the wheel.

Environments

  • Basic framework should work in all V8 environments. eg.
  • Middleware might be environment specific. Eg. blockbid-messages/amqp requires node. blockbid-messages/socketio-browser may require browser objects. (YTBI)

TODO

  • [x] Mock out tests properly
  • [x] Export proper typescript types
  • [x] Revisit blockbid-tools and ensure it supports versioning
  • [x] Implement connection resillience
  • [x] Fix concurrent connection issues
  • [x] Guard for configuration shape
  • [ ] Write docs on AMQP middleware

Installation

You can install by referencing a version tag directly off the github repo.

yarn add blockbid/blockbid-message#<semverish>

Framework Usage

import { createConsumer, createProducer } from 'blockbid-message';
import { filter } from 'rxjs/opeerators';

// createProducer accepts a list of middleware
// the message passes top down
// It returns an RxJS Observer that sends messages
const producer = createProducer(
  transformMessageSomehow,
  broadCastsMessagesSomewhere
);

// createConsumer also accepts a list of middleware
// the message also passes top down
// It returns an RxJS Observable that will receive the message
const consumer = createConsumer(
  receivesMessagesFromSomewhere,
  logOrTransformMessage,
  doSomeMoreTransformation
);

// Use RxJS's Observable#next() method to send a message
producer.next({
  content: 'Hello World!',
  route: 'hello'
});

// Note that because consumer is simply an RxJS observable
// you can apply filtering and throttling or do whatever you want to it
const sub = consumer
  .pipe(filter(msg => msg.content.toLowerCase().includes('world')))
  .subscribe(msg => {
    console.log(`Received: ${msg.content}`);
  });

Typescript types

Messages

Generic message objects look like this:

// Generic message
export interface IMessage {
  content: any;
  route?: any;
}

You might use a message by sending it to the next() method of a producer.

producer.next({
  content: 'Hi there!',
  route: 'some-queue'
});

Middleware

Middleware are effectively functions designed to decorate RxJS streams and looks like this:

// Generic Middleware decorates a stream
export type Middleware<T extends IMessage> = (
  a: Observable<T>
) => Observable<T>;

You might use a middleware by passing it as one of the arguments to the createProducer() or createConsumer() functions

import {tap} from 'rxjs/operators';

function logger(stream: Observable<IMessage>) {
  return stream
    .pipe(tap(
      (msg:IMessage) => console.log(`Stream logged: ${msg.content}`
    ));
}

// Pass the middleware in order to the consumer or producer
const consumer = createConsumer(someReceiver, logger);

AMQP Middleware

AMQP Middleware is designed to work in Node environments only due to limitations with the amqplib package it is based on.

Basic Usecase with amqp middleware

import { createConsumer, createProducer } from 'blockbid-message';

import { createAmqpConnector } from 'blockbid-message/amqp';

const { sender, receiver } = createAmqpConnector({
  declarations: {
    // This declares the queue you want to use
    queues: [
      {
        durable: false,
        name: 'hello'
      }
    ]
  },
  uri: 'amqp://user:[email protected]/user'
});

// Here is an RxJS Observer that sends the message
const producer = createProducer(sender());

producer.next({
  content: 'Hello World!',
  route: 'hello'
});

// Here is an RxJS Observable that will receive the message
const consumer = createConsumer(
  receiver({
    noAck: true,
    queue: 'hello'
  })
);

const sub = consumer.subscribe(msg => {
  console.log(`Received: ${msg.content}`);
});

sub.unsubscribe();

Example Usage AMQP Middleware

For usage and examples please look at the basic tests thrown together here

  1. Hello World
  2. Work Queues
  3. PubSub
  4. Routing
  5. Topics

RxJS References

Docs

Videos

NOTE: Using version 6

blockbid-message uses RxJS v6.0 so you need to pipe all your operators:

import { filter } from 'rxjs/operators';

// ...

consumer.pipe(filter(forUserEvents(userId))).subscribe(
  msg => {
    dealWithMessage(msg.content);
  },
  () => {}
);

Other References

  • https://www.rabbitmq.com/tutorials/tutorial-one-javascript.html
  • https://www.rabbitmq.com/tutorials/tutorial-two-javascript.html
  • https://www.rabbitmq.com/tutorials/tutorial-three-javascript.html
  • https://www.rabbitmq.com/tutorials/tutorial-four-javascript.html
  • https://www.rabbitmq.com/tutorials/tutorial-five-javascript.html
  • https://aws.amazon.com/blogs/compute/building-scalable-applications-and-microservices-adding-messaging-to-your-toolbox/