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

redis-pubsub-futures

v1.2.2

Published

A node-redis wrapper to implement an RPC client for redis using Futures written in Typescript

Downloads

21

Readme

redis-pubsub-futures

This is a small wraper for node-redis to implement an RPC client using Redis pub/sub capabilities and Fluture's monadic Futures.

Installation

npm install redis-pubsub-futures

#or

yarn add redis-pubsub-futures

Usage

Using the rpcClientBuilder:

Build a client with a curried functionalesqe style.

import { rpcClientBuilder } from 'redis-pubsub-futures';

const clientConfig = { serviceChannel: 'SERVICE_CHANNEL' };
const rpcOptions = { timeout: 500 };

const sendRpcCall = 
    rpcClientBuilder(clientConfig)(rpcOptions)(console.error, console.log);

// The call will return a cancel function
const sendRpcCall = sendRpcCall({ type: 'PING' });

Using the rpcClientFactory:

Pass an object, get a disposable client.

import { rpcClientFactory } from 'redis-pubsub-futures';

const sendRpcCall = rpcClientFactory({
    options,
    clientConfig,
    onError,
    onSuccess,
});

// calling the client returns a cancel function
const cancel = sendRpcCall({ type: 'PING', payload: 'yolo' });

Using the rpcPromiseBuilder:

In case you hate the Future or you simply are more into Promises:

import { rpcPromiseBuilder } from 'redis-pubsub-futures';

const clientConfig = { serviceChannel: 'SERVICE_CHANNEL' };
const rpcOptions = { timeout: 500 };

// no way to cancel this one
const rpcPromiseCaller = rpcPromiseBuilder(clientConfig)(rpcOptions);

rpcPromiseCaller({ type: 'PING', payload: 'yolo' })
    .then(console.log)
    .catch(console.error);

Configuration

Client Config

For the client, the only required parameter is serviceChannel which should be a string to use as the redis pub/sub key, everything goes with the defaults unless you specify otherwise.

You can pass options directly to the node-redis client on the clientOptions property to define a host and password for redis among other options, please check their documentation here.

Connection pattern example:

interface ClientConfig {
  serviceChannel: string;
  clientOptions?: redis.ClientOpts;
}

// example
const clientConfig = {
    serviceChannel: 'mySuperChannel',
    clientOptions: {
        url: 'redis://bob:[email protected]:6380',
    }
};

const redisConnection = rpcClientBuilder(clientConfig);

Rpc Options

Timeout

Timeout the RPC after a number of ms the default value is 120 * 1000

JSON parsing

By default the client will return the response in string form, you can use the basic included JSON parser to attempt to cast the answer to a JSON object by including a json key with value of true in the options:

Dispossable client pattern example:

interface RcpFactoryConfig extends ClientConfig {
  options: RpcSendOptions;
  onError: RejectFunction<unknown>;
  onSuccess: ResolveFunction<unknown>;
}

const clientOptions = { ... };
const rpcOptions = { 
    options: {
        json: true,
        timeout: 5000
    },
    onSuccess: console.log,
    onError: console.error,
};

rpcClientFactory({...clientOptions, rpcOptions });

Lower level fluture Futures API:

You can also access the low level fluture Future created by the factory and builder by importing rpcSend:

import { fork } from 'fluture';
import { rpcSend } from 'redis-pubsub-futures';

const rpcFuture = 
    rpcSend('SERVICE_CHANNEL')({ timeout: 500, json: true })({ type: 'PING' });

rpcFuture.pipe(fork(console.error)(console.log));

Contributing

Pull requests are welcome. For major changes, please open an issue first to discuss what you would like to change.

Please make sure to update tests as appropriate.

License

MIT