crp-stream-client v0.0.12
task-stream-client
Getting it
Install it
npm install --save crp-stream-client
Require and Instantiate
var Client = require('crp-stream-client');
var client = Client({
token: 'e1ffe165-10ad-beef-b00b-115B0Ab0053d'
});
You can also instantiate it with an email and password. You can generate a token like the one above with crp-account-client.
Use it
This module exposes:
- a writable Tasks stream;
- a readable Results stream;
- a readable Errors stream;
- a duplex stream with all of the above.
And they work like you would expect: you write tasks to the Task stream, read results from the Result stream and errors from the Error stream.
The duplex stream is a convenience stream that you can write tasks to it, it emits data events when results arrive, and error events when errors arrive.
They are all Object streams, meaning you should write objects and expect objects as well.
Tasks Stream
Tasks stream accepts any javascript object that is
JSON stringifyable. You write it to tasksStream
parsed,
and it will arrive also parsed at the Run
function, just
like you would expect.
Suppose the jobId
value used in the examples corresponds
to a valid job (created with crp-job-client) that returns whatever
it gets (an echo job really, like
function Run (d) { return d; }
).
var tasks = client(jobId).Tasks();
var n = 1000;
while (n--) {
tasks.write({
name: 'something',
n: n
});
}
Results Stream
The Results stream takes an options object, which can have
a timeout
and a stream
.
timeout
must be a string that has a number and a time
unit, such as 100ms
or 1s
or 1m
. The timeout means the
result stream will end after timeout
time has passed since
the last result was received.
stream
is a boolean that indicates whether you'd
like to receive results as soon as they arrive from the
browsers, or you want to fetch them from the database when
it's done.
If the job above is an echo job, we'll get 1000 results:
var results = client(jobId).Results({
timeout: '5s',
stream: true
});
results.on('data', onData);
function onData (data) {
console.log('result:', data);
}
Errors Stream
Sometimes, there are errors in running your tasks, and you can get them like this:
var errors = client(jobId).Errors({
timeout: '5s',
stream: true
});
errors.on('data', onError);
function onError (data) {
console.error(data);
}
The Errors stream takes the same options object as described in the Results stream.