vue3 composable for NATS
- connects to NATS using a websocket
- publish/subscribe to subjects
- reactive
Setup App.vue
import { createNats } from '@c0d3x42/vue3-nats'
import { createApp } from 'vue';
import App from 'App.vue';
createApp(App).use(createNats({servers: ['ws://localhost:8443']}));
Components
Publishing
import { publish } from '@c0d3x42/vue3-nats';
import { StringCodec } from 'nats.ws';
const sc = StringCodec();
publish("some.subject", sc.encode("hello from vue3"));
Subscribing
import { subscribe } from '@c0d3x42/vue3-nats';
const { msg } = subscribe("some.subject");
watch(msg, (m) => {
console.log("msg received", m.data)
})
Request/Response
import { request } from '@c0d3x42/vue3-nats';
import { StringCodec } from 'nats.ws';
const sc = StringCodec();
request("req.res.subject", sc.encode("some payload"))
.then(msg => {
console.log("response was", msg.data);
})