Ad

Socket.IO Connection Is Not Establishing

I am not able to establish the connection to Socket.IO in node server. My server is successfully started with Socket.IO but during the connection to Socket, I am not getting the console logs.

 this.server.listen(this.port, () => {
        console.log(`Server is running on ${this.port} with process id ${process.pid}`)
    });

 this.io.on('connection', (socket: any) => {
        console.log(`Connected client on port ${this.port}`);

        socket.on('messsage', (msg: Message) => {
            console.log(`Data: ${msg}`);
            socket.emit('message', msg);
        });

        socket.on('disconnect', () => {
            console.log(`Client Disconnected`);
        });
    });
Ad

Answer

try to change your code: var socket = io.connect('localhost');

to

var socket = io.connect('http://localhost:port');

Ad
source: stackoverflow.com
Ad