npm stats
  • Search
  • About
  • Repo
  • Sponsor
  • more
    • Search
    • About
    • Repo
    • Sponsor

Made by Antonio Ramirez

fetch-streaming

0.1.2

@paulmillr

npmHomeRepoSnykSocket
Downloads:3
$ npm install fetch-streaming
DailyWeeklyMonthlyYearly

fetch-streaming

Simple XMLHTTPRequest-based fetch implementation for streaming content.

60 LOC and no dependencies, works in all browsers.

This is not a replacement for fetch - there is no API match.

Install

npm install --save fetch-streaming

Usage

const fetchS = require('fetch-streaming');

// Would be executed for every new chunk of data.
fetchS('/api/stream', stream => console.log(stream));

// Options
const options = {method: 'POST', headers: {'Content-Type': 'BS'}};
fetchS('/api/stream', options, stream => console.log(stream));

// Promise is returned.
const result = fetchS('/api/stream');
result.then(response => {console.log(response.body)}, error => {console.error(error)})

Tips

Example of server-side express.js infinite endpoint.

app.get('/api/stream', (req, res) => {
  res.write(Date.now().toString());

  // Send new data every second.
  setInterval(() => {
    res.write(Date.now().toString());
  }, 1000);
});

License

MIT