{"size": 1078, "ext": "js", "max_stars_repo_path": "lib/server/hub.js", "max_stars_repo_name": "401-advanced-javascript-joseph-zabaleta/caps", "max_stars_repo_stars_event_min_datetime": null, "max_stars_repo_stars_event_max_datetime": null, "max_issues_repo_path": "lib/server/hub.js", "max_issues_repo_name": "401-advanced-javascript-joseph-zabaleta/caps", "max_issues_repo_issues_event_min_datetime": null, "max_issues_repo_issues_event_max_datetime": null, "max_forks_repo_path": "lib/server/hub.js", "max_forks_repo_name": "401-advanced-javascript-joseph-zabaleta/caps", "max_forks_repo_forks_event_min_datetime": null, "max_forks_repo_forks_event_max_datetime": null}
{"max_stars_count": null, "max_issues_count": null, "max_forks_count": null, "avg_line_length": 19.962962963, "max_line_length": 62, "alphanum_fraction": 0.599257885}
'use strict';

/** 3rd Party */
require('dotenv').config({path:'../../.env'});
const net = require('net');
const server = net.createServer();
const { v4: uuidv4 } = require('uuid');

/** Local */
const PORT = process.env.PORT || 3000;

const socketPool = {};

server.on('connection', (socket) => {
    const id = uuidv4();
    socketPool[id] = socket;

    console.log(`Connection Established on Socket[${id}]`);

    socket.on('error', (error) => console.error(error));
    socket.on('end', () => { delete socketPool[id]; });
    socket.on('data', pushMessage);


});


function pushMessage(buffer) {
    let message = JSON.parse(buffer.toString());


    if (message.event && message.payload) {

        logger(message.event, message.payload);

        for (let socket in socketPool) {
            socketPool[socket].write(JSON.stringify(message));
        };

    };
};


function logger(event, payload) {
    let time = new Date();
    console.log("EVENT", {event, time, payload});
};


server.listen(PORT, () => {
    console.log(`Server is running on PORT:${PORT}`);
});

