Home > Back-end >  My socket.io set up with express not working
My socket.io set up with express not working

Time:12-12

I have an express app running, and I wanted to add socket.io for real time features. So I followed the get started on socket.io. But somehow it is not working.

Thanks !

const express = require('express');
const app = express();
const http = require('http');
const server = http.createServer(app);
const { Server } = require("socket.io");
const io = new Server(server);

app.get('/', (req, res) => {
  res.sendFile(__dirname   '/index.html');
});

io.on('connection', (socket) => {
  console.log('a user connected');
});

app.listen(3000, () => {
  console.log('listening on *:3000');
});

CodePudding user response:

I once did this mistake. You should call the listen function on server not on app.

server.listen(3000, () => {
  console.log('listening on *:3000');
});
  • Related