@taksa/bus
v0.0.2
Published
Microservice bus
Downloads
2
Readme
This is bus for backend RCP operations
server example
require('custom-env').env( process.env.CUSTOM_ENV || 'staging' )
const Bus = require('@taksa/bus')
const bus = new Bus(process.env.RABBIT_URL)
function fib(n) {
var a = 0, b = 1;
for (var i=0; i < n; i++) {
var c = a + b;
a = b; b = c;
}
return a;
}
bus.rpc('rpc_queue', (msg) => {
console.log(' [.] fib(%d)', msg)
return fib(+msg)
})
bus.rpc('two_q', (msg) => {
console.log(' [.] fib(%d)', msg)
return `Hello! ${msg}`
})
bus.rpc_json('test_json', async (json) => {
console.log(
json
)
await new Promise( res => setTimeout(res, 2000))
return {
...json,
done: true
}
})
client example
require('custom-env').env( process.env.CUSTOM_ENV || 'staging' )
const Bus = require('@taksa/bus')
const bus = new Bus(process.env.RABBIT_URL)
(async () => {
console.log(
await bus.req('rpc_queue', 30)
)
console.log(
await bus.req('two_q', 123)
)
console.log(
await bus.req_json('test_json', { test: 123 })
);
})()
docker-compose.yml for server
version: '3'
services:
rabbitmq:
image: rabbitmq:3.6-management-alpine
container_name: rabbitmq
volumes:
- ./.docker/rabbitmq/etc/:/etc/rabbitmq/
- ./.docker/rabbitmq/data/:/var/lib/rabbitmq/
- ./.docker/rabbitmq/logs/:/var/log/rabbitmq/
# environment:
# RABBITMQ_ERLANG_COOKIE: ${RABBITMQ_ERLANG_COOKIE}
# RABBITMQ_DEFAULT_USER: ${RABBITMQ_DEFAULT_USER}
# RABBITMQ_DEFAULT_PASS: ${RABBITMQ_DEFAULT_PASS}
ports:
- 5672:5672
- 15672:15672