{"size": 721, "ext": "js", "max_stars_repo_path": "src/sse.js", "max_stars_repo_name": "enkot/SSE-Fake-Server", "max_stars_repo_stars_event_min_datetime": "2019-11-19T13:10:23.000Z", "max_stars_repo_stars_event_max_datetime": "2021-05-23T10:17:36.000Z", "max_issues_repo_path": "src/sse.js", "max_issues_repo_name": "enkot/SSE-Fake-Server", "max_issues_repo_issues_event_min_datetime": null, "max_issues_repo_issues_event_max_datetime": null, "max_forks_repo_path": "src/sse.js", "max_forks_repo_name": "enkot/SSE-Fake-Server", "max_forks_repo_forks_event_min_datetime": "2021-05-23T10:17:44.000Z", "max_forks_repo_forks_event_max_datetime": "2021-05-23T10:17:44.000Z"}
{"max_stars_count": 4, "max_issues_count": null, "max_forks_count": 1, "avg_line_length": 22.53125, "max_line_length": 52, "alphanum_fraction": 0.4715672677}
let itAlive = null;

const SSE = (req, res) => {
    const aliveInterval = 60000;

    req.socket.setNoDelay(true);

    res.writeHead(200, {
        'Access-Control-Allow-Origin': '*',
        'Content-Type': 'text/event-stream',
        'Cache-Control': 'no-cache',
        'Connection': 'keep-alive'
    });

    itAlive = setInterval(() => {
        res.write(':ping\n\n');
    }, aliveInterval);
  
    return {
        send: (data) => {
            res.write('data: ' + data + '\n\n');
        },
        close: (callback) => {
            res.on('close', () => {
                if (itAlive) clearInterval(itAlive);
                if (callback) callback();
            });
        }
    };
};

export default SSE;