'use strict';

import { log, LOG_TYPES } from './log';

/*
 * Socket.io related things go !
 */

let io;

let connections = [];

function createSocketIO(server, app) {
    io = require('socket.io')(server);
 
    io.on('connection', (socket) => {
        log(`Socket connected with id ${socket.id}`);
        connections.push(socket);

        socket.on('disconnect', () => {
            log(`Socket disconnected with id ${socket.id}`, LOG_TYPES.WARN);

            var i = connections.indexOf(socket);
            connections.splice(i, 1);
        });

    });
}


export { io, connections, createSocketIO }


'use strict';

import { log, LOG_TYPES } from './log';
import decodeJWT          from './decodeJWT';
/*
 * Socket.io related things go !
 */

let io;

let connections = [];

function createSocketIO(server, app) {
    io = require('socket.io')(server);

    io.on('connection', (socket) => {

        socket.on('disconnect', () => {
            log(`Socket disconnected with id ${socket.id}`, LOG_TYPES.WARN);

            var i = connections.indexOf(socket);
            connections.splice(i, 1);
        });

        decodeJWT(socket.handshake.query.token)
            .then( results => {
                log(`Socket connected with id ${socket.id}`);
                socket.user = results;
                connections.push(socket);
            })
            .catch(error => {
                console.log('token is invalid');
                socket.emit('token_invalid', {}); 
                socket.disconnect(true);
            })
    });
}


export { io, connections, createSocketIO }


Check JWT token on socket connection