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

111
Views
Authorize websocket connection with ws.js

I'm currently trying to write a websocket connector between the server and one exclusive client for real-time information exchange between the two using ws.js. I would like that the server automatically asks the client for authentication what i have looks like this:

const ws = require("ws");
const pass = "1234"

class WebsocketHandler {
    constructor(port = 8080) {
        this.server = new ws.Server();
        this.server.on("connection", this.startHandleConnection.bind(this));
    }
    startHandleConnection(wsc) {
        this.wsc = wsc;
        //Authorize the client with a password before accepting the connection
        wsc.on("message", this.handleMessage.bind(this));
        wsc.send("Authorize"); // The client-side is expected to respond with the password
    }
    handleMessage(message) {
        if(!this.authorized && message !== pass) {
           delete this.wsc;
        } else {
           //do stuff
        }
    }
}

Is there a more elegant approach to this?

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!