Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/bifot/ms-udp
Solution for communication between services using UDP protocol with built-in auto-retry & round-robin balancing. 🔬
https://github.com/bifot/ms-udp
Last synced: 12 days ago
JSON representation
Solution for communication between services using UDP protocol with built-in auto-retry & round-robin balancing. 🔬
- Host: GitHub
- URL: https://github.com/bifot/ms-udp
- Owner: bifot
- License: apache-2.0
- Created: 2019-04-23T14:27:01.000Z (over 5 years ago)
- Default Branch: master
- Last Pushed: 2022-12-30T17:32:15.000Z (almost 2 years ago)
- Last Synced: 2024-12-08T09:19:45.735Z (15 days ago)
- Language: JavaScript
- Homepage: https://www.npmjs.com/package/ms-udp
- Size: 423 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 10
-
Metadata Files:
- Readme: README.md
- License: LICENSE.md
Awesome Lists containing this project
README
# ms-udp
Solution for communication between services using UDP protocol with built-in auto-retry & round-robin balancing. 🔬
## Install
```sh
$ npm i ms-udp -S
```## Examples
[There are some simple examples](examples).
## API
### Server
#### .constructor()
```js
const server = new udp.Server();
```#### .on(action, ...middlewares)
* `event` <[string](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type)> Action name
* `...middlewares` <[function[]](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function)> Action middlewaresThis method creates action.
```js
const { Balances } = require('./db');server.on('get', async (ctx) => {
const { amount } = await Balances.findOne({
userId: ctx.payload.userId,
});
ctx.reply(amount);
});
```#### .use(...middlewares)
* `...middlewares` <[function[]](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function)> Common middlewares
This method creates common middlewares.
#### .listen(port[, host, callback])
* `port` <[number](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type)>
* `host` <[?string](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type)>
* `callback` <[?function](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function)>This method starts listening.
### Client
#### .constructor(options)
* `options` <[Object](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object)>
* `services` <[Object](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object)> Available services
* `[key]` - <[string](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type)> Service name
* `[value]` - <[string](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type) / [Array](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array)<[string](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type)>> Service's address```js
const client = new udp.Client({
services: {
balances: '127.0.0.1:3000',
},
});
```#### .mock(requests)
* `requests` <[Object](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object)>
* `[key]` <[string](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type)> Service name
* `[value]` <[Object](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object)> Service requests
* `[key]` <[string](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type)> Event name
* `[value]` <[any](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures)> Event response
This method save mocks responses for [`.ask`](#askname-payload-options).
```js
if (process.env.NODE_ENV === 'test') {
client.mock({
balances: {
get: 200,
},
users: {
create: payload => payload.userId >= 100,
},
});
}const [balance, badUser, goodUser] = await Promise.all([
client.ask('balances.get', { userId: 1 }),
client.ask('users.create', { userId: 10 }),
client.ask('users.create', { userId: 200 }),
]);console.log(balance); // => 200
console.log(badUser); // => false
console.log(goodUser); // => true
```#### .ask(name[, payload, options])
* `event` <[string](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type)> Event name in format `.`
* `payload` <[?Object](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object)> Event data
* `options` <[?Object](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object)> Options
* `attempts` <[?number](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type)> Maximum number of attempts *(default: 5)*
* `timeout` <[?number](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type)> Maximum timeout in ms *(default: 5000)*This method asks other service for something.
```js
app.use(async (ctx, next) => {
const isAuth = await ctx.udp.ask('users.checkAuth', {
login: ctx.query.login,
password: ctx.query.password,
});ctx.assert(isAuth, 403);
await next();
});
```#### .middleware()
This method returns middleware for Koa or Express.
```js
const Koa = require('koa');
const { Client } = require('ms-udp');const app = new Koa();
const client = new Client();app.use(client.middleware());
app.use((ctx) => {
ctx.body = 'Hello, world!';
});app.listen(3000);
```