Work with streams in JavaScript
There are a few ways to make use of nio.js, follow the instructions for the one that applies to your situation
-
Install from bower
bower install nio.js
-
Add the script to your HTML
<script src="./bower_components/nio.js/dist/nio.min.js"></script>
-
Use it! - See the Examples Section
-
Download the source file: https://raw.githubusercontent.com/nioinnovation/nio.js/master/dist/nio.min.js
-
Add the script to your HTML
<script src="./nio.min.js"></script>
-
Use it! - See the Examples Section
-
Install from npm
npm install niojs
-
Require nio
var nio = require('niojs')
-
Use it! - See the Examples Section
nio.source.socketio(
'http://yoursocketserver.com:8080',
['socket', 'rooms', 'go', 'here'],
120 // optional - will immediately stream cached data within the last 120 seconds
).pipe(nio.log())
The following methods allow you to filter/manipulate/work with streams of data. You can pipe streams (via .pipe(...)
) into these methods, which will then return their own streams.
Perform a function on the data but pass it through unchanged. Changes to the data inside of the function will not be realized in the output stream. Use nio.func()
to do that.
Example:
nio.source.generate({
test_a: 1,
test_b: 2
}).pipe(nio.pass(function(chunk) {
console.log("My value is " + chunk.test_a);
}));
Output:
My value is 1
Note that you did not have to return anything from the function, the original chunk was already emitted from the pass
function.
Perform a function on the data and emit the results of the function.
Example:
nio.source.generate({
test_a: 1,
test_b: 2
}).pipe(nio.func(function(chunk) {
return chunk.test_b + 5;
})).pipe(nio.log("Final value"));
Output:
Final value 7
Note that this time we did return something from the function. The output of the function is what will be emitted to the stream.
Log the data of the stream to the JavaScript console, with an optional prefix
Only emit the data if the function evaluates to true
Only emit the data if it contains an attribute property
.
Only emit the data if it contains an attribute property
and if its value is value
.
Emit the value of property
on the data, if it exists.
The following methods allow you to connect to data sources or generate data in a stream
Connect to a socket.io server and subscribe to a list of rooms.
Generate an asynchronous data stream at a regular interval.
- data (function or object): If
data
is a function, it can receive one argument which would be the iteration number (starting at 0) of the current execution. If it is an object, that object will be emitted. - maxTimes (int): How many times the data will get generated. Defaults to 1. Setting this to a negative number will cause it to run indefinitely.
- rate (int): The rate (in milliseconds) of how often to generate the data.
Example #1:
nio.source.generate({val: 1})
.pipe(nio.log("output"));
Output #1:
output {val: 1}
Example #2:
nio.source.generate({val: 1}, 3)
.pipe(nio.log("output"));
Output #2:
output {val: 1}
output {val: 1}
output {val: 1}
Example #3:
nio.source.generate(function(iter) {
return {val: iter};
}, 3).pipe(nio.log("output"));
Output #3:
output {val: 0}
output {val: 1}
output {val: 2}