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

micro-whalla

v0.1.22

Published

A simple, fast framework for writing microservices in Node.js communicate using RPC / IPC

Downloads

32

Readme

Micro Whalla

npm version Build Status Coverage Status Dependency Status Gitter

A simple, fast framework for writing microservices in Node.js communicate using RPC / IPC. It uses Redis server for communicate clients and services, it is making it fast, stable and easily scaling. It can be used for interprocess communication, or for communication between servers.

var micro = require('micro-whalla');
var Client = micro.Client;
var client = new Client('example-service');

client
    .request('method2', {param: new Date()})
    .timeout(1000)
    .send()
    .on('succeeded', function (result) {
        console.log(result);
    })
    .on('progress', function (progress) {
        console.log(result);
    })
    .on('failed', function (error) {
        console.error(result);
    });
var util = require('util');
var micro = require('micro-whalla');
var Service = micro.Service;
var service;

function ExampleService() {
  this.repository = function () {
      return [1, 2, 3];
    };

  Service.call(this, 'example-service');
}

util.inherits(ExampleService, Service);

service = new ExampleService();

service.register('method', function (req, res) {
    res.done(this.repository());
});

service.register('method2', function (req, res) {
  res.progress(50);
  setTimeout(function () {
    res.done(req.data);
  }, 300);
});

service.start();

Features

  • Focus on high performance
  • Concurrent processing
  • Design based on Redis
  • Many methods in the service
  • Interprocess communication (IPC)
  • Servers communication (RPC)
  • Request timeouts
  • Fire And Forget
  • Processing callback events
  • 100% code coverage

Installation

npm install micro-whalla

Tests

To run tests, first install the dependencies, then run gulp test

npm install
gulp test

API Reference

Initialization

Used to configuration for redis. This function should only be called once at the beginning of the application.

var micro = require('micro-whalla');
micro.init({ host: '192.168.99.100', port: 32768 });

The opts fields are:

  • host: string, Redis host.
  • port: number, Redis port.
  • db: number, Redis DB index.

Client

Client(serviceName, opts)

It creates a new Client dedicated for communicate with other Service.

var Client = require('micro-whalla').Client;
var client = new Client('service');

Client.prototype.request(method, data, callback)

Used to create new request to other service. This function return new Request.

var request = client.request('method', { param: new Date() });
  • method - [required] name method which be executed
  • data - [required] params and data for method which be executed
  • callback - [optional] callback of request - executed when client received succeed or failed status

Service

Service(serviceName, opts)

It creates a new Service.

var Service = require('micro-whalla').Service;
var service = new Service('service', {});

The opts fields are:

  • concurrency - (optional) maximum number of simultaneously active jobs for this processor. It defaults to 1

Service.prototype.register(name, handler(req, res))

Used to register method in service before start service.

service.register('method', function (req, res) {
  res.done(req.data);
});

The name of handler should be unique.

The handler function should:

  • call res.done(result) or res.error(error)
  • not block event loop for very long time

The handler function is called with Request and Responseparams.

Service.prototype.start()

Used to start service.

Service.prototype.findAndRegisterMethods(pattern)

Used to find all methods in folders and register them in the service.

  • pattern - [optional] pattern used to find methods. It defaults to /methods/**/*.method.js

All files should exports Method.

var Method = require('micro-whalla').Method;

function getUser(req, res) {
  var self = this;
  var userId = req.data.userId;

  self.usersRepository.findUserById(userId, function (err, user) {
    if (err) {
      return res.error(err);
    }

    return res.done(user);
  });
}

module.exports = new Method('getUser', getUser);

Request

  • id - number, unique id
  • method - string, name of method in remote service
  • data - object, data / params for method
  • options - object, used by framework
  • sent - date, sent date

Request.prototype.timeout(timeout)

Sets a reqeust timeout. After the time has elapsed without a response is returned error.

var request = client.request('method', { param: new Date() }).timeout(1000);

Request.prototype.cache(time)

Sets a reqeust cache time. During this time result is returned from the cache.

var request = client.request('method', { param: new Date() }).cache(1000);

Request.prototype.fireAndForget()

Indication that are not expected to result from the request.

var request = client.request('method', { param: new Date() }).fireAndForget();

Request.prototype.send(callback)

Send request to service.

var request = client.request('method', { param: new Date() }).send();

Request.on(event, listener)

Set listeners for callback events. Default events:

  • succeeded
  • failed
  • info
  • progress
var request = client.request('method', { param: new Date() })
  .on('succeeded', function (result) {
    console.log(result);
  })
  .on('failed', function (error) {
    console.log(error);
  }).send();

Response

Response.prototype.done(result)

Ends the process and returns result for the client.

 res.done({date: new Date()});

Event: succeeded

Response.prototype.error(error)

Ends the process and returns error for the client.

 res.error(new Error(''));

Event: failed

Response.prototype.info(data)

Sends information to client with data.

res.info({ information: '' });

Event: info

Response.prototype.progress(progress)

Sends progress information to client with progress value.

res.progress(50);

Event: progress

Response.prototype.event(name, data, err)

Sends any custom events to client.

res.event('custom', { data: 1 });

License

MIT