Next.js Discord

Discord Forum

Whenever I use socket.io with Next.js I get this error: GET http://localhost:3000/socket.io?EIO=4&tr

Unanswered
Hokkaido posted this in #help-forum
Open in Discord
Original message was deleted.

4 Replies

Hokkaido
This is my API route:
import { Server } from 'socket.io';

export default async function handler(req, res) {
  try {
    if (res.socket.server) {
      console.log('Already set up');
      res.end();
      return;
    }

    console.log('Creating socket.io server instance');
    const io = new Server(req.socket.server);
    res.socket.server = io;

    io.on('connection', (socket) => {
      console.log('Client Connected!');

      socket.on('send-message', (data) => {
        io.emit('receive-message', data);
      });
    });

    res.status(200).end();
  } catch (error) {
    console.error('Error setting up socket.io:', error);
    res.status(500).end();
  }
}
I've installed and imported all the required packages
Golden northern bumble bee
Downgrade to next@13.2.4 and it will work