Replies: 3 comments 8 replies
-
Your logs show the "Server initialized for gevent" line twice. This suggests to me that you have two |
Beta Was this translation helpful? Give feedback.
-
Thank you for your response Miguel; good pickup. I tried commenting out this line in my socketio = SocketIO(logger=True, engineio_logger=True, async_mode='gevent') To see if there was another Then when running ImportError: cannot import name 'socketio' from 'main_app' (/project-folder/main_app/__init__.py)
) Which suggests to me that if there is another Is there any other way to test this? |
Beta Was this translation helpful? Give feedback.
-
Sorry, that was a typo. As you mention, I meant to say there were still two "Server initialized" entries.
Yes, that makes sense. I tried to further this line of troubleshooting at the end of my last reply. I thought the last code block where the print statement in the
All good - I'm still fairly new to this, so I like to err on the side of naivete.
Sure, if that will make things easier. Now... to copy/paste the app without breaking everything I love... one moment 🙃 |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
Hey, thank you for taking the time to read this.
I've been experiencing an issue where messages emitted from my server using Flask-SocketIO aren't being received by the client side. I've spent the last couple of days troubleshooting and I'm a bit stuck, so I'm hoping someone can point me in the right direction.
The specific problems I'm facing are:
I've tried several steps to resolve the issue:
eventlet
togevent
.gunicorn
andgevent
.Despite these changes, the problem still persists.
I've tried to keep the information contained in the files below to only what is potentially relevant to my issue.
I'm running
Python 3.9.16
with the following (potentially) relevant dependencies:(note that I was having issues with
eventlet
so swapped togevent
).In
__init__.py
:And within
main_app.py
:(just trying to catch anything from the frontend).
Lastly, on the frontend:
In reading the documentation, it notes that:
Since
flask run
was my typical way of running the app locally, I had to adapt to this.I tried two options:
gunicorn
andgevent
viagunicorn -k geventwebsocket.gunicorn.workers.GeventWebSocketWorker -w 1 main_app:'create_app()'
For (1), I first had to reconfigure the direct running of the app to avoid circular imports.
To keep
flask run
running as-is, as well as my HerokuProcfile
asweb: flask db upgrade; gunicorn main_app:'create_app()', I created a new
run.py` file in the project root directory, as below:(note that this defaults to port 8000 for simplicity, due to using this port in authentication callbacks).
Now when I run
python run.py
, the app (seemingly) runs as it would if I usedflask run
- except now, hopefully, it supports Web Sockets.Here's the terminal logs:
The browser only logs 'Socket connected', and nothing else.
To be clear, here is what I'm expecting to happen:
On the server side, when a client successfully connects to the server, I expect to see the 'Client connected' message being printed on the server console. However, this is not currently happening.
On the client side, I expect to see 'Socket connected' (which I do) and a subsequent message logged, "Received my response event" with data payload as soon as the socket connection is established. This is due to the fact that on connection, I'm emitting a 'my response' message from the server. Despite this, I don't see any message logged in the console apart from 'Socket connected'.
Furthermore, I added
socketio.emit('my response', {'data': 'Connected!'})
to myindex
view in an attempt to have another message sent to the client as soon as the index route is accessed. I expected to see "Received my response event" with a data payload on the client-side console when accessing the index route, but nothing is logged.In summary, my main question is: Why are the messages sent from my server using Flask-SocketIO not being received on the client side? And, why isn't the 'Client connected' message being printed on the server-side console when a client successfully connects?
I appreciate any insight or suggestions on how to troubleshoot this issue further.
Thank you very much for taking the time to read this.
Beta Was this translation helpful? Give feedback.
All reactions