# zquest

> Promise based request socket client

Latest version **1.1.2** (published 2016-10-25) · MIT license · 0 weekly downloads

## Install

```sh
npm install zquest
pnpm add zquest
yarn add zquest
bun add zquest
```

## Health

**Score 15/100 (F)** — status: abandoned.

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.1.2 |
| Published | 2016-10-25 |
| First published | 2016-10-11 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 4 |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 1 |
| Author | John Hofrichter |
| Maintainers | johnhof |
| Keywords | zmq, zeromq, request, promise |

## Links

- npm: https://www.npmjs.com/package/zquest
- Repository: https://github.com/johnhof/zmq-request
- Homepage: https://github.com/johnhof/zmq-request#readme
- Issues: https://github.com/johnhof/zmq-request/issues
- npm.io page: https://npm.io/package/zquest

## Dependencies (4)

- [zmq](https://npm.io/package/zmq.md) ^2.15.3
- [comise](https://npm.io/package/comise.md) ^1.0.1
- [lodash](https://npm.io/package/lodash.md) ^4.16.4
- [node-uuid](https://npm.io/package/node-uuid.md) ^1.4.7

## Alternatives

- [@commercetools/sync-actions](https://npm.io/package/@commercetools/sync-actions.md) — 25.1K weekly downloads
- [cwait](https://npm.io/package/cwait.md) — 21.4K weekly downloads
- [@ledgerhq/hw-app-cosmos](https://npm.io/package/@ledgerhq/hw-app-cosmos.md) — 4.2K weekly downloads
- [@financial-times/o-loading](https://npm.io/package/@financial-times/o-loading.md) — 2.8K weekly downloads
- [fa](https://npm.io/package/fa.md) — 185 weekly downloads

## Recent versions

- 1.1.2 (latest) — 2016-10-25
- 1.1.1 — 2016-10-25
- 1.1.0 — 2016-10-25
- 1.0.1 — 2016-10-24
- 1.0.0 — 2016-10-11

## README

# zquest

[![Build Status](https://travis-ci.org/johnhof/zquest.svg?branch=master)](https://travis-ci.org/johnhof/zquest)

Promise based [ØMQ](http://zeromq.org/) request client.

Best used in tandem with the [zeromatter](https://www.npmjs.com/package/zeromatter) server framework.

## Usage

Zquest is designed to loosely follow the [request](https://github.com/request/request) module. It comes with several build in friendly features:
- Self managing [socket pool](https://github.com/johnhof/zquest/blob/master/lib/pool.js)
  - Self expiring (10 min default), including member socket cleanup
  - Auto scaling of member sockets within bounds (2-500 default)
  - Socket acquisition timeouts
- Self expiring [sockets](https://github.com/johnhof/zquest/blob/master/lib/socket.js) (10 min default)
- Mapping of one socket pool per host

## Example

### Client

```javascript
let zquest = require('zquest');

// tcp://localhost:5555
zquest({
  message: 'testing'
}).then((data) => console.log(data)) // => testing

// tcp://127.0.0.1:5555
zquest({
  host: '127.0.0.1',
  port: 5555,
  message: 'testing'
}).then((data) => console.log(data)) // => testing
```

### Simple Server (Blocking)

**NOTE** for non-blocking, see the [reflector](https://github.com/johnhof/zquest/blob/master/test/utilities/reflector.js) server used for testing

```javascript
let zmq = require('zmq');
let zquest = require('zquest');

let socket = zmq.socket('rep');
socket.bindSync(`tcp://localhost:5555`);

socket.on('message', (msg) => {
msg = zquest.parse(msg);
console.log(msg.data); // => testing
socket.send(msg._request_id + ':' + msg.data);
});
```

## Key

- [Example](#example)
  - [Client](#client)
  - [Server](#server)
- [Caveat](#caveat)
- [Documentation](#)
  - [Defaults](#defaults)
  - [`zquest(req)`](#zquestreq)
  - [`zquest.defaults(opts)`](#zquestdefaultsopts)
  - [`zquest.close()`](#zquestclose)
  - [`zquest.client`](#zquestclient)
  - [`zquest.new(message)`](#zquestnewmessage)
  - [`zquest.parse(message)`](#zquestparsemessage)
  - [`zquest.parse.message(message)`](#zquestparsemessagemessage)
  - [`zquest.parse.messageId(message)`](#zquestparsemessageidmessage)


### Caveat

the responding server **MUST** return the request ID as part of the response. As **either** a property `_request_id` or prefixed to the request as `[ID]:`

Eg:
```javascript
zquest({ data: 'testing' });

socket.on('message', (message) => {
  message = zquest.parse(message); // => { _request_id: b6c537f5-73cb-4681-9d4c-786248c4dc93, data: 'testing' }

  // Either:
  socket.send(message._request_id + ':hello_world');

  // OR
  socket.send({
    _request_id: message._request_id,
    data: 'hello_world'
  });
});
```

## Documentation

### Defaults

defaults used by all zquest clients

```javascript
{
  "host": "127.0.0.1",
  "port": 5555,
  "timeout": 10000
  "pool": {
    "min": 2,
    "max": 500,
    "socket_expire": 600000,
    "self_expire": 600000,
    "acquire_retry": 100,
    "acquire_attempts": 10
  }
}
```

### `zquest(req)`

- Shared zquest client
- Accepts
  - `host` - host to connect to [default: `127.0.0.1`]
  - `port` - port to connect to [default: `5555`]
  - `data|message|body` -  message data to send
- Returns
  - `Promise`

```javascript
zquest({
    host: '127.0.0.1',
    port: 5555,
    message: 'testing'
}).then((response) => console.log(response))
```

### `zquest.defaults(opts)`

- Set defaults for the shared zquest client
- Accepts
  - Object to default over the [request defaults](#defaults)

```javascript
zquest.defaults({
  host: 'myserver.google.com',
  pool: {
    min: 5
  }
});
```

### `zquest.close()`

- Close and clean all pools/sockets

```javascript
zquest.close();
```

### `zquest.client`

- Shared zquest client

```javascript
zquest.client;
zquest.client.request() // === zquest()
```

### `zquest.new(message)`

- Utility to retrieve a new zquest client
- Accepts
  - Object to default over the [request defaults](#defaults) **for only this instance**
- Returns
  - New zquest client with its own independent configuration and defaults

```javascript
let myserver = zquest.new({
  host: 'myserver.google.com',
  pool: {
    min: 5
  }
});

myserver.defaults();
myserver.request();
```

### `zquest.parse(message)`

- Proxy of `zquest.parse.message`
- Accepts
  - `Buffer` or `String` message
- Returns
  - Object
    - `_request_id` - request ID which must be returned in the response for routing purposes
    - `data` - data in the message

```javascript
let message = zquest.parse(message);
console.log(message); // => { _request_id: [SOME_GUID], data: [BODY] }
```

### `zquest.parse.message(message)`

- Message parser used by the zquest client
- Accepts
  - `Buffer` or `String` message
- Returns
  - Object
    - `_request_id` - request ID which must be returned in the response for routing purposes
    - `data` - data in the message

```javascript
let message = zquest.parse.message(message);
console.log(message); // => { _request_id: [SOME_GUID], data: [BODY] }
```


### `zquest.parse.messageId(message)`

- Parser for retrieving only the message ID
- Accepts
  - `Buffer` or `String` message
- Returns
  - Request ID which must be returned in the response for routing purposes

```javascript
let id = zquest.parse(message);
console.log(id); // => [SOME_UUID]
```

## Authors

- [John Hofrichter](https://github.com/johnhof)

---
_Source: https://npm.io/package/zquest · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
