Firefox can't establish a websocket connection while Chrome can

16,079

I, finally, found a solution.

The problem was that for https connections, in port 443, Firefox had already stored an exception for unknown certificate while it needed another exception for wss (port 1337 in this case).

I've added a certificate exception, in advanced preferences, for this port and now works fine.

Share:
16,079
francadaval
Author by

francadaval

Updated on July 20, 2022

Comments

  • francadaval
    francadaval almost 2 years

    I'm developing a webapp and I'm including websocket connectivity. I've installed a websocket server with node.js (5.0.0) with websocket (https://www.npmjs.com/package/websocket).

    In Chrome it works perfectly but in Firefox this message appears in console:

    Firefox no puede establecer una conexión con el servidor en wss://www.my-dev-server.com:1337/.
    

    (Firefox can't establish a connection with server at...)

    This is the server code (basically as in examples):

    var WebSocketServer = require('websocket').server;
    
    var https = require('https');
    var fs = require('fs');
    
    var options = {
      key: fs.readFileSync('/keyfile.key'),
      cert: fs.readFileSync('/pemfile.pem')
    };
    var port = 1337; 
    
    // Create HTTPS service.
    var server = https.createServer(options, function(request, response) {
        console.log((new Date()) + ' Received request for ' + request.url);
        response.writeHead(404);
        response.end();
    });     
    
    server.listen(port, function() {
        console.log((new Date()) + ' Server is listening on port ' + port);
    });
    
    // create the server
    wsServer = new WebSocketServer({
        httpServer: server,
        autoAcceptConnections: false
    });
    
    function originIsAllowed( origin ) {
        // TODO: complete
        return true;
    }
    
    var clients = [];
    
    // WebSocket server
    wsServer.on('request', function(request) {
    
        if( !originIsAllowed( request.origin ) ) {
            request.reject();
            console.log((new Date()) + ' Connection from origin ' + request.origin + 'rejected.');
            return;
        }
        console.log((new Date()) + ' Connection accepted from ' + request.origin + '.');
    
        var connection = request.accept(null, request.origin);
        clients.push(connection);
    
        connection.on('message', function( message ) {
            if (message.type === 'utf8') {
                console.log('Received Message: ' + message.utf8Data);
            }
            else if( message.type === 'binary' ) {
    
            }
        });
    
        connection.on( 'error', function( error ) {
        });
    
        connection.on('close', function( reasonCode, description ) {
            console.log((new Date()) + " Peer " + connection.remoteAddress + " disconnected.");
        });
    });
    

    I use a self-signed certificate for development purposes, the same that is used by the web server.

    This is my client code:

    var connection = new WebSocket('wss://www.my-dev-server.com:1337');
    
    connection.onopen = function () { };
    
    connection.onerror = function (error) { };
    
    connection.onmessage = function (message) {
        /* some code here */
    };
    
  • mayank
    mayank over 7 years
    any programmatical way to do this in node.js
  • mayank
    mayank over 7 years
    I am facing this issue in firefox in my uat phase , chrome and other browser working fine
  • francadaval
    francadaval over 7 years
    No, it's about browser configuration. You have to use a trusted certificate or config the browser to allow the self-signed certificate for your websocket URL.
  • mayank
    mayank over 7 years
    okay. I have handled it using nginx proxy_pass it worked for me :)
  • Esqarrouth
    Esqarrouth about 4 years
    @mayank how did you do this with nginx proxy_pass?
  • Esqarrouth
    Esqarrouth about 4 years
    @francadaval does that mean each firefox browsers have to do a manual operation to allow self-signed certificate?