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

309
Views
Chrome WebSocket connection closes immediately

I have been trying to setup a wss server using nodejs, and have encountered a problem when trying to connect to it using chrome. The problem still occurs with all extensions disabled and in an incognito window so I've ruled that out as the problem.

When trying to connect using chrome, I get the error:

WebSocket connection to 'wss://www.domain-name.com/' failed:

with no reason given. On the server, socket.on('close') is called immediately with description "Connection dropped by remote peer" The close event has wasClean = false. This error does not occur when connecting from safari and Firefox so I'm not really sure where to look to see what's causing it. It's running on AWS Lightsail, and through an Apache proxy server.

The client code:

var socket = new WebSocket("wss://www.domain-name.com", 'JSON')
socket.onopen = function (event) {
    console.log('open');
    socket.send('socket opened')};

socket.onclose = function (event) {
    console.log(event)};

socket.onmessage = function(message) {
    console.log('receiving message from server...')};

And the server code:

const WebSocketServer = require('websocket').server;
app = express()
var server = app.listen(3000, () => {
    console.log('Server started');
});

app.use(express.static('public'));

var wsServer = new WebSocketServer({
    httpServer: server
});

wsServer.on('request', function(request){
    console.log('New connection');
    var connection = request.accept(null, request.origin);

    connection.send('welcome from server...');

    connection.on('message', function(message){
        console.log(message)};

    connection.on('close', function(reasonCode, description) {
        console.log('disconnecting', reasonCode, description);
        });
});

I also got the same error before switching to a secure WebSocket server. Any help would be appreciated, I've run out of places to look and ways to try and get more information to help out what the problem is.

EDIT: it seems to work on chrome on my phone, but not on chrome on my friends phone?

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

The problem was not specifying the protocol when accepting the connection. After about 20 hours working on the same bug and implementing an SSL certificate to get it to work, I changed:

request.accept(null, request.origin);

to:

request.accept('json', request.origin);

For some reason the chrome gives a really unhelpful error message. Microsoft edge the same error occurs, but gives a much more helpful error message so I could work out what was going on.

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