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

4 messages · Page 1 of 1 (latest)

smoky hamlet
#

This is my API route:

wild steppeBOT
#

🔎 This post has been indexed in our web forum and will be seen by search engines so other users can find it outside Discord

      🕵️ Your user profile is private by default and won't be visible to users outside Discord, if you want to be visible in the web forum you can add the "Public Profile" role in <id:customize>

      ✅ You can mark a message as the answer for your post with `Right click -> Apps -> Mark Solution`
      (if you don't see the option, try refreshing Discord with Ctrl + R)
smoky hamlet
#
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