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

microservice-redis-net

v2.1.5

Published

This package helps services communicate with each other using redis

Downloads

10

Readme

microservice-redis-net

npm version

USAGE

A simple wrapper around redis pub/sub to allow for easy microservice communication.

To autogenerate code from services install the cli tool

$ npm install mrn-cli

1. Install

$ npm install microservice-redis-net
$ yarn add microservice-redis-net

2. Configuration

With the cli tool you can run the follow command on the terminal to generate a config file.

$ mrn init --application <name>

edit the redis configuration to point to your redis server

2. Import and create Service

Import the createService function and call it to create a service To make use of the decorators, you'll need to provide a dependency manager like typdi in the createService function

import {createService} from 'microservice-redis-net';
import Container from 'typedi';

// example
const exampleService = createService (Container);

You can also pass in a config object if the config file is not in the root directory

import {createService} from 'microservice-redis-net';
import Container from 'typedi'

// example
const exampleService = createService (Container, {
    application: "example",
    service: "example-service",
    broker: 'bull' | 'rabbitmq',
    queue: {
        server: {
            host: "localhost",
            port: 6379
        }
    }
});

Bind the decorators to your class methods

import {createService, registerFunction, subscribeFunction} from 'microservice-redis-net';

createService ();

class ExampleService {

    @registerFunction
    async exampleFunction (id: string, name: string, created: boolean){
        console.log('recieved job', id, name, created);
        return {status: "ok"};
    }

    @subscribeFunction
    async exampleSubscribe (args){
        console.log('recieved job', args);
        return {status: "ok"};
    }
}

3. Invoke functions

using mrn-cli run the following command to generate a client

$ npx mrn pull

Import the client and invoke the function


import example, {exampleSubscribe} from './mrn-application/example';

// call service function
exmple.exampleFunction({data: "example data"})
    .then((result) => {
        console.log('result', result);
    })
    .catch((err) => {
        console.log('error', err);
    });

// can as well make a custom call
import { Service } from 'microservice-redis-net';
Service.instance.call(exampleSubscribe, {data: "example data"});

// Can also invoke with the type function exampleSubscribe
// invoke the event Subscribed
Service.instance.Invoke(exampleSubscribe, {data: "example data"});

// or subscribe via the type function exampleSubscribe
Service.instance.hook(exampleSubscribe, (args)=>{
    console.log('recieved job', args);
    return {status: "ok"};
});

Using the types generated from the cli tool gives you access to parameters for the functions

You can also specify an event from the auto generated code in the subscribe function

class ExampleService {

    @subscribeFunction(exampleSubscribe)
    async hookEVent(args: any){
        console.log('recieved job', job);
        return {status: "ok"};
    }
}

Other Features

If you're not making use of decoratorsm, the following apis are also available to you

import {createService} from 'microservice-redis-net';

const service = createService ();

service.registerFunction(async function exampleFunction(id:string, user:any)=>{
    console.log('recieved job', job);
    return {status: "ok"};
});
// register a function with a route
service.registerHandler('route', async (args)=>{
    console.log('recieved job', args;
    return {status: "ok"};
});
// subscribe to an event or function
service.subscribe('event_route', async (args)=>{
    console.log('recieved job', args);
    return {status: "ok"};
});
//send a job to the service
await service.send('service', 'route', {data: "exampledata"});
//invoke event
await service.invokeEvent('event_route', {data: "exampledata"});

NOTE

  • This package is still in beta and not very stable for production environment, it might undergo few changes in future iterations

  • If you're using typescript, you need to enable the experimentalDecorators and emitDecoratorMetadata compiler options in your tsconfig.json file.

  • Run mrn init to generate a config file so you can manage your configuration in one place

  • Run mrn pull to generate a client file to invoke functions

  • To make use of the decorators efficiently, you'll need to use a dependency injection manager, this package supports typedi