socketioxide icon indicating copy to clipboard operation
socketioxide copied to clipboard

Emitting with an array/vector data incorrectly serialized as separate arguments.

Open NessajCN opened this issue 1 year ago • 14 comments

Describe the bug As described in title, emitting an event with an array or vector like:

let v = vec![1,2,3];
socket.emit("message",  v).ok();

will split elements in the vector and send them as separate arguments. Clients receive that many arguments instead of the intact array:

To Reproduce Run a server using socketioxide in rust and start a Javascript or python client. Emitting an event from server with an array/vector payload.

socket.on("message", (arg)=>{
    // will print 1
    console.log(arg);
    // error
    console.log(arg[1]);
})

socket.on("message", (...arg)=>{
    // will print 
   // 1
   // 2
   // 3 
    for (const a of arg) {
            console.log(a);
    }
})

Expected behavior The expected resulte would be:

socket.on("message", (arg)=>{
    // will print [1,2,3]
    console.log(arg);
    // will print 2
    console.log(arg[1]);
})

Same behavior for tuple and array.

Versions (please complete the following information):

  • Socketioxide version: 0.10.0
  • Http lib: axum 0.7
  • Socket.io client version js v4.7.2

Additional context If I wrap the array/vector with an additional array, the results will be fine:

let v = vec![1,2,3];
socket.emit("message",  [v]).ok();

NessajCN avatar Jan 05 '24 05:01 NessajCN