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

287
Views
How to decode websocket data on client?

I've spent numerous hours trying to decode a buffer from the server and I haven't been anle to figure it put.

I've tried to remove JSON.parse() and I still an error.

This is the sent data from the backend where I convert a plain object to a buffer and stringify it:

 wss.clients.forEach((client) => {
      if (client.userID === contactID) {
        console.log(client.userID);
        client.send(Buffer.from(JSON.stringify(messageObj)));
      }
    });

On the frontend I have a listener:

 ws.socket.addEventListener("message", async (message) => {
          const blobToText = await new Response(JSON.parse(message.data)).text();
          alert(blobToText);
          console.log(blobToText)
        });

This is the error i'm getting:


[Unhandled promise rejection: SyntaxError: JSON Parse error: Unexpected identifier "object"]
at components/Home/Home.js:38:12 in ws.socket.addEventListener$argument_1

When I remove buffer from the backend (just using stringify), I get this on client:

{"fields":{"consumerTag":"amq.ctag-LijiOI-nknkknjknj","deliveryTag":16,"redelivered":false,"exchange":"","routingKey":"messages"},"properties":{"headers":{}},"content":{"type":"Buffer","data":[123,34,114,101,97,100,34,58,102,97,108,115,101,44,34,116,105,109,101,83,101,110,116,34,58,49,54,52,54,48,55,52,50,54,52,55,54,53,44,34,109,101,115,115,97,103,101,34,58,34,102,101,114,102,101,114,102,34,44,34,117,115,101,114,73,68,34,58,52,55,44,34,99,111,110,116,97,99,116,73,68,34,58,52,55,125]}}

I am getting this when parsing now; how can I convert this to the actual message object?:

[
  101,  44,  34, 116, 105, 109, 101,  83, 101, 110, 116,  34,
   58,  49,  54,  52,  54,  48,  55,  54,  56,  50,  48,  52,
   58,  34, 101, 101, 102, 101, 119, 102,  34,  44,  34, 117,
  115, 101, 114,  73,  68,  34,  58,  52,  55,  44,  34,  99,
]
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Just get rid of the Buffer.

    client.send(JSON.stringify(messageObj));

instead of

    client.send(Buffer.from(JSON.stringify(messageObj)));
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!