klox
v1.7.0
Published
Taking care of the plumbing
Downloads
582
Readme
KLOX
Klox is a basic API wrapper that abstracts the communication layer.
Our usecase is in a node.js microservice architecture where different services communicates with each other without having to implement the code for basic communication every time.
Klox is taking care of the plumbing.
Basic usage
In order to use klox, it needs to be configured.
First, let use require klox and its transports.
const klox = require('klox');
const Transports = require('klox/transports');
Let us define a basic client that is able to send requests.
let client = klox().sends().through(RabbitmqTransport).replies().through(RedisTransport);
We are basically making a klox instance, that is able to send API requests through RabbitMQ and the expect replies through Redis.
In order to make setup the server-side code to respond to the client, we could use the following snippet:
let server = klox()
.receives().through(RabbitmqTransport).replies().through(RedisTransport);
It will receive messages through RabbitMQ and replies to them through RedisTransport.
The following example shows how to set up klox to both send and receive messages.
It is possible to mix sending, receiving and reply transports.
let api = klox()
.receives().through(RabbitmqTransport).replies().through(RedisTransport)
.sends().through(RabbitmqTransport).replies().through(RedisTransport);
If no transport is supplied to the "through" method, then the primary transport (RabbitMQ in this case), will be used as the reply transport.
let api = klox()
.receives().through(RabbitmqTransport).replies().through(RedisTransport)
.sends().through(RabbitmqTransport).replies().through(RedisTransport);
If the "replies" method is not called, klox will ignore listening to and sending replies.
let api = klox()
.receives().through(RabbitmqTransport)
.sends().through(RabbitmqTransport);
Timeouts
Example of server setting a timeout of 50 ms
let server = klox({timeout: 50, requeue_on_timeout: false})
.receives().through(RabbitmqTransport).replies().through(RedisTransport);
By default, timeout is set to "-1", meaning that it is disabled.
requeue_on_timeout is used to tell klox what should happen to a message timing out. By default, it will requeue the payload, meaning that it will end up in a new listen instance somewhere.
Transports
Klox communicates independently across different transport, by internally using a coment set of interface methods.
Out of the box, klox supports the following transports: SocketIO, Redis, RabbitMQ.
They can all be accessed on the common interface below:
const Transports = require('klox/transports');
Redis
let RedisTransport = new Transports.Redis({host: '127.0.0.1', port: 6379});
Env variables
Redis will look for the following environment variables:
| Variable | Default value | | --- | --- | | REDIS_HOST | 127.0.0.1 | | REDIS_PORT | 6379 |
RabbitMQ
let RabbitmqTransport = new Transports.Rabbitmq({host: '127.0.0.1', port: 5672});
Env variables
RabbitMQ will look for the following environment variables:
| Variable | Default value | | --- | --- | | RABBITMQ_HOST | 127.0.0.1 | | RABBITMQ_PORT | 5672 |
SocketIOServer
View "test/socket.js" for an example
const klox = require('klox');
const Transports = require('klox/transports');
let SocketServerTransport = new Transports.SocketIOServer({
port:4004
});
let server = klox().receives().through(SocketServerTransport).replies();
Env variables
The SocketIO server will look for the following environment variables:
| Variable | Default value | | --- | --- | | PORT | 4001 |
SocketIOClient
View "test/socket.js" for an example
const klox = require('klox');
const Transports = require('klox/transports');
let SocketClientTransport = new Transports.SocketIOClient({
hosts: ['http://localhost:4004']
});
let client = klox().sends().through(SocketClientTransport).replies();
Releases
1.1.10
- Removed logging
1.1.9
- Fixing race condition issue in RabbitMQ creating too many connections
1.1.8
- Bugfix
1.1.7
- Bugfix
1.1.6
- Implemented request validation and casting using Horai
1.1.5
- Added option to pass credentials to RabbitMQ
1.1.4
- Fixed potential memory leak in call multiple.
1.1.3
- Fixed error where payload was sent despite requeue set to true.
1.1.2
- Fixed memory leak in call reply.
1.1.1
- Will cleanup internal registry of timeout timeouts.
1.1.0
- Will not reply from listen, if set to requeue and payload has timed out.
1.0.9
- Port option can be passed to SocketIOServer transport
1.0.8
- Option to add timeouts on listen added. Klox accepts "timeout" and "requeue_on_timeout" options.
1.0.7
- Bugfix, will no longer attempt to reply if no reply_on property exists on payload
1.0.6
- Call accepts a fourth argument, a callback which is triggered whenever the payload is ready (For backward compatibility)
1.0.5
- Only expect reply in call, if callback is supplied. Will prevent unneeded listen on reply endpoint
1.0.4
- Verbose option added
- RabbitMQ transport refactor
1.0.3
- Type bugfix
1.0.2
- RabbitMQ are by default non-durable
1.0.1
- Call multiple added
1.0.0
- Initial release