WebSocket connection to ‘ws://(server ip)/socket.io/?EIO=4&transport=websocket&sid=52ViP_GqPGSr4DkqAAAN’ failed:

I have been trying to figure out this error for a few days. When I try to emit a socket from the client side (JS) to the server (Python – flask) I get the error of connection failed. However, this error isn’t always consistent. If the error does happen, it will normally happen on the first time I try to emit the socket and it normally works after that. Also, I am having some trouble with mobile devices receiving events as well. I also have an nginx reverse proxy on my server as well.

Client side:

function formsubmitted(){
        var name = document.getElementById("startexampleInputEmail1").value
        var pin = document.getElementById("startexampleInputGamePin").value
        var predictions = document.getElementById("startexampleInputPredictions").value
        var joinmsg = document.getElementById("startexampleInputJoinMsg").value

        socket.emit("AccountCreation",[socket.id,name,pin,predictions,joinmsg])



    }

Server side:

@socketio.on("AccountCreation")
def handle_new_acc(data):
    sid = data[0]
    name = data[1]
    pin = data[2]
    predictions = data[3]
    joinmsg = data[4]
    canCreateAcc = True
    gameExists = False
    for user in User.query.all():
        if user.name == name:
            canCreateAcc = False
            emit("UsernameTaken",to=sid)
            return
    for game in Game.query.all():
        if game.pin == pin:
            gameExists = True

    if gameExists == False:
        emit("GameNotFound",to=sid)


    if gameExists == True and canCreateAcc == True:
        usertoadd = User(name,pin,predictions,joinmsg,1,"0 seconds","0 seconds",0,"")
        db.session.add(usertoadd)
        db.session.commit()
        game = Game.query.filter_by(pin=pin).first()
        player_list = json.loads(game.players)
        player_list.append(name)
        game.players = json.dumps(player_list)
        db.session.commit()
        emit("AccountReady",[name,pin,predictions,joinmsg],to=sid)