Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

289
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda