Search code examples
pythonflaskcpanelpassengerflask-socketio

problems with sockets on cpanel flask application


i have a flask application on cpanel.

from flask_socketio import SocketIO, emit

from flask_cors import CORS
CORS(app)

app.config['DEBUG'] = True

import eventlet
eventlet.monkey_patch()

socketio = SocketIO(app, async_mode='eventlet', cors_allowed_origins="*")

@socketio.on('message')
def handle_message(message):
    socketio.emit('message', message)
<main class="container">
    <div id="messages"></div>
    <input type="text" name="message" id="message" placeholder="Enter your message">
    <button onclick="send()">Send</button>
</main>
<script>
    const socket = io.connect('https://www.name.com:443');

    socket.on('message', (message) => {
        const ul = document.getElementById('messages');
        const li = document.createElement('li');
        
        li.appendChild(document.createTextNode(message));
        ul.appendChild(li);
    });

    const send = () => {
       const message = document.getElementById('message');

        socket.emit('message', message.value);
        document.getElementById('message').value = '';
    }
</script>

it is running from passenger_wsgi.py

from app import app as application

the browser console says

Firefox can’t establish a connection to the server at wss://www.name.com/socket.io/?EIO=4&transport=websocket&sid=pF1NGCYlzyJPgkpDAAAF

& the logs say

You need to use the eventlet server. See the Deployment section of the documentation for more information.

i think my website is using ngnix & i cant find the configuration file. it is hiding it from me so nothing dangerous happens

i have a ssl certificate & the app runs fine in my localhost


Solution

  • I suggest reading this document carefully -> https://flask-socketio.readthedocs.io/en/latest/deployment.html

    If you are using a Passenger web server, unfortunatelly it is supported by Flask-SocketIO. Please see the answer from Miguel, author of Flask-SocketIO https://github.com/miguelgrinberg/Flask-SocketIO/issues/760 some time ago on this matter.

    You would need to choose between specific deployment options mentioned in the documentation - uWSGI, Gunicorn, or an embedded server.

    I hope it helps!