title: Flask SocketIO Error during websocket handshake: Unexpected response code: 400
body: <p>Trying to run Flask-SocketIO chat app but whenever I try to deploy it on localhost machine it always return the following error on the debugger  </p>
  
  <pre><code>"GET /socket.io/?EIO=3&amp;transport=websocket HTTP/1.1" 400
  </code></pre>
  
  <p>The chrome console return this error:</p>
  
  <pre><code>socket.io.min.js:2 WebSocket connection to 'ws://127.0.0.1:5000/socket.io/?EIO=3&amp;transport=websocket' failed: Error during WebSocket handshake: Unexpected response code: 400
  </code></pre>
  
  <p>The following are the client-side script as well as the connection string</p>
  
  <pre><code>window.onload = function(){
  
  var socket = io.connect('http://127.0.0.1:5000', {transports: ['websocket']});
  var firstConnect = true;
  var myUsername = "";
  
  sendButtonListener(socket);
  
  socket.on('connect', function(){
      socket.emit('connection event');
  });
  
  socket.on('someone connected', function(username, avatar, onlineUsers, onlineUserAvatars){
      console.log(firstConnect)
      if(firstConnect){
          myUsername = username;
          for(var c = 0; c &lt; onlineUsers.length; c++){
              console.log(onlineUsers[c])
              addUserToOnlineDiv(onlineUsers[c], onlineUserAvatars[c]);
          }
          firstConnect = false;
      }
      else{
          addUserToOnlineDiv(username, avatar);
          connectionBroadcast(username, true);
      }
  });
  
  socket.on('incoming message', function(message, author, avatar){
      if(author == myUsername){
          addMessageFromSelf(author, avatar, message);
      }
      else{
          addMessageFromOtherUser(author, avatar, message);
      }
  });
  
  socket.on('disconnect event', function(username){
      let divToRemove = "." + username + "-user-online-div";
      $(divToRemove).remove();
      connectionBroadcast(username, false)
  });
  </code></pre>
  
  <p>};</p>
  
  <p>The server-side socket handler:</p>
  
  <pre><code>@socketio.on('connection event')
  def connectionEvent():
      newUsername = session.get('username')
      newUseravatar = session.get('avatar')
      socketio.emit('someone connected', (newUsername, newUseravatar,  onlineUsers, onlineUserAvatars))
  
  @socketio.on('message')
  def handleMessage(msg):
      author = session.get("username")
      avatar = session.get('avatar')
      socketio.emit("incoming message", (msg, author, avatar))
  
  @socketio.on('disconnect')
  def disconnect():
      username = session['username']
      indexOfUser = onlineUsers.index(username)
      onlineUsers.pop(indexOfUser)
      onlineUserAvatars.pop(indexOfUser)
      session.pop(username, None)
      session.clear()
      socketio.emit('disconnect event', username)
  </code></pre>