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

300
Views
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
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!