|
| 1 | +Examples for Models.stream() |
| 2 | + |
| 3 | +.stream() will send records as soon as they are fetched instead of waiting until they have all been fetched and sending them all together. |
| 4 | + |
| 5 | +Not all adapters have a stream method. Currently only mongo, posgresql, and mysql have support for .stream() |
| 6 | + |
| 7 | +Unless you want the models in a format other than JSON, you dont need to supply the second parameter containing the write and end methods |
| 8 | + |
| 9 | + |
| 10 | + |
| 11 | +# Example Using Default Write and End Methods |
| 12 | + |
| 13 | + |
| 14 | +### Users Controller |
| 15 | + |
| 16 | +```javascript |
| 17 | +module.exports = { |
| 18 | + |
| 19 | + testStream: function(req,res){ |
| 20 | + |
| 21 | + if (req.param('startStream') && req.isSocket){ |
| 22 | + |
| 23 | + var getSocket = req.socket; |
| 24 | + |
| 25 | + Users.stream({name:'Walter'}).pipe(getSocket.emit); |
| 26 | + |
| 27 | + } else { |
| 28 | + |
| 29 | + res.view(); |
| 30 | + |
| 31 | + } |
| 32 | + |
| 33 | + |
| 34 | + } |
| 35 | +} |
| 36 | +```` |
| 37 | + |
| 38 | + |
| 39 | +### view for Users.testStream |
| 40 | + |
| 41 | +```javascript |
| 42 | +<style>.addButton{display:inline-block;line-height:100px;width:400px;height:100px;border:1px solid black;cursor:pointer;}</style> |
| 43 | +
|
| 44 | +<script> |
| 45 | +window.onload = function startListening(){ |
| 46 | + socket.on('gotUser',function(data){ |
| 47 | + console.log(data.name+' number '+data.id+' has joined the party'); |
| 48 | + }); |
| 49 | +}; |
| 50 | +
|
| 51 | +</script> |
| 52 | +<center> |
| 53 | +<div class="addButton" onClick="socket.get('/users/testStream/',{startStream:true})"> |
| 54 | +Stream all the Users ! </div> |
| 55 | +
|
| 56 | +``` |
| 57 | + |
| 58 | + |
| 59 | + |
| 60 | + |
| 61 | +# Example with Custom Write Methods |
| 62 | + |
| 63 | +### view for Users.testStream |
| 64 | + |
| 65 | +```javascript |
| 66 | +<style>.addButton{display:inline-block;line-height:100px;width:400px;height:100px;border:1px solid black;cursor:pointer;}</style> |
| 67 | +
|
| 68 | +<script> |
| 69 | +window.onload = function startListening(){ |
| 70 | + socket.on('gotUser',function(data){ |
| 71 | + console.log(data.name+' number '+data.id+' has joined the party'); |
| 72 | + }); |
| 73 | +}; |
| 74 | +
|
| 75 | +</script> |
| 76 | +<center> |
| 77 | +<div class="addButton" onClick="socket.get('/users/testStream/',{startStream:true})"> |
| 78 | +Stream all the Users ! </div> |
| 79 | +
|
| 80 | +``` |
| 81 | +### Users Controller |
| 82 | + |
| 83 | +```javascript |
| 84 | +module.exports = { |
| 85 | + |
| 86 | + testStream: function(req,res){ |
| 87 | +
|
| 88 | + if (req.param('startStream') && req.isSocket){ |
| 89 | +
|
| 90 | + var getSocket = req.socket; |
| 91 | + |
| 92 | + var streamMethods = { |
| 93 | + |
| 94 | + write: function(model,index,cb){ |
| 95 | + if (!model) { // Do not omit this line. Not all data events that are emitted are useful. |
| 96 | + getSocket.emit('gotUser',{id:model.id,name:model.name}); |
| 97 | + console.log('Found:'+JSON.stringify(model)); |
| 98 | + } |
| 99 | + |
| 100 | + return cb(); |
| 101 | +
|
| 102 | + }, |
| 103 | + end: function(cb){ |
| 104 | + console.log('We should be done streaming.'); |
| 105 | + cb(); |
| 106 | + } |
| 107 | +
|
| 108 | + } |
| 109 | +
|
| 110 | + var s = Users.stream({name:'Walter'},streamMethods).pipe(getSocket.emit); |
| 111 | + } else { |
| 112 | +
|
| 113 | + res.view(); |
| 114 | + |
| 115 | + } |
| 116 | +
|
| 117 | +
|
| 118 | + } |
| 119 | +} |
| 120 | +```` |
| 121 | +
|
| 122 | +
|
| 123 | + >You can also listen for stream events |
| 124 | +```javascript |
| 125 | + var s = Users.stream({name:'Walter'}) |
| 126 | + s.on('data',function(a){console.log('event:'+a)}); |
| 127 | + s.on('error',function(a){console.log('error:'+a)}); |
| 128 | + s.on('end',function(a){console.log('done')}); |
| 129 | +``` |
| 130 | + |
0 commit comments