You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
There is no hapijs SSE example. I recently figured it out but don't know where to put it. So I have it here:
'use strict';constHapi=require('@hapi/hapi');constinit=async()=>{constserver=Hapi.server({port: 3000,host: 'localhost'});// Route using raw.res for direct response handlingserver.route({method: 'GET',path: '/sse',handler: async(request,h)=>{constres=request.raw.res;res.writeHead(200,{"Content-Type": "application/json"});constintervalId=setInterval(()=>{res.write('this is a server sent event message');},1000);// send a message every 1 second constsleep=(ms: number)=>newPromise((resolve)=>setTimeout(resolve,ms));awaitsleep(10000);// mock a long running request handlingres.end('This is a raw response using raw.res');returnh.abandon;// Abandon Hapi's normal response flow because we used raw response}});awaitserver.start();console.log('Server running on %s',server.info.uri);};process.on('unhandledRejection',(err)=>{console.log(err);process.exit(1);});init();
That is not really a SSE, as it needs to have Content-Type: text/event-stream.
Anyway, you should probably not use raw.res, but rather return a Passthrough stream that you can keep and later write the event data to. The response won’t end until the stream is closed.
Also, a Cache-Control: no-cache header is probably appropriate.
I just went through the code of susie, it seems pretty close to what kanongil has in mind. It's pretty old but I think it should be mostly functional even on today's hapi, you can always fork it if you find bugs.
Hi everyone ,
I noticed there’s some back-and-forth about adding a proper SSE example to the Hapi docs. I’d be happy to help put together a minimal, correct snippet (using text/event-stream, proper headers, and streams) if that would be useful. Could you confirm if a contribution like this would be welcome for the documentation?
Activity
kanongil commentedon Aug 3, 2024
That is not really a SSE, as it needs to have
Content-Type: text/event-stream.Anyway, you should probably not use
raw.res, but rather return aPassthroughstream that you can keep and later write the event data to. The response won’t end until the stream is closed.Also, a
Cache-Control: no-cacheheader is probably appropriate.wy193777 commentedon Aug 5, 2024
what's the difference between explicitly using res.end() compare to return a PassThrough?
Marsup commentedon Aug 5, 2024
I just went through the code of susie, it seems pretty close to what kanongil has in mind. It's pretty old but I think it should be mostly functional even on today's hapi, you can always fork it if you find bugs.
8th-block commentedon Oct 18, 2024
So is there a way to do "text/event-stream" in hapi?
damusix commentedon Nov 18, 2024
@wy193777 @8th-block Check this out: https://matthewwid.github.io/better-sse/reference/recipes/#hapi
I haven't tried it, but I ran into it and asked the maintainer to add a Hapi example and he did. Perhaps it can fit your needs?
hashda04 commentedon Aug 19, 2025
Hi everyone ,
I noticed there’s some back-and-forth about adding a proper SSE example to the Hapi docs. I’d be happy to help put together a minimal, correct snippet (using text/event-stream, proper headers, and streams) if that would be useful. Could you confirm if a contribution like this would be welcome for the documentation?
Thanks!