Crates.io | naps |
lib.rs | naps |
version | 0.2.1 |
source | src |
created_at | 2022-01-26 00:43:07.693403 |
updated_at | 2022-02-27 14:22:50.958105 |
description | NATS server proxy service to relay and forward custom topics and subjects |
homepage | https://github.com/sonirico/naps |
repository | https://github.com/sonirico/naps |
max_upload_size | |
id | 521185 |
size | 143,581 |
Simple tool to forward specific topics from one nats.io cluster to the same server or another. Provides support to process messages with deno Javascript or TypeScript code.
Imagine that we use nats.io to relay events for every confirmed or canceled order in our shopping platform:
./naps --source nats://aws:4222 --destination nats://aks:4222 --topics "orders.>"
If the --script
flag is present, naps
will spawn a Deno
runtime with all v8
capabilities plus promises and all event loop goodies, allowing you to, for example, only
keep the confirmed ones and relay them to the myapp.orders.confirmed
. You only have to code a recv
function
with the following signature:
interface RecvResult {
topic: string,
msg: string
};
function recv(topic: string, data: Uint8Array): boolean | RecvResult {
//... your code here...
}
true
, the message will be simply forwarded to the same topic. Do note that the message
will end up twice in the topicfalse
, this message will be discardedRecvResult
is returned, that data will be sent over the nats wire../naps --source nats://aws:4222 --destination nats://aks:4222 --topics "myapp.v1.orders" --script "
import { Buffer } from 'http://deno.land/x/node_buffer/index.ts';
interface Order {
status: 'confirmed' | 'canceled',
user: string,
amount: number,
item: any
};
function processOrder(data: Buffer): RecvResult {
const orderRaw = data.toString();
const order = JSON.parse(orderRaw) as Order;
// Skip orders that are not confirmed
if (order.status !== 'confirmed') {
return false;
}
return {
topic: 'myapp.v1.orders.confirmed',
msg: orderRaw
};
}
function recv(topic, uint8array) {
switch (topic) {
case "myapp.v1.orders":
return processOrder(Buffer.from(uint8array))
default:
// nothing to do...
}
}
"
RecvResult
when employing deno runtime