Created
September 1, 2015 20:01
-
-
Save ben-bradley/20018294d50a8cecc711 to your computer and use it in GitHub Desktop.
http streaming with hapi
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
'use strict'; | |
import Stream from 'stream'; | |
import config from 'config'; | |
import Hapi from 'hapi'; | |
let server = new Hapi.Server(config.server); | |
server.connection(config.connection); | |
server.route({ | |
method: 'get', | |
path: '/', | |
handler: (request, reply) => { | |
let stream = new Stream.Transform(); | |
let i = setInterval(() => { | |
stream.push(JSON.stringify({ foo: 'bar' }) + '\n'); | |
}, 100); | |
setTimeout(() => { | |
clearInterval(i); | |
stream.push(null); | |
console.log('ending stream'); | |
}, 5000); | |
console.log('starting stream'); | |
reply(stream); | |
} | |
}); | |
server.start((err) => { | |
if (err) throw new Error(err); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment