Git Product home page Git Product logo

Comments (8)

sabuhish avatar sabuhish commented on August 15, 2024

but I dont see any code you provide, it is the error not from library, it is most probably you have connections issues.

from fastapi-mqtt.

sreesoumyajajs avatar sreesoumyajajs commented on August 15, 2024

Thank you for the reply.

Please find the code which I am running .
When there is connection issues, say not able to connect after time out , I am getting the following error. Could you please tell me how to handle socket timeout issues using fastapi_mqtt library

ERROR: Application startup failed. Exiting.
ERROR:uvicorn.error:Application startup failed. Exiting.

Code:

from fastapi import FastAPI,Body
from fastapi_mqtt import FastMQTT, MQQTConfig

mqtt_config = MQQTConfig(host = mqtt_host,
port= mqtt_port,
keepalive = 60,
username= gUsername,
password= gPassword)

app = FastAPI()

app.parseThreadRunning = True

fast_mqtt = FastMQTT(config=mqtt_config)
fast_mqtt.init_app(app)

@fast_mqtt.on_connect()
def connect(client, flags, rc, properties):
pass

@fast_mqtt.on_message()
async def message(client, topic, payload, qos, properties):
pass

@app.on_event("startup")
async def startup_event():

app.parseThreadRunning = True
app.parseThread = threading.Thread(target=start_message_parsing)
app.parseThread.start()

@app.on_event("shutdown")
async def shutdown_event():

print("Entered: FastAPI shutdown event")
app.parseThreadRunning = False
if app.parseThread.is_alive():
print("Waiting for message parsing thread to stop")
app.parseThread.join()

if name == 'main':
uvicorn.run("main:app", host="0.0.0.0", port=8000,reload=True)

from fastapi-mqtt.

sabuhish avatar sabuhish commented on August 15, 2024

there is nothing I can do about it, it is more likely a connection socket error, fastapi-mqtt is not able to connect given mqtt host, this is not on the high level of the library can be solved, we are able to connect without any issues to mqtt hosts.

from fastapi-mqtt.

sreesoumyajajs avatar sreesoumyajajs commented on August 15, 2024

OK.Thank you

from fastapi-mqtt.

marvleaf avatar marvleaf commented on August 15, 2024

Hello man,
My team and I understand your sad situation (pat pat on the shoulder). After a lot of work trying to upload our code on railway we found that the problem is given by an override of "port" variable in /fastapi-mqtt/config.py
In order to solve the problem you can delete "Settings" from MQTT_Config(Settings) in /fastapi-mqtt/config.py

from fastapi-mqtt.

sabuhish avatar sabuhish commented on August 15, 2024

@marvleaf could you please clarify your response? deleting Settings should not be the solution. What is the problem with port variable?

from fastapi-mqtt.

ftapajos avatar ftapajos commented on August 15, 2024

@sabuhish I think we could look to this issue with an aditional concern: What should be the behaviour of a failed first connection to a broker? Currently it fails the entire process of FastAPI. Even though it can be assumed that an API designed to work with MQTT will be useless without MQTT, one cannot assume that an unsucessful connection to a broker means the broker will be never available, since this event may be just an instability during the time of the first try.
In my case, for example, in the development environment, the broker usually is setup after the setup of the FastAPI container, which usually yields a ConnectionRefusedError exception.

from fastapi-mqtt.

sabuhish avatar sabuhish commented on August 15, 2024

@ftapajos I understand your concern about your problem, I think implementing a health endpoint would be much better, because in this way your health endpoint of MQTT broker will depend on fastapi app, it will wait MQTT container to get up and be healthy.

Normally databases also depends on app, runs first, like redis, mongo or postgresdb they should be healthy and then the application itself should take a start.

What we can implement re-connect feature in case of failure. I need to do research about it. Closing this issue because,
it is a connection error, not library error.

from fastapi-mqtt.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.