goodly
v0.4.1
Published
Microservice framework
Downloads
22
Maintainers
Readme
goodly
Goodly is an unopininated microservice communication framework. It manages the low-level queing so you can focus on the important stuff: handling events.
Goodly has several design goals:
- Create a simple programming interface for event-based microservices.
- Support scale-out of individual services -> add more instances of the service and work will automatically be distributed between the instances.
- Support addition of new services -> add as many services as you need.
- Supports large-event transmission between services -> send data through the broker, http, tcp, udp, protocol buffers, etc.
Goodly doesn't tell you the best way to structure you app, it gives you a set of tools and lets you build on-top of RabbitMQ. Goodly manages the AMQP exchange creation, queue creation, and messaging routing creation. It does all this to provide a simple interface for building your application that will automatically scale.
Made for coding
How easy it is to use? Create a service in a few lines of code...
import goodly from 'goodly';
const service = goodly({ name: 'documents' });
// start the service
service.start({ brokerPath: 'ampq://192.168.99.100' });
// listen for an event and do something
service.on('document.uploaded', async ({ data, emit }) => {
// do something with the data
let document = await saveDocumentAsync(data);
// emit another event
emit('document.available', document);
});
With the above code, you could start a single instance or 1000 instances. The work will be distributed between your instances automatically.
API
Service Methods
Promise start({ brokerPath, concurrent)
Starts the service and if necessary creates all exchange and queues owned by the service. Once start is complete, the service will immeidately begin pulling work from the service queue.
Parameters:
- string brokerPath - host of RabbitMQ
- int concurrent - the number of open messages that will be concurrent consumed
Promise stop()
stops the service and disconnects from RabbitMQ
Promise emit(path, data, options)
Emits a message with the specified path and data. Emit does not expect a response and is equivalent to pub/sub actions.
Paramters:
- string path - name of the event to send
- any data - the data that will be send that could be a buffer, string, int, float, bool, array, object, or error
Promise request(path, data)
Makes a request with the specified path and data. Request will block until a response is received.
Paramters:
- string path - name of the event to send
- any data - the data that will be send that could be a buffer, string, int, float, bool, array, object, or error
Promise on(path, fn1, fn2, ...)
Adds a handler to the service for supplied path. The supplied functions will be executed in order of attachment.
Parameters:
- string path - name of the event to listen for
- fn handler - a handler for the event
Handler
Handlers are called with an Event
object. The Event
object contains several methods and pieces of data:
Properties:
- any data - the data that will be send that could be a buffer, string, int, float, bool, array, object, or error
- correlationId - the UUID mapping to a unique origin event
- msg - the raw RabbitMQ message
Methods:
- emit - calls the emit method on the service but uses the supplied correlationId for the incoming event
- reply - replys to a message that is a Request/Response message