socket.io-client icon indicating copy to clipboard operation
socket.io-client copied to clipboard

Cannot send more than 2 properties to my socketio server

Open YacineOurt opened this issue 1 year ago • 0 comments

The bug

Hi everyone, my bug is easy to understand. I want to send from my next api a message to a socketIO server running with nodejs. When I try to send a message including an object with 2 or less properties, it's working. But when I send an object with more than 2 properties, the socketIO server don't receive the message

To Reproduce

"socket.io-client": "^4.7.5" for the nextjs Api "socket.io": "^4.7.5" for the socketio server

SocketIO server

const http = require('http');
const { Server } = require('socket.io');

const server = http.createServer();
const io = new Server(server, {
  cors: {
    origin: '*',
  }
});

io.on('connection', (socket) => {
  console.log('A user connected:', socket.id, socket.username);


  socket.on("mytest", ({ first, second, third }) => {
    console.log("Received message: ", { first, second, third });
  });

  
  socket.on('disconnect', () => {
    console.log('A user disconnected:', socket.id, socket.username);
  });
});

server.listen(5000, () => {
  console.log('Server is running on port 5000');
});

NEXTJS API

"use server";
import { io } from 'socket.io-client';
import { NextApiRequest } from 'next';
import { NextResponse } from 'next/server';
import prisma from '@/lib/prisma';

const SOCKET_URL = 'ws://localhost:5000'; // URL du serveur Socket.io

const newSocket = io(SOCKET_URL);

newSocket.on('connect', () => {
    console.log("Connected with id: ", newSocket.id);
});

async function getMessagesToSend(visibility_date: Date) {
    try {
        const now = new Date();
        now.setMinutes(now.getMinutes() - 30);
        const messages = await prisma.messages.findMany({
          where: {
            visibility_date: { gte: now, lte: visibility_date }
          },
          select: {
            sender_id: true,
            receiver_id: true,
            content: true,
          }
        });
        return messages;
      } catch (error) {
        console.error('Erreur lors de la récupération des messages :', error);
        throw new Error('Erreur Interne du Serveur');
      }
}

async function handleScheduleMessage() {
    try {
        const now = new Date();
        const messagesToSend = await getMessagesToSend(now);
        
        messagesToSend.forEach((message) => { 
            newSocket.emit("mytest", {
              first: "first",
              second: "second",
              third: "third"
          });
        });

        return messagesToSend;
    } catch (error) {
        console.error('Erreur lors de la gestion des messages programmés :', error);
        throw new Error('Erreur Interne du Serveur');
    }
}

async function GET(req: NextApiRequest) {
    try {
        const response = await handleScheduleMessage();
        return NextResponse.json({ message: response }, { status: 200 });
    } catch (error) {
        console.error('Erreur lors de la gestion de la requête GET :', error);
        return NextResponse.json({ message: 'Une erreur s\'est produite lors de la gestion de la requête GET.' }, { status: 500 });
    }
}

module.exports = { GET };

Expected behavior If I run this code, the Socket io server don't console.log the received message (because it's probably not received). But if I remove the "third" property in the next api and in the socketio server, the message is displayed in my terminal

YacineOurt avatar May 10 '24 19:05 YacineOurt