I have successfully created a Flask-SocketIO application that runs on eventlet server. Then I moved the webpage to Apache web server. I just deleted the part of myapp.py where template was rendered and placed the index.html file in Apache's /www/html directory. Here is the code:
myapp.py:
from flask import Flask
from flask_socketio import SocketIO, emit
import eventlet
eventlet.monkey_patch()
app = Flask(__name__)
app.config['SECRET_KEY'] = 'secret!'
io = SocketIO(app, cors_allowed_origins="http://localhost")
clients = []
@io.on('connected')
def connected():
clients.append(request.sid)
print("client connected")
print(request.sid)
@io.on('disconnect')
def disconnect():
clients.remove(request.sid)
print("client disconnected")
print(request.sid)
if __name__ == '__main__':
io.run(app, host='localhost', port=5000)
index.html:
<html>
<br>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
<script src="//cdnjs.cloudflare.com/ajax/libs/socket.io/2.2.0/socket.io.js" integrity="sha256-yr4fRk/GU1ehYJPAs8P4JlTgu0Hdsp4ZKrx8bDEDC3I=" crossorigin="anonymous"></script>
</head>
<br>
<body>
<h2>HELLO</h2>
<script>
$("document").ready(function(){
var socket = io.connect('http://localhost:5000');
socket.on('connect', function() {
socket.emit('connected');
});
});
</script>
</body>
</html>
Everything works just fine. But then I read Miguel's answer to this question: Using eventlet to manage socketio in Flask, which says that using Apache's web server is not a good idea. I do not understand very well how web servers work. Is what I did a wrong thing to do? The answer also says that Apache does not support web sockets. How come my application works at all, then? I'll be very thankful if somebody can explain this matter to me.
Your example works because you are only serving your HTML file through Apache, the Flask-SocketIO server is still running by itself on port 5000.
What is currently difficult/impossible to do is to make the Socket.IO connections pass through Apache and then on to the server, which is what most people prefer.
You are really running two web servers, Apache on ports 80/443 for your HTML and Flask-SocketIO on port 5000 for your Python logic.