Home > Enterprise >  Why is my socket io not connecting? Problem with connection io
Why is my socket io not connecting? Problem with connection io

Time:01-14

When starting my server I get the console.log from the listen, but not the console.log from the socket io connection with socket id

import express from "express";
import dotenv from 'dotenv';
import http from 'http';
import { Server } from 'socket.io';

dotenv.config();

const app = express();
const PORT = process.env.PORT;

const httpServer = http.createServer(app);
const io = new Server(httpServer);


io.on('connection', (socket) => {
    console.log("Socket connection is ready!", socket.id);
})
    

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

On my console it is just returned to me

Server is running on port 3009

CodePudding user response:

The server-side 'connection' event is supposed to be the event fired when a Socket.io client connects to such server. Moreover, the socket object passed to the event callback is the client socket object. So you won't see the 'connection' callback executed until a socket client tries to connect. Here you can find more info about how to set up a socket client with Socket.io.

If you need to better understand how websockets work take a look here.

  • Related