1
votes

I've built simple webserver in flask according to this tutorial: https://blog.miguelgrinberg.com/post/the-flask-mega-tutorial-part-i-hello-world Then I realized that I need websockets, because I need real-time communication between client and server. So I installed in my venv flask-socketio library. Then I modified my __init__.py file so it looks like that:

from flask import Flask
from flask_socketio import SocketIO
from config import Config
from flask_sqlalchemy import SQLAlchemy
from flask_migrate import Migrate
import logging
from logging.handlers import RotatingFileHandler
import os


app = Flask(__name__)
app.config.from_object(Config)
db = SQLAlchemy(app)
migrate = Migrate(app, db)
socketio = SocketIO(app)

if not app.debug:
    if not os.path.exists('logs'):
        os.mkdir('logs')
    file_handler = RotatingFileHandler('logs/webserver.log', maxBytes=10240,
                                       backupCount=10)
    file_handler.setFormatter(logging.Formatter(
        '%(asctime)s %(levelname)s: %(message)s [in %(pathname)s:%(lineno)d]'))
    file_handler.setLevel(logging.INFO)
    app.logger.addHandler(file_handler)

    app.logger.setLevel(logging.INFO)
    app.logger.info('Reaction test startup')

from app import routes, models, errors

if __name__ == '__main__':
    socketio.run(app)

The problem is that now when I try to run my server using flask run, I get warning:

WARNING in __init__: Flask-SocketIO is Running under Werkzeug, WebSocket is not available.

I saw other people had this problem too (https://github.com/miguelgrinberg/Flask-SocketIO/issues/894), and that I should run my __init__.py script instead of flask run. However, when I try to start my server with "python app/__init__.py", my imports are failing, feels like all dependencies are lost. At first I received message:

Traceback (most recent call last): File "app/init.py", line 3, in from config import Config

So I decided to basically rewrite whole config file into __init__.py script. This didn't help though, as after doing it i received message:

File "app/init.py", line 37, in from app import routes, models, errors ModuleNotFoundError: No module named 'app'

So I guess rewriting contents of Config.py file wasn't the problem solver. My question is, how should I run my app so that it supports websockets properly without rewriting it from scratch?

1
Are you using Gevent or eventlet? For developmet the docs mention you need to start your application with flask run instead of python app.py flask-socketio.readthedocs.io/en/latestLucas Scott
I use eventlet, but I actually solved my problem already, I just should have added socketio.run(app) line into my python script which was assigned to FLASK_APP (server.py) enviromental variable instead of "init.py" file. Then i should have ran python server.py and everything works just fine.Mentos1105

1 Answers

1
votes

I solved my problem already. The problem was I tried to edit my __init__.py file instead of the one which was bound to FLASK_APP environmental variable. When I added:

if __name__ == '__main__':
    socketio.run(app)

to my FLASK_APP script and then ran this other script from console with python server.py (this is the name of my script bound to FLASK_APP variable) everything worked just fine.