Socket.io emit an event with data will disconnect the client with "parse error"

Viewed 15

I built a Socket.io server which handle connection and event :

import { createServer } from "http";
import { Server } from "socket.io";

const httpServer = createServer();
const io = new Server(httpServer);

io.on("connection", (socket) => {
    console.log('New connection');

    socket.on('join', data => {
        console.log(data);
    });
   ...
});

I'm trying to built a Socker.io client using npm module socket.io-client

import { io } from 'socket.io-client';
const socket = io(servHost);

socket.on('connect', () => {
    console.log(`Connected : ${socket.id}`);
        
    socket.emit('join', "Some data");
    socket.emit('otherEvent');

  });

 socket.on('disconnect', (err) => {
    console.log(err);
    console.log("Disconnected");
});

Server log :

New connection
New connection
...

Client log :

Connected : IyR4Gr0Gvao16UD6AAAD
parse error
Disconnected
Connected : m8aSe6HI67i6VIBNAAAF
parse error
Disconnected
...

Note : If I comment socket.emit('join', "Some data"); everything works fine, even the otherEvent is emited and client can handle the expected event in return (not shown on the code). So I guess the error comes from the data inside join event.

some data can be string / array or object (I tried everything) I have the same error.

I'm using :

"socket.io": "^4.5.2",
"socket.io-client": "^4.5.2"
0 Answers
Related