Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

132
Views
WebSocket data cannot be converted to array from the buffer

I simply send an array from client to server. When I try to log this array from the client-side it gives me random values in the array.

Client-Side Code

let ws = new WebSocket("ws://localhost:4000");
ws.send([1,2,3]);

Server-Side Code

wss.on("connection", (ws) => {
  ws.on("message", m => {
    //I searched a little and learnt a way to convert buffer to array with a simple ES6 feature but it doesn't work properly
    console.log([...m]);
  });
});

The output is: [ 49, 44, 50, 44, 51 ]

Please post how can I fix it, and explain the reason for it so I and other people can understand the logic behind it.

about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

try parsing the buffer into a javascript object using JSON.parse(m)is not required.
Please note that if the message is not a JSON string, this will throw an error.

about 4 years ago · Juan Pablo Isaza Report

0

I still don't know why it happens but know I got a workaround for this problem. I'm simply converting the array to a string on the client-side and reconverting it to an array on the server side.

Client-Side

ws.send([1,2,3].toString());

Server Side

arr = []

wss.on("connection", (ws) => {
    ws.on("message", m => {
        arr = m.toString().split(",");
        arr = arr.map(x => {
            return parseInt(x);
        });
        console.log(arr);
    });
});

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!