Adonis-Nats
Add helper on Controller for Adonis JS 4+
Table of contents
Installation
Run:
npm i --save @fickou/adonis4-nats
Install provider:
Go to start/app.js
const providers = [
...,
'@fickou/adonis4-nats/providers/NatsProvider'
]
Sample Usage
in service who you want to use
try {
const data={...};
const client = use("NatsClient");
await client.publish(`eventName`, data)
}catch (e){}
Listener
In a preload file:
Create preload file in start/nats.js
.
Add in server.js
.
const {Ignitor} = require('@adonisjs/ignitor');
new Ignitor(require('@adonisjs/fold'))
.appRoot(__dirname)
.preLoad('start/nats') // add
.fireHttpServer()
.catch(console.error);
After got to start/nats.js
const client = use("NatsClient");
await client.subscribe("eventName", function (message) {
console.log(message);
})
Message interface
Message {
msg: Msg<any>// go to nats docs
decodeMsgContent: Record<string, any>
sub: Subscription//go to nats docs
}
How to use nats ?
docker user:
create nats-cluster.yaml
version: "3.5"
services:
nats:
image: nats
ports:
- "8222:8222"
- "4222:4222"
command: "--cluster_name nats --cluster nats://0.0.0.0:6222 --http_port 8222 "
networks: ["nats"]
volumes:
- natsData:/data
networks:
nats:
name: nats
run :
docker docker compose -f nats-cluster.yaml up -d