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

@opuscapita/event-client

v2.4.0

Published

An event client to handle the event system in microservices architecture.

Downloads

1,725

Readme

EventClient

This module provides simplified access to the publish/subscribe system provided by a Message Queue server RabbitMQ. It uses Consul in order to determine the required server endpoint and further configuration. To have a look at the full API, please visit the related wiki page or github pages.

Branches and release tags

Library is published to npmjs repository under the tags:

  • latest - stable code corresponding to master git branch
  • next - possibly unstable code released from develop git branch - it could change and you should NOT rely on its features

There is a deprecated branch 3x which originally was meant as the next upcoming release that combine both kafka and rabbitMQ support. It was abandonned long time ago and separate module @opuscapita/kafka was released instead.

During nightly build the npm package shouldn't be published - nightly is only to confirm that everything works fine

Possible future of this project

Once it is revived, it might be at some point used again (after many fixes applied) and finally combined into a single repository together with other @opuscapita libraries.

Original documentation (possibly outdated) starts bellow

Minimum setup

First got to your local code directory and run:

npm install @opuscapita/event-client

To go with the minimum setup, you need to have access to a running Consul server to get your endpoint configuration for Message Queue server. In addition, a Message Queue server is required which has to be registered inside Consul. If Message Queue password authentication is required, Consul has to provide the configuration key {{your-service-name}}/mq/password where {{your-service-name}} is the least name of the directory your code runs in. If authentication is not used, you can set the consul.mqPasswordKey to null or false when creating a new instance of EventClient.

The basic implementation requirements define, that the format of an event's name has to be serviceName.domainName.eventType.eventDetail where serviceName defines the name of the exchange used. If the exchange does not exist, an error will be thrown by the subscribe() method. Therefor please make sure, the exchange exists by either creating it manually or bringing up the service that raises the event as the emit() method will create the exchange for the service it is called in.

Common subscription

If all this is set up, go to you code and add the following lines:

const EventClient = require('@opuscapita/event-client');

(async () =>
{
    const events = new EventClient({ consul : { host : '{{your-consul-host}}' } });

    // Subscribe to a channel by name.
    await events.subscribe('my-service.my-channel', console.log);
    await events.emit('my-service.my-channel', 'Hello, world!');

    // - OR -
    // Subscribe to a channel by pattern.
    await events.subscribe('my-service.my-channel.#', console.log);
    await events.emit('my-service.my-channel.sub-channel', 'Hello, world!');

    // unsubscribe from a particular key
    await events.unsubscribe('my-service.my-channel');

    // unsubscribe from a pattern
    await events.unsubscribe('my-service.my-channel.#');

    await events.dispose();
})();

Fetching single message

EventClient is also capable of fetching single events (messages) from a queue. This is done by subscribing to a queue without a callback and then fetching messages manually one by one.

const EventClient = require('@opuscapita/event-client');

(async () =>
{
    const events = new EventClient();

    await events.subscribe('my-service.my-channel');
    await events.emit('my-service.my-channel', 'Hello, world!');

    // With automatic acknowledgement.
    let message = await events.getMessage('my-service.my-channel');

    console.log(message);


    await events.emit('my-service.my-channel', 'Hello, world!');

    // With manual acknowledgement.
    message = await events.getMessage('my-service.my-channel', false);
    await events.ackMessage(message);

    console.log(message);

    await events.dispose();
})();

Default configuration

The default configuration object provides hints about what the module's standard behavior is like.

{
    serializer : JSON.stringify,
    parser : JSON.parse,
    serializerContentType : 'application/json',
    parserContentType : 'application/json',
    queueName : null,
    exchangeName : null,
    consul : {
        host : 'consul',
        mqServiceName  : 'rabbitmq-amqp',
        mqUserKey: 'mq/user',
        mqPasswordKey : 'mq/password'
    },
    consulOverride : {
        host : null,
        port : null,
        username : null,
        password : null
    },
    context : {
    }
}

Testing

local docker-compose comes with a two node rabbitmq cluster that can be used to test cluster node failure conditions. To bring down a node curl it on port 6666 for rabbit1 and 6667 for rabbit2 which will take them down...