Forums

SocketIO Error with WSGI

Hello. So I recently started using SocketIO in my facial recognition system to get the current webcam status and send the modified webcam feed (after the facial recognition process) back to the client. However, I’ve had a problem with my WSGI file. (The WSGI code is:

import sys
from flask_socketio import SocketIO as socketio
sys.path.insert(0, /home/Cryptic02/mysite)
from facialrecognition import app as application
application = socketio.WSGIApp(application)
socketio.init_app(application)

Whenever I try to use it, it gives me the error:

2023-05-31 11:21:13,800: Error running WSGI application
2023-05-31 11:21:13,810: ImportError: cannot import name 'Namespace' from 'socketio' (/home/Cryptic02/.local/lib/python3.10/site-packages/socketio/__init__.py)
2023-05-31 11:21:13,811:   File "/var/www/cryptic02_pythonanywhere_com_wsgi.py", line 2, in <module>
2023-05-31 11:21:13,811:     from flask_socketio import SocketIO as socketio
2023-05-31 11:21:13,811: 
2023-05-31 11:21:13,811:   File "/home/Cryptic02/.local/lib/python3.10/site-packages/flask_socketio/__init__.py", line 26, in <module>
2023-05-31 11:21:13,811:     from .namespace import Namespace
2023-05-31 11:21:13,812: 
2023-05-31 11:21:13,812:   File "/home/Cryptic02/.local/lib/python3.10/site-packages/flask_socketio/namespace.py", line 1, in <module>
2023-05-31 11:21:13,812:     from socketio import Namespace as _Namespace

I have uninstalled and reinstalled SocketIO but it doesn’t fix the problem. Is this a problem in my code, or does PythonAnywhere not support SocketIO? Many thanks.

[edit by admin: formatting]

It looks like you may have an incompatibility between the version of Flask-SocketIO that you're using and the version of SocketIO -- the line that is highlighted at the end of that stack trace shows that the former is trying to import something from the latter that does not exist.

However, even if you fixed that, it unfortunately wouldn't work -- we don't support SocketIO servers on PythonAnywhere right now.

Thanks for the clarification, I’ll keep that in mind when looking for an alternative.

i know you don’t support socket.IO how ever i am writing a message system in my app that unterließt the app.route flask system and i have a database that everything is being saved too. In summary, the route is meant to handle conversation-related requests. It interacts with the database to fetch relevant messages, utilizes forms to handle new messages, and ensures CSRF protection for form submissions. It finally uses these variables to generate dynamic HTML for the client. If any part of this process fails (like the missing CSRF token), Flask will raise an exception. i just need to know that i can write a messages service in my app that allows people to connect between users and items. Plasee get back to me as i have it rendering however the message is not saving to the database and not showing on the screen. I need to know if its my code or its the server hosting not allowing me to run this properly, as the message is not passing throw the server.… THANK YOU PAUL

Which database are you using -- MySQL, SQLite or Postgres? And are you getting any errors when you try to write to it?

I HOPE THIS IS THE INFORMATION THAT YOU NEED TO ASWER THE QUESTION BETTER,

. The main issue involves submitting a form to send messages between users. Here's a brief rundown of the function in question:

The application has two forms, MessageForm and SendMessageForm, both implemented via Flask-WTForms. When a POST request is made to the '/send_message/' route, the send_message function is invoked. This function validates the MessageForm, creates a new Message instance, and adds it to the session. The Message instance includes the sender ID (the current user), the recipient ID (an argument from the route), the message content (from the form data), and a timestamp. It then tries to commit the session to save the changes in the MySQL database.

In a similar vein, for the '/conversation/' route, the application uses MessageForm in a comparable manner. Messages are ordered by their timestamp and displayed.

However, despite no errors being thrown, the messages are not being saved to the MySQL database. The application seems to be operating correctly: it validates the form, creates the Message object, adds it to the session, and even the response times indicate normal operation. Nevertheless, the messages do not appear in the database or on the screen, which leads me to suspect that the db.session.commit() operation might not be succeeding.

Could you kindly confirm if PythonAnywhere has any restrictions or specific settings related to handling SQLAlchemy sessions or any other aspects that might prevent my messages from being saved to the database? If there are any such restrictions, could you please guide me on the appropriate way to configure or modify my application to ensure that it functions as expected?

I DONT KNOW IF IT IS MY CODE OR IF IT IS THE SERVER AS I KNOW THAT PYTHONANYWHERE DOESN’T ALLOW ISOCKET.IO AND I AM TRYING TO DO SOMETHING SIMILAR. SHOULD I KEEP ON TRING TO MAKE IT COMMUNICATE WITH MY USERS AND recipient OR AM I FIGHT A LOSING WAR WITH MY CODE.

Hey, that sounds like a pretty fundamental mysql app so the problem will be in your code. Check the the error log and the server log on the webapp page if you havent already. There is almost always helpful stuff in there.

You might want to connect to your db to be able to do some debugging - https://help.pythonanywhere.com/pages/AccessingMySQLFromOutsidePythonAnywhere

Also might be helpful at some point - https://help.pythonanywhere.com/pages/UsingSQLAlchemywithMySQL/

Hello i know that pythonanywhere doesn’t support web-socket like socketio how ever i have a work around now use the POLLING AS HEARTBEAT that will assess the server every X minute to retrieve the new message. i am just wondering if Pythonanywhere will have a problem with me loading the server with request every X minutes to retrieve my user messages. i don't want to start coding and find out it is not allowed. thanks you i wait you response…..

There should be no problem with that as long as you have enough web workers to manage the incoming requests: https://help.pythonanywhere.com/pages/HowManyHitsCanMySiteHandle/