-
Notifications
You must be signed in to change notification settings - Fork 36
Streaming Content
Despite using the Stream name for this functionality, it is not the same as the Stream
class of the core nodejs. This functionality adopt the concept of a duplex Stream, where continuous flow of data is received and piped to a callback we provide. Since there is only one event, which is data
, we don't need to use the EventEmitter
, this client will wrap that for you, needing only an Error-first
callback to get the data streamed.
Before we start streaming data, it is important to know that:
- Doing something other than streaming will work, but you should never do it since it can cause memory leak on the long run.
- This functionality is meant for commands like
/ip address listen
or menus like/tool torch
or when you inform an interval for printing like/interface print stats-detail interval=2
, in which we are expecting continous flow of data. - If you are done with the stream, remember to
stop()
it, not justpause()
it. TheEventEmitter
will be waiting for data if not stopped. - After a stream is stopped, you can't resume it. In that case, just recreate the stream.
So, how do we start a stream? As simple as this:
stream(action: any, callback?: (error: Error, data: object[], stream: Stream) => void): Stream
var torchMenu = client.menu("/tool torch");
var torch = torchMenu.stream({
interface: "ether1"
}, (err, data) => {
if (err) {
// Error trying to stream
console.log(err);
return;
}
console.log(data); // { tx: 37894, rx: 35481, txPackets: 9, rxPackets: 9 }
});
An example when streaming print intervals:
var routerboardMenu = client.menu("/system routerboard");
var routerboard = routerboardMenu
.query({ interval: 2 })
.stream("print", (err, data) => {
if (err) {
// Error trying to stream
console.log(err);
return;
}
});
An example when streaming using "listen":
var addressMenu = client.menu("/ip address");
var address = addressMenu.stream("listen", (err, data) => {
if (err) {
// Error trying to stream
console.log(err);
return;
}
});
The callback we provided will run everytime data is received.
The API will send complete chunks of related ".section" packets, it means that might be a little delay when the API receives the data and passes to your callback (~300ms). This only applies to streams that has the .section
parameter, which is the case of /tool/torch
.
After we start our stream, the stream()
function will return a Stream
object, this object will allow us to pause, resume and stop our stream. Another way to get the running stream is to use the third parameter of the callback, example:
// Using the stream returned
var torch = torchMenu.stream({
interface: "ether1"
}, (err, data) => {
if (err) {
// Error trying to stream
console.log(err);
return;
}
console.log(data); // { tx: 37894, rx: 35481, txPackets: 9, rxPackets: 9 }
// Pausing
torch.pause().then(() => {
// Paused!
}).catch((err) => {
console.log(err);
});
});
// ---------------- //
// Using the third parameter
torchMenu.stream({
interface: "ether1"
}, (err, data, torch) => {
if (err) {
// Error trying to stream
console.log(err);
return;
}
console.log(data); // { tx: 37894, rx: 35481, txPackets: 9, rxPackets: 9 }
// Pausing
torch.pause().then(() => {
// Paused!
}).catch((err) => {
console.log(err);
});
});
Note: if you try to pause an already paused stream, nothing will happen, the Promise will fulfill :).
Resuming is the same as when pausing:
var torch = torchMenu.stream({
interface: "ether1"
}, (err, data) => {
if (err) {
// Error trying to stream
console.log(err);
return;
}
console.log(data); // { tx: 37894, rx: 35481, txPackets: 9, rxPackets: 9 }
// Pausing
torch.pause().then(() => {
// Paused!
return torch.resume();
}).then(() => {
// RESUMED!!!!!!!!!!!!
}).catch((err) => {
console.log(err);
});
});
Stopping is also the same, but note that, once stopped, you can't resume it. Example:
var torch = torchMenu.stream({
interface: "ether1"
}, (err, data) => {
if (err) {
// Error trying to stream
console.log(err);
return;
}
console.log(data); // { tx: 37894, rx: 35481, txPackets: 9, rxPackets: 9 }
// Stopping
torch.stop().then(() => {
// STOPPED!!!!!!
}).catch((err) => {
console.log(err);
});
});
Next: Entry Model