lolo32 / fastify-sse

Provide Server-Sent Events to Fastify
20 stars 16 forks source link

Add x-no-compression header #2

Closed jinxmcg closed 4 years ago

jinxmcg commented 6 years ago

Hi,

At this moment SSE on chrome and firefox do not play well with compressed event-streams, even though the GET request from the browsers comes with "accept-encoding:gzip, deflate, br", the browsers do not trigger onmessage event anymore if the stream is compressed. So when using this module with any compression module like fastify-compress SSE stops working.

By adding "x-no-compression" header to the request eg:

app.route({
        method: "GET",
        url: "/live",
        handler: (request, reply) => {
            let index
            const options = {}
            request.headers["x-no-compression"]=true            
            reply.sse({event: "test", data: index})
            const interval = setInterval(() => {
                reply.sse({event: "test", data: index})            
            }, 3*1000)            
        }
})

fixes the issue and the stream is uncompressed. I am talking about http2 on http1 it appears there is a little bit of workaround but might work (check the headers on this thread https://stackoverflow.com/questions/23769001/is-it-possible-to-use-gzip-compression-with-server-sent-events-sse - I tested the headers of suggested on stackoverflow and it does not work with http2)

I think the header x-no-compression should be added by default specially at least if the connection is http2

What do you think?

Thanks