1.1.2 • Published 8 years ago

zquest v1.1.2

Weekly downloads
2
License
MIT
Repository
github
Last release
8 years ago

zquest

Build Status

Promise based ØMQ request client.

Best used in tandem with the zeromatter server framework.

Usage

Zquest is designed to loosely follow the request module. It comes with several build in friendly features:

  • Self managing socket pool
    • 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 (10 min default)
  • Mapping of one socket pool per host

Example

Client

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 server used for testing

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

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:

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

{
  "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
  • Returns
    • Promise
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
zquest.defaults({
  host: 'myserver.google.com',
  pool: {
    min: 5
  }
});

zquest.close()

  • Close and clean all pools/sockets
zquest.close();

zquest.client

  • Shared zquest client
zquest.client;
zquest.client.request() // === zquest()

zquest.new(message)

  • Utility to retrieve a new zquest client
  • Accepts
  • Returns
    • New zquest client with its own independent configuration and defaults
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
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
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
let id = zquest.parse(message);
console.log(id); // => [SOME_UUID]

Authors

1.1.2

8 years ago

1.1.1

8 years ago

1.1.0

8 years ago

1.0.1

8 years ago

1.0.0

8 years ago