View our GitHub

Please visit sails-docs on GitHub to view documentation on your mobile device.

Edit Page

.stream( criteria )

Purpose

This method uses a node write stream to pipe model data as it is retrieved without first having to buffer all of the results to memory.

Overview

Parameters

Description Accepted Data Types Required ?
1 Find Criteria {},[{}], string, int Yes
2 Custom Write/End Methods {} No

Returned

Description Possible Data Types
1 Error Error
2 Stream of Records stream

Example Usage

UsersController.js

module.exports = {

  testStream: function(req, res){

    if (req.param('startStream') && req.isSocket){

        var getSocket = req.socket;

        // Start the stream.  Pipe it to sockets.
        User.stream({name:'Walter'}).pipe(getSocket.emit);

    } else {

      res.view();

    }


  }
}
`

views/users/testSocket.ejs

<script type="text/javascript">
window.onload = function startListening(){
    socket.on('gotUser',function(data){
      console.log(data.name + ' number ' + data.id + ' has joined the party');
    });
};

</script>
<div class="addButton" onClick="socket.get('/users/testStream/', {startStream:true})">Stream all the Users!</div>

Notes

This method is useful for piping data from VERY large models straight to res. You can also pipe it other places. See the node stream docs for more info. Only the mongo, mysql, and posgresql adapters support this method. This won't work with the disk adapter. Any string arguments passed must be the ID of the record.

Is something missing?

If you notice something we've missed or could be improved on, please follow this link and submit a pull request to the sails-docs repo. Once we merge it, the changes will be reflected on the website the next time it is deployed.

Reference

We wrote a book!

Get early access to the book
with promotion code: mcneildoc

Get the Book