Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

A hapijs server sent event (SSE) example #4521

Open
wy193777 opened this issue Aug 2, 2024 · 4 comments
Open

A hapijs server sent event (SSE) example #4521

wy193777 opened this issue Aug 2, 2024 · 4 comments
Labels
documentation Non-code related changes

Comments

@wy193777
Copy link

wy193777 commented Aug 2, 2024

Module version

21.3.10

What documentation problem did you notice?

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';

const Hapi = require('@hapi/hapi');

const init = async () => {
    const server = Hapi.server({
        port: 3000,
        host: 'localhost'
    });

    // Route using raw.res for direct response handling
    server.route({
        method: 'GET',
        path: '/sse',
        handler: async (request, h) => {
            const res = request.raw.res;
            res.writeHead(200, { "Content-Type": "application/json" });

            const intervalId = setInterval(() => {
                res.write('this is a server sent event message');
            }, 1000);  // send a message every 1 second 
            const sleep = (ms: number) => new Promise((resolve) => setTimeout(resolve, ms));
            await sleep(10000);  // mock a long running request handling

            res.end('This is a raw response using raw.res');
            return h.abandon; // Abandon Hapi's normal response flow because we used raw response
        }
    });

    await server.start();
    console.log('Server running on %s', server.info.uri);
};

process.on('unhandledRejection', (err) => {
    console.log(err);
    process.exit(1);
});

init();
@wy193777 wy193777 added the documentation Non-code related changes label Aug 2, 2024
@kanongil
Copy link
Contributor

kanongil commented 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 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.

@wy193777
Copy link
Author

wy193777 commented Aug 5, 2024

Contributor

what's the difference between explicitly using res.end() compare to return a PassThrough?

@Marsup
Copy link
Contributor

Marsup commented 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
Copy link

So is there a way to do "text/event-stream" in hapi?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
documentation Non-code related changes
Projects
None yet
Development

No branches or pull requests

4 participants