A dream-class that handles sequential-request streaming.
- POSTs multipart request bodies
- Caches all requests
- Failed request are retried immediately within a specified time window
- If >10% of requests fail during the stream, the entire post sequence is cached for later retry under conditions of greater reliability.
var reachability = require('reachability');
/**
* init post sequence
*/
var postSequence = new PostSequence({
endpoint: '/videos/12345/append'
});
/**
* begin stream
*/
postSequence.post({
mediaSegment = new Buffer('faux-content'),
fileSequence = 0,
isFinalMediaSegment = false
});
postSequence.post({
mediaSegment = new Buffer('faux-content'),
fileSequence = 1,
isFinalMediaSegment = false
});
postSequence.post({
mediaSegment = new Buffer('faux-content'),
fileSequence = 2,
isFinalMediaSegment = false
});
postSequence.post({
mediaSegment = new Buffer('faux-content'),
fileSequence = 3,
isFinalMediaSegment = false
});
postSequence.post({
mediaSegment = new Buffer('faux-content'),
fileSequence = 4,
isFinalMediaSegment = true
});
/**
* end stream
*/
postSequence.end(function(error, failedRequests, allRequests) {
if (error || failedRequests.length / allRequests.length > 0.10) {
/**
* if streaming encountered significant failure, retry
* all requests sequentially when wifi is available.
*/
reachability.on('wifi', function() {
postSequence.retry(allRequests);
});
} else {
console.log('Success!');
}
});