Skip to content

Send HTTP request to multiple endpoints at once with minimal overhead!

License

Notifications You must be signed in to change notification settings

AlexPereverzyev/fanout

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

10 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

fanout

npm Coverage Status

Send HTTP request to multiple endpoints at once with minimal overhead using the thin wrapper around Node.js HTTP API.

Fanout excels at handling large amounts of outgoing HTTP traffic (think 100 outgoing per 1 incoming requests) and outperforms existing Node.js HTTP clients by 20-50%.

Installation

npm install fast-fanout

Usage

Fanout provides flexible API to send request batches efficiently.

For example, to post JSON payload to multiple endpoints and process responses as soon as they arrive:

const fanout = require('fast-fanout')

const options = [
    'http://1st.endpoint.com',
    'http://2nd.endpoint.com',
    'http://3rd.endpoint.com',
]
.map(endpoint => {
    return {
        url: endpoint,
        method: 'POST',
        timeout: 200,
    }
})

const payload = { test: true }

for (let response of fanout(options, payload)) {
    try {
        const res = await response
        const body = await res.json()
    } catch (e) {
        console.error(e)
    }
}

fanout is a generator function, it returns promises and resolves them as soon as responses arrive. In turn, returned promise resolves to native Node.js HTTP response which can be piped further.

Request Options

The following request options are available:

  • url - endpoint URL
  • method - HTTP method (GET default)
  • headers - customize request headers per endpoint
  • body - customize request body per endpoint
  • timeout - response timeout (1000 ms default)
  • agent - HTTP(S) agent for connection pooling

Besides request options, fanout accepts the following optional arguments (in order):

  • body - common for all endpoints request body
  • headers - common request headers
  • lookup - custom DNS lookup function
  • callback: function(err, res) - when provided, fanout executes the callback for each received response in order they arrive and no longer works as promise generator. The reason to support callback - they are a bit faster than promises

Where body can be Unicode string, stream or object (stringified before request sent).

About

Send HTTP request to multiple endpoints at once with minimal overhead!

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published