-
Notifications
You must be signed in to change notification settings - Fork 9
How to stream.pipe response in hapi v17 #618
Comments
Can you explain a little more about what you mean when you say "waiting until the end to send the data"? |
Under hapi 16, we were able to pipe the stream on the response: When sending large pdf documents, this allowed the first page to display to the user long before the rest of the document was completed rendering and sending. Now with hapi 17, we are using a different syntax (see above), which does not behave the same way to the user. They don't see any pages until the whole pdf document has been sent by the server and received. So, essentially, I'm looking for a way to "pipe" a stream on a hapi response. Does "h.response(stream)..." do that (if so, I'm not seeing it), or is there some way to adapt the hapi 16 technique we were using to hapi 17 with async/await? |
To my knowledge that should work just fine as-is. How can you tell that hapi's behavior has changed? If you need to take control of the response yourself you can always return |
Hi, I receive the error and the open events. I cannot have the messages. In my front js : let source = new EventSource('/builds')
source.onmessage = event => { console.log('mess', event.data) }
source.onerror = event => { console.log('err', event) }
source.onopen = event => { console.log('open', event) } In my server for the route const channel = new stream.PassThrough
setInterval(() => {
channel.write('event: message\n')
channel.write('data: abcdef\n\n')
console.log('write data...')
}, 1000)
return h
.response(channel)
.type('text/event-stream')
.header('Connection', 'keep-alive')
.header('Cache-Control', 'no-cache') |
@yereby Are you sure it is not because of missing gzip flushing? See hapijs/hapi#3599 for details. |
@kanongil It is possible but i don't know how to cut it in v17. A little step forward i added It is somehow due to the sending response but i don't know how |
Does anyone have any ideas for the original issue of this post? In Hapi 17, how do you do a |
My guess is that h.response "reads" a stream (converts to a buffer) rather than actually listening to and piping. Perhaps implemented as a fallback? So h.response is probably not the correct way to do it. Using request.raw.res is advised against in the docs, maybe there should be a feature request for a writeable. |
With all due respect, I'd say you were doing it wrong on hapi 16, trying to do it the same way again would be an error. @kanongil referenced an issue that was closed by a commit, have a look at its tests and try it this way. |
I can confirm the other issue helped. request.raw.res.compressor = compressor; And i can call it in my interval like this : handler: (request, h) => {
try {
const channel = new stream.PassThrough
setInterval(() => {
channel.write('event: message\n')
channel.write('data: abcdef ' + it.i + '\n\n')
console.log('write data...', it.i)
request.raw.res.compressor.flush()
}, 1000)
return h
.response(channel)
.type('text/event-stream')
} catch(err) { throw err }
} Indeed the response is flushed and i got my stream send. But now i am wondering how to implement this the good way oO |
Hello, By defining the compression option in my server But it is defined for the entire server. return h
.response(channel)
.type('text/event-stream; charset=utf-8')
.header('Content-Encoding', 'none') I am not sure that's the good way to do it. Somebody has some good practice ? Thanks. |
@yereby Would it be possible to use the server.options.mime to override the compression for example for just the stream mimetype? There is an example here https://hapijs.com/api how to override. |
@johnparn I believe that would serve to override all mimetype, and not for juste one route. |
I've exactly the same prob. any ideal how to resolve it ? tried with compression = false, not working |
Here\s a working / work in progress example for hapi16 The key is the SSEStream class - it flushes the compressor. |
In hapi 16, we were doing this:
stream.pipe(request.raw.res)
In hapi 17, this works, but is waiting until the end to send the data:
Any ideas on how to make this work well in hapi v17?
Much appreciated.
The text was updated successfully, but these errors were encountered: