title: WebSocket (Socket.io) connection Error - failed: Connection closed before receiving a handshake response
body: <p>I have spent all day figuring out and searching for solution online for error</p>
  <blockquote>
  <p>WebSocket connection to 'wss://localhost/myapp/peerjs?key=peerjs&amp;id=5c70da87-62c1-41dd-b1b1-e7aea5acc09b&amp;token=j4n0nprnu6' failed: Connection closed before receiving a handshake response</p>
  </blockquote>
  <p>I am developing this nodejs app on express server. My express server where I initialize PeerServer along with it:</p>
  <pre><code>// Dependencies
  const express = require('express')
  const app = express()
  const httpPort = process.env.PORT || 80
  const httpsPort = 443
  const { ExpressPeerServer } = require('peer')
  const path = require('path')
  const http = require('http')
  const https = require('https')
  const fs = require('fs')
  
  // Certificate &amp; credentials
  const privateKey = fs.readFileSync(path.join(__dirname, 'certs', 'key.pem'))
  const certificate = fs.readFileSync(path.join(__dirname, 'certs', 'cert.pem'))
  const credentials = {
          key: privateKey,
          cert: certificate
  }
  
  const mainServer = http.createServer(app).listen(httpPort, () =&gt; { console.log('Main Server listening to port ' + httpPort) })
  const httpsServer = https.createServer(credentials, app).listen(httpsPort, () =&gt; { console.log('Peer Server listening to port ' + httpsPort) })
  
  const peerServer = ExpressPeerServer(mainServer, {
          debug: true,
          path: '/myapp'
  })
  
  //app.use('peerjs', peerServer)
  app.use(peerServer)
  
  const io = require('socket.io')(httpsServer)
  const { v4: uuidV4 } = require('uuid')
  
  app.set('view engine', 'ejs')
  app.use(express.static('public'))
  
  app.get('/', (req, res) =&gt; {
    res.redirect(`/${uuidV4()}`)
  })
  
  app.get('/:room', (req, res) =&gt; {
    res.render('room', { roomId: req.params.room })
    //console.log('Room Created / Joined')
  })
  
  io.on('connection', (socket) =&gt; {
      //console.log('IO Connectedd')
      socket.on('join-room', (roomId, userId) =&gt; {
          console.log(roomId, userId)
          socket.join(roomId)
          socket.to(roomId).emit('user-connected', userId)
  
          socket.on('disconnect', () =&gt; {
              socket.to(roomId).emit('user-disconnected', userId)
          })
      })
  })
  </code></pre>
  <p>And below is my client side code</p>
  <pre><code>const socket = io('/')
  const videoGrid = document.getElementById('video-grid')
  const myPeer = new Peer(undefined, {
      host: '/',
      port: '443',
      path: '/myapp'
  })
  const ownVideo = document.createElement('video') // Own Video
  ownVideo.muted = true
  const peers = {}
  navigator.mediaDevices.getUserMedia({
      video: true,
      audio: true
  }).then(stream =&gt; {
      addVideoStream(ownVideo, stream)
  
      myPeer.on('call', call =&gt; { // On Receiving Other Persons Call
          call.answer(stream) // Send Video Stream On Answer
          const video = document.createElement('video')
          call.on('stream', userVideoStream =&gt; { // Send Back Video Stream On Stream
              addVideoStream(video, userVideoStream)
          })
      })
  
      socket.on('user-connected', userId =&gt; { // Allow Self To Be Connected To Others
          console.log('User Connected: ' + userId)
          connectToNewUser(userId, stream)
      })
  })
  
  socket.on('user-disconnected', userId =&gt; {
      if (peers[userId]) peers[userId].close()
  })
  
  myPeer.on('open', id =&gt; {
      socket.emit('join-room', ROOM_ID, id)
  })
  
  function connectToNewUser(userId, stream) {
      const call = myPeer.call(userId, stream) // We Connect To Other User
      const video = document.createElement('video')
      call.on('stream', userVideoStream =&gt; { // Other User Connects To Us
          addVideoStream(video, userVideoStream)
      })
      call.on('close', () =&gt; { // Other User Disconnects
          video.remove()
      })
  
      peers[userId] = call
  }
  
  function addVideoStream(video, stream) {
      video.srcObject = stream
      video.addEventListener('loadedmetadata', () =&gt; {
          video.play()
      })
      videoGrid.append(video)
  }
  </code></pre>
  <p>I am new to nodejs and couldn't find any solution online for it.</p>
  <p>Please help!!</p>