0

I have problem to implement my Flask-RESTful application with Flask-SocketIO. Any ideas how to configure both? Here is my code:

from decouple import config
from flask import Flask
from flask_cors import CORS
from flask_migrate import Migrate
from flask_restful import Api

from db import db
from resources.routes import routes


class DevApplicationConfiguration:
    DEBUG = True
    TESTING = True
    SQLALCHEMY_DATABASE_URI = (
        f"postgresql://{config('DB_USER')}:{config('DB_PASSWORD')}"
        f"@{config('DB_HOST')}/{config('DB_NAME')}"
    )


def create_app(config="config.DevApplicationConfiguration"):
    app = Flask(__name__)
    app.config.from_object(DevApplicationConfiguration)
    migrate = Migrate(app, db)
    CORS(app)
    api = Api(app)
    [api.add_resource(*r) for r in routes]
    return app

  • Please edit your question to indicate what is the problem you are having. It's impossible to help you if you don't share all the information that you have. – Miguel Grinberg Aug 20 '23 at 10:30
  • The problem s that I can not configure both, I just need working configuration for working together Flask-RESTful and Flask-SocketIO. Then I will implement it in my code. – Mr. Terminix Aug 21 '23 at 10:24
  • The code that you shared does not show your attempt at running both extensions together. The two extensions have nothing to do with each other, so my guess is that the problem you are having is caused by something wrong you are doing yourself. If you don't show what you have tried and describe the problem I cannot really help you. – Miguel Grinberg Aug 21 '23 at 10:49
  • Actually I found solution, maybe I was tired these days. – Mr. Terminix Aug 22 '23 at 05:25

1 Answers1

0

Actually in that day I can see that my brain got stackoverflow.. After revisiting of the code, I was able to implement Flask-RESTful and Flask-SocketIO together. Here is the code:

main.py

from config import create_app
from db import db
from flask import request


app, socketio = create_app()
app.config["INITIALIZED"] = False 

db.init_app(app)

@socketio.on('user_id')
def user_id(data):
    print(f"User {data} have been connected")
    print(request.sid)


@app.before_request
def init_request():
    if not app.config["INITIALIZED"]:
        db.create_all()
        app.config['INITIALIZED'] = True


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

config.py:

from decouple import config
from flask import Flask
from flask_cors import CORS
from flask_migrate import Migrate
from flask_restful import Api
from flask_socketio import SocketIO

from db import db
from resources.routes import routes


class DevApplicationConfiguration:
    DEBUG = True
    TESTING = True
    SQLALCHEMY_DATABASE_URI = (
        f"postgresql://{config('DB_USER')}:{config('DB_PASSWORD')}"
        f"@{config('DB_HOST')}/{config('DB_NAME')}"
    )
    SECRET_KEY = "secret!"


def create_app(config="config.DevApplicationConfiguration"):
    app = Flask(__name__)
    app.config.from_object(DevApplicationConfiguration)
    migrate = Migrate(app, db)
    CORS(app)
    api = Api(app)
    [api.add_resource(*r) for r in routes]
    socketioapp = SocketIO(app, cors_allowed_origins='*')
    return app, socketioapp