Skip to content
This repository has been archived by the owner on Mar 25, 2024. It is now read-only.

Streaming Content

Aluisio Amaral edited this page Feb 6, 2018 · 9 revisions

Summary

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.

Starting

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 just pause() it. The EventEmitter 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.

Pausing

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:

pause(): Promise

// 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

Resuming is the same as when pausing:

resume(): Promise

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

Stopping is also the same, but note that, once stopped, you can't resume it. Example:

stop(): Promise

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