title: Sockets.io issue: WebSocket is closed before the connection is established
body: <p>I am currently building a nodejs webrtc video conference, hosted on azure. The program works perfectly fine locally, but when hosted there seems to be an issue with websockets. The client side error is below:</p>
  <pre><code>WebSocket connection to '&lt;URL&gt;' failed: WebSocket is closed before the connection is established.
  </code></pre>
  <pre><code>index.js:83 WebSocket connection to 'wss://etuition.azurewebsites.net:8080/socket.io/?EIO=3&amp;transport=websocket' failed: WebSocket is closed before the connection is established.
  </code></pre>
  <p>I have seen other stack overflow posts claiming that this is due to SSL, but site is currently running on  HTTPS so this should not be a problem for me. Is it possible that the ws server is insecure even though my http server is secure?</p>
  <p>Any other advice on what could be the problem will be greatly appreciated.
  Below I have included my server.js code.
  Please ask if any more information is needed:</p>
  <pre class="lang-js prettyprint-override"><code>/**
   * Server module.
   *
   *
   */
  
  'use strict';
  
  var environment = process.env.RTC_ENV || 'local';
  var debug = require('debug')('expressapp:server');
  
  var express = require('express');
  var cors = require('cors');
  const http = require('http');
  var logger = require('./logger').logger(environment);
  
  
  var serverPort = normalizePort(process.env.PORT || '8080');
  //var serverPort = normalizePort(process.env.PORT || '8080');
  
  
  function normalizePort(val) {
      var port = parseInt(val, 10);
    
      if (isNaN(port)) {
        // named pipe
        return val;
      }
    
      if (port &gt;= 0) {
        // port number
        return port;
      }
    
      return false;
    }
    
  //var serverPort = process.env.RTC_PORT || 31000
  var serverIpAddress = process.env.RTC_IP || 'localhost'
  var  socketIoServer = 'etuition.azurewebsites.net' + ':' + serverPort;
  
  ////////////////////////////////////////////////
  // SETUP SERVER
  ////////////////////////////////////////////////
  var app = express();
  
  app.set('port', serverPort);
  
  
  function redirectSec(req, res, next) {
      if (req.headers['x-forwarded-proto'] == 'http') {
          var redirect = 'https://' + req.headers.host + req.path;
          console.log('Redirect to:' + redirect);
          res.redirect(redirect);
      } else {
          return next();
      }
  }
  
  app.use(redirectSec);
  
  require('./router')(app, socketIoServer, environment);
  
  // Static content (css, js, .png, etc) is placed in /public
  app.use(express.static(__dirname + '/public'));
  
  app.use(cors());
  
  // Location of our views
  app.set('views', __dirname + '/views');
  
  // Use ejs as our rendering engine
  app.set('view engine', 'ejs');
  
  // Tell Server that we are actually rendering HTML files through EJS.
  app.engine('html', require('ejs').renderFile);
  
  const server = http.createServer(app);
  
  server.listen(serverPort);
  server.on('listening', onListening);
  
  
  function onListening() {
      var addr = server.address();
      var bind = typeof addr === 'string'
        ? 'pipe ' + addr
        : 'port ' + addr.port;
      debug('Listening on ' + bind);
      logger.info(&quot;Socket IO Address:&quot; + socketIoServer);
      logger.info(&quot;Server IP Address:&quot; + serverIpAddress);
      logger.info('Server running on port ' + serverPort);
    } 
  
  var io = require('socket.io').listen(server, { log: false, origins: '*:*' });
  
  ////////////////////////////////////////////////
  // EVENT HANDLERS
  ////////////////////////////////////////////////
  
  
  io.sockets.on('connection', function (socket) {
  
      function log() {
          var array = [&quot;&gt;&gt;&gt; Message from server: &quot;];
          for (var i = 0; i &lt; arguments.length; i++) {
              array.push(arguments[i]);
          }
          socket.emit('log', array);
      }
  
      socket.on('message', function (message) {
          log('Got message: ', message);
          logger.info(&quot;message: &quot;, message);
          socket.broadcast.to(socket.room).emit('message', message);
      });
  
      socket.on('create or join', function (message) {
          var room = message.room;
          socket.room = room;
          var participantID = message.from;
          configNameSpaceChannel(participantID);
  
          io.of('/').in(room).clients(function (error, clients) {
              var numClients = clients.length;
  
              log('Room ' + room + ' has ' + numClients + ' client(s)');
              log('Request to create or join room', room);
  
              if (numClients == 0) {
                  logger.info(participantID + &quot; joined first. Creates room &quot; + room);
                  socket.join(room);
                  socket.emit('created', room);
              } else {
                  logger.info(participantID + &quot; joins room &quot; + room);
                  io.sockets.in(room).emit('join', room);
                  socket.join(room);
                  socket.emit('joined', room);
              }
          })
      });
  
      // Setup a communication channel (namespace) to communicate with a given participant (participantID)
      function configNameSpaceChannel(room) {
          var nsp = '/' + room;
          var socketNamespace = io.of(nsp);
  
          logger.info('ConfigNameSpaceChannel:' + nsp);
  
          socketNamespace.on('connection', function (socket) {
              socket.on('message', function (message) {
                  // Send message to everyone BUT sender
                  socket.broadcast.emit('message', message);
              });
  
          });
  
          return socketNamespace;
      }
  
  });
  </code></pre>