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

145
Views
Node websockets missing reason in close event

I'm developing a websocket server with Node.Js v14.17.3 using "ws": "^7.4.5" package. My server code is this:

const WebSocket = require("ws");

const url = "localhost";
const port = 8081;

const wss = new WebSocket.Server({ host: url, port });

wss.on("connection", function (ws) {
  ws.on("message", function (data) {
    console.log("received: %s", data);
  });

  function onSocketClose(code, reason) {
    console.log("Closing open web socket", code, reason);
    console.log("Closed web socket.");
  }

  ws.on("close", onSocketClose);
});

My client code is this:

window.onclose = function (event) {
  console.log("Window closing.");
  closeSocket("Window closed");
};

window.onunload = function (event) {
  console.info("Window unloaded");
  closeSocket("Window unloaded");
};

function closeSocket(reason) {
  if (reason) {
    console.info("Closing socket due to:", reason);
  } else {
    reason = "Unknown reason";
  }

  webSocket.onclose = function (ev) {
    console.info("Websocket closed.");
  };

  webSocket.close(1001, reason);
}

In server on("close", ...) callback I get an empty reason, while I'm expecting the reason I've specified in client method.

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!