kartikk221 / hyper-express

High performance Node.js webserver with a simple-to-use API powered by uWebsockets.js under the hood.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

From version 6.0.0, the response component doesn´t call a close or abort event when the SSE client is closed

penadeurubu opened this issue · comments

When I tested the code below on versions 6.14.9 and 6.0.0, the response component didn´t call a close or abort event when the SSE client was closed.

When I tested the code below on version 5.9.2, the response component called an abort event when the SSE client was closed.

Test code - Server.js

const { Server } = require("hyper-express")
const crypto = require('crypto')

const server = new Server()
const sse_streams = {};

server.get('/news/events', (request, response) => { 
    
    // You may perform some authentication here as this is just a normal HTTP GET request
    
    if (response.sse) {        
        response.sse.open();
        response.sse.send('data: Some initial message \n\n');

        response.sse.id = crypto.randomUUID();
        sse_streams[response.sse.id] = response.sse;
        console.log("New Connection - ID =",response.sse.id)

        response.on('close', () => {
            console.log("Close Connection - delete ID =", response.sse.id)            
            delete sse_streams[response.sse.id]
        });
        response.on('abort',() => {
            console.log("Close Connection - Delete ID =", response.sse.id)
            delete sse_streams[response.sse.id]
        });        
    }
     else {
        console.log("Server-Sent Events Not Supported")
        response.sse.send('data: Server-Sent Events Not Supported!\n\n');
    }
});

server.listen(8000)

Test code - Client.js

const  eventSource = require('eventsource');
const evtSource = new eventSource('http://localhost:8000/news/events', {});  

evtSource.onmessage = (event) => {
    console.log(event)
};

evtSource.addEventListener('error', function(e) {
    if (e.readyState == EventSource.CLOSED) {
        console.log("Connection was closed")
    }
}, false)

Hi, this bug has been fixed in the recent v6.14.10 version. Let me know If you have any other issues.