Flask SocketIO server implementation is not receiving client messages or event triggers when starting the Flask application (as per socketio.run(app)) using the flask command: flask run (on http://127.0.0.1:5000).
It is only outputting GET and POST polling:
127.0.0.1 - - [04/Jan/2022 13:18:26] "GET /socket.io/?EIO=4&transport=polling&t=Nub1htW HTTP/1.1" 200 -
127.0.0.1 - - [04/Jan/2022 13:18:26] "POST /socket.io/?EIO=4&transport=polling&t=Nub1i2I&sid=ELpMQjSfqpcXN731AAAA HTTP/1.1" 200 -
127.0.0.1 - - [04/Jan/2022 13:18:26] "GET /socket.io/?EIO=4&transport=polling&t=Nub1i2J&sid=ELpMQjSfqpcXN731AAAA HTTP/1.1" 200 -
Project structure:
├── Flask Web App
│ ├── app
│ │ ├── common
│ │ ├── distributed_manager
│ │ │ ├── distributed_manager.py
│ │ │ └── __init__.py
│ │ ├── __init__.py
│ │ ├── local_settings.py
│ │ ├── models
│ │ ├── settings.py
│ │ ├── static
│ │ │ ├── css
│ │ │ ├── img
│ │ │ └── js
│ │ │ ├── chat.js
│ │ │ └── responses.js
│ │ ├── templates
│ │ ├── util.py
│ │ └── views
│ │ ├── bot_views.py
│ │ ├── error_views.py
│ │ ├── __init__.py
│ │ ├── main_views.py
│ │ └── site_views.py
│ ├── flask_app.py
The app is initialised in flask_app.py by calling a function create_app() defined in app/__init__.py. This function returns a tuple containing the app object and socketio object, to be used to combine these two in the following way (in flask_app.py):
from app import create_app
app, socketio = create_app()
if __name__ == "__main__":
socketio.run(app)
I have managed to handle messages from client by placing the event handle functions in flask_app.py in the following way:
from app import create_app
app, socketio = create_app()
@socketio.on("connect")
def handle_connection():
print("Connected a client...")
@socketio.on("message")
def handle_message(msg):
print("Client message: " msg)
if __name__ == "__main__":
socketio.run(app)
But for separation of concern reasons and to decouple the app as much as possible I would like to place the server (EDIT: handle functions of the server) in the distributed_manager.py, because later I plan to pass the messages as requests in this module.
This is my server (distributed_manager.py):
from flask_socketio import send, emit
from flask_app import socketio
def ack_client():
print("Message was received by the client.")
@socketio.on("connect")
def handle_connection(message):
print("connected: " message)
@socketio.on("message")
def handle_message(message):
print(f"Received message: {message}")
And client in chat.js:
var socket;
connectToSocket();
...
function connectToSocket(){
socket = io.connect('http://' document.domain ':' location.port);
socket.on('connect', function() {
socket.send('User has connected!');
});
// console.log("Connected to the socket.")
}
CodePudding user response:
So, I have connected my two brain cells and figured out that my distributed_manager module is executed after socketio.run(app) call and after creating the app so the @socketio.on... functions must be defined before it, specifically in my solution in app/__init__.py, which creates the app and socketio objects.
