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

301
Vistas
Websocket is not connecting with javascript

So i'm trying to connect to a websocket server using javascript. I tried doing this with NODEJS with the following code and it worked.

const WebSocket = require('ws');
const ws = new WebSocket('<Websocket Address>', {
  origin: '<URL>'
});

ws.on('open', function open() {
  ws.send(JSON.stringify({"event":"auth","args":["<My authentication token>"]}))
  ws.send(JSON.stringify({"event":"send logs","args":[null]}))
  console.log('connected');
});

ws.on('close', function close() {
  console.log('disconnected');
});
ws.addEventListener('send logs', function (event) {
  console.log('Message from server ', event);
});
ws.on('message', function incoming(data) {
  console.log(data);
  
  }, 500);
Is there a way I can write this in plain javascript? Like in script tags? I've read that the following code should work but it's giving me this error Uncaught DOMException: "An invalid or illegal string was specified"

const ws = new WebSocket('<Websocket address>', {
  origin: '<URL>'
});

ws.on('open', function open() {
  ws.send(JSON.stringify({"event":"auth","args":["<My authentication Token>"]}))
  ws.send(JSON.stringify({"event":"send logs","args":[null]}))
  console.log('connected');
});

ws.on('close', function close() {
  console.log('disconnected');
});
ws.addEventListener('send logs', function (event) {
  console.log('Message from server ', event);
});
ws.on('message', function incoming(data) {
  console.log(data);
  
  }, 500);
Could anyone tell me what i'm doing wrong?

about 4 years ago · Juan Pablo Isaza
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