Home Reference Source Repository
import {Microwork} from 'microwork/src/microwork.js'
public class | source

Microwork

Core Microwork class that provides a way to create new microservice

Constructor Summary

Public Constructor
public

constructor(opts: object): void

Microwork class construct

Member Summary

Public Members
public

RabbitMQ exchange name

public

RabbitMQ host address

public

Service unique ID

public

Reconnect timeout timer stored for later usage

public

Active route handlers and queues

Method Summary

Public Methods
public

registerPlugin(plugin: Object): void

Register new Microwork plugin

public

send(topic: string, data: Any, opts: Object): Promise

Send given data to the specified topic

public

Stops the service, closes all workers/subscriptions and terminates the connection to RabbitMQ

public

subscribe(topic: string, handler: Function, queueConfig: Object, consumeConfig: Object, config: Object): string

Create subscription to given topic that will pass all incoming messages to given handler

public

tryReconnect(e: *)

public

unsubscribe(topic: string, consumerTag: string): Promise

Removes existing subscription or worker.

Public Constructors

public constructor(opts: object): void source

Microwork class construct

Params:

NameTypeAttributeDescription
opts object

Microwork instance options

opts.host string

RabbitMQ host to use

opts.exchange string

RabbitMQ exchange to use

opts.reconnectTimeout Number

Timeout before trying to reconnect to RabbitMQ on failure

Return:

void

Example:

const service = new Microwork({host: 'localhost', exchange: 'test.exchange'});

Public Members

public exchange: string source

RabbitMQ exchange name

public host: string source

RabbitMQ host address

public id: string source

Service unique ID

public reconnectTimeout: Number source

Reconnect timeout timer stored for later usage

public routeHandlers: Object source

Active route handlers and queues

Public Methods

public registerPlugin(plugin: Object): void source

Register new Microwork plugin

Params:

NameTypeAttributeDescription
plugin Object

Microwork plugin object

Return:

void

Example:

import myMicroworkPlugin from 'my-microwork-plugin';
microworkInstance.registerPlugin(myMicroworkPlugin);

public send(topic: string, data: Any, opts: Object): Promise source

Send given data to the specified topic

Params:

NameTypeAttributeDescription
topic string

Topic to send data to

data Any

Data to send

opts Object

Publish options for RabbitMQ

Return:

Promise

Returns promise that can be awaited to ensure termination

Example:

await microworkInstance.send('test.topic', 'test');
await microworkInstance.send('test.topic', {json: 'works too'});

public stop(): Promise source

Stops the service, closes all workers/subscriptions and terminates the connection to RabbitMQ

Return:

Promise

Returns promise that can be awaited to ensure termination

Example:

await microworkInstance.stop();

public subscribe(topic: string, handler: Function, queueConfig: Object, consumeConfig: Object, config: Object): string source

Create subscription to given topic that will pass all incoming messages to given handler

Params:

NameTypeAttributeDescription
topic string

Topic to subscribe to

handler Function

Handler function that will get all incoming messages

queueConfig Object

Queue config to pass to RabbitMQ

consumeConfig Object

Consume config to pass to RabbitMQ

config Object

Config for subscriber (e.g. wether to auto-ack messages)

Return:

string

Consumer tag that can be used for more precise unsubscribe action

Example:

Simple subscribe usage
await microworkInstance.subscribe('test.topic', (msg, reply) => {
	if (msg === 'ping') {
		reply('test.reply', 'pong');
	}
});
Subscribe with custom RabbitMQ options
await microworkInstance.subscribe('test.topic', (msg, reply) => {
	if (msg === 'ping') {
		reply('test.reply', 'pong');
	}
}, {durable: true, autoDelete: true, exclusive: true});
Subscribe without auto-ack
await microworkInstance.subscribe('test.topic', (msg, reply, ack, nack) => {
	if (msg === 'ping') {
		ack();
		reply('test.reply', 'pong');
	} else {
		nack();
	}
}, {}, {}, {ack: false});

public tryReconnect(e: *) source

Params:

NameTypeAttributeDescription
e *

public unsubscribe(topic: string, consumerTag: string): Promise source

Removes existing subscription or worker. If consumerTag is given only corresponding subscription will be removed. Otherwise, all consumers for given topic will be terminated.

Params:

NameTypeAttributeDescription
topic string

Topic to remove subscription/worker from

consumerTag string

Consumer tag to unsubscribe with

Return:

Promise

Returns promise that can be awaited to ensure removal

Example:

Remove one subscription with consumerTag
await microworkInstance.unsubscribe('test.topic', 'tag');
Remove all subscriptions with topic
await microworkInstance.unsubscribe('test.topic');