0

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.

Lucy865
  • 15
  • 6

1 Answers1

0

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.

Miguel Grinberg
  • 65,299
  • 14
  • 133
  • 152
  • Hi Miguel, thanks a lot for your answer. So if it was possible, what would be the advantages of having Apache pass connections on to the SocketIO server compared to the configuration that I have? – Lucy865 Apr 05 '20 at 10:38
  • Well for starters anybody could connect to your port 5000 bypassing Apache, so any security settings such as SSL certificates would need to be applied on two servers instead of one. The solution that I recommend, based on nginx, has only nginx as a public web server. For connections intended to the Flask-SocketIO server nginx acts as intermediary and can handle SSL for you. – Miguel Grinberg Apr 05 '20 at 12:02
  • Please excuse me if I am confusing two different things, but I thought that the line `io = SocketIO(app, cors_allowed_origins="http://localhost")` ensured that only connections from the same computer were allowed. Is it insecure anyway? – Lucy865 Apr 05 '20 at 12:39