Back to Discord Js

README

packages/brokers/README.md

14.26.44.5 KB
Original Source
<div align="center">
<p>
	<a href="https://discord.js.org"></a>
</p>


<p>
	<a href="https://discord.gg/djs"></a>
	<a href="https://www.npmjs.com/package/@discordjs/brokers"></a>
	<a href="https://www.npmjs.com/package/@discordjs/brokers"></a>
	<a href="https://github.com/discordjs/discord.js/actions"></a>
	<a href="https://github.com/discordjs/discord.js/commits/main/packages/brokers"></a>
	<a href="https://codecov.io/gh/discordjs/discord.js"></a>
</p>
<p>
	<a href="https://vercel.com/?utm_source=discordjs&utm_campaign=oss"></a>
	<a href="https://www.cloudflare.com"></a>
</p>
</div>

About

@discordjs/brokers is a powerful set of message brokers

Installation

Node.js 20 or newer is required.

sh
npm install @discordjs/brokers
yarn add @discordjs/brokers
pnpm add @discordjs/brokers

Example usage

pub sub

ts
// publisher.js
import { PubSubRedisBroker } from '@discordjs/brokers';
import Redis from 'ioredis';

const broker = new PubSubRedisBroker(new Redis());

await broker.publish('test', 'Hello World!');
await broker.destroy();

// subscriber.js
import { PubSubRedisBroker } from '@discordjs/brokers';
import Redis from 'ioredis';

const broker = new PubSubRedisBroker(new Redis());
broker.on('test', ({ data, ack }) => {
	console.log(data);
	void ack();
});

await broker.subscribe('subscribers', ['test']);

RPC

ts
// caller.js
import { RPCRedisBroker } from '@discordjs/brokers';
import Redis from 'ioredis';

const broker = new RPCRedisBroker(new Redis());

console.log(await broker.call('testcall', 'Hello World!'));
await broker.destroy();

// responder.js
import { RPCRedisBroker } from '@discordjs/brokers';
import Redis from 'ioredis';

const broker = new RPCRedisBroker(new Redis());
broker.on('testcall', ({ data, ack, reply }) => {
	console.log('responder', data);
	void ack();
	void reply(`Echo: ${data}`);
});

await broker.subscribe('responders', ['testcall']);

Contributing

Before creating an issue, please ensure that it hasn't already been reported/suggested, and double-check the documentation.
See the contribution guide if you'd like to submit a PR.

Help

If you don't understand something in the documentation, you are experiencing problems, or you just need a gentle nudge in the right direction, please don't hesitate to join our official discord.js Server.