Closed Curiouspaul1 closed 1 month ago
See the troubleshooting section of the Flask-SocketIO docs to learn how to enable detailed logs. The logs might have some clues about the problem, I can't really tell from the code alone.
will do just that and let you know how it goes, thanks a lot
Okay so I just did that, turns out i actually did this before, but I turned it off. I think the logs show that my client is able to connect but for some reason the handlers don't trigger (not sure)
There is no indication of problems in this log. I think the client isn't sending anything.
I can verify that the client works because i tried interacting with your chat app example, using the same client (postman) and it worked fine
All I can tell you is that if the client was sending anything, then the log would show it. Even if it is complete garbage, the log would show that the client is sending. Your log does not show any indication of data received.
Hmmm, I see that's troubling I'll have to do some more digging
was sending anything, then th
what about the connect event, that should be triggered automatically at least since it can at least connect , the handler for "connect" should at least be triggered, even before any thing is sent by the client. What do you think?
The connect event is the one that reads Received packet MESSAGE data 0
. There are a few in your log. You can see that the server responds with a sid
value.
Hi! Miguel, hope you're doing great, wanted to give you updates on the issue I'm having. So i decided to build a mirror of the application but progressively, instead of building the whole thing at once (this mostly involved copying and pasting files), and test each sector as i add them to this new mirror. It started out well and eventually right after i imported some celery tasks into the blueprint, the handlers stopped working and i was able to reproduce this behavior. I took a step backward and realised that it worked when i removed the celery task imports from the views and the events file. I don't know why this might be happening, but I'm at least glad i was able to figure out what was going on. I'll show my folder structure again and how i defined these celery tasks and how i used them in the blueprint so you can see them, perhaps i made a blunder with the way i used it.
projectfolder/
- core/
| - __init__.py
| - bookings (blueprint)/
| - __init__.py
| - events.py
| - views.py
| - other blueprint
| - other blueprint
- tasks/
| - __init__.py
| - task1.py
| - task2.py
- wsgi.py
This is just an addition to the folder structure i sent earlier on at the beginning, its mostly the same i just omitted some unnecessary files and subfolders but now that the tasks
folder is important i am updating it so you can get the full context.
In tasks/__init__.py
#!/usr/bin/env python
# flake8: noqa
import os
from core import celery, create_app, socketio
app = create_app(os.getenv('FLASK_CONFIG') or 'default')
app.app_context().push()
from .second_task import stask
task1.py
(this is an alias for the actual name btw)
from . import celery
from extensions import redis_
@celery.task(bind=True, name='push_booking_to_queue.pbq')
def pbq(self, booking_details):
# does some stuff
print('yes')
So back in the bookings
blueprint (in its views and events.py) i import these tasks like so
in bookings/views.py
from tasks.task1 import pbq
@bookings.route('/', methods=['POST'])
# @login_required
# @permission_required(Permission.service_request)
def create_booking():
data = request.get_json(force=True)
new_order = Booking(params=data)
# new_order.user = current_user
db.session.add(new_order)
db.session.commit()
data['booking_id'] = new_order.booking_id
init_task = pbq.apply_async([data])
return {
'status': 'success',
'msg': 'booking created successfully',
'data': {
'task_id': init_task.id
}
}
in the bookings/events.py
from tasks.task1 import pbq
@socketio.on('order_updates')
def get_updates(data):
# add client to special room
room = request.sid
# continually query celery for updates
while True:
task = pbq.AsyncResult(data['task_id'])
if task.state == 'SUCCESS':
result = task.info.get('match_profile')
send(result, to=room)
break
perhaps this is coming from the app context that celery is pushing or something (i have no idea tbh)
@Curiouspaul1 I'm going to repeat my observation from before. The client isn't sending the events that you are expecting. I don't see why the tasks can influence what the client sends, but that is what you should be looking for, as the problem is not in the server.
Please go ahead and review the log screenshot that you pasted above once again. Note the line near the top that reads:
received event "joined" from ...
This is proof that your application is able to receive events. This event is sent by the application in this repository. At the time you capture the log you must have had an instance of this application running in your browser, and it was trying to connect.
Then look at the two connections that follow in your log. Both connect successfully, but they don't send any events.
Hi Miguel hope you're doing well. So I think you misunderstood me, possibly because I didn't add the new logs from my last test to my previous message, but what I'm trying to tell you is that the application works whenever I remove the "import from the tasks module" into my events file. For more context here's the behavior of the application as I have come to see:
The client is able to send events, in the screenshot above i used the client to send an event called "test", and the client acknowledged receipt
The server's event handlers in the events.py
remain inactive or ignorant to the received requests so long as i have this line of import in the file:
from tasks.push_booking_to_queue import pbq
NOte: the tasks package contains more than one module, and importing any of these modules inside the events
module results in the same behavior.
events
file, i tried importing it locally inside of the handler(s) that require the task(s), and that also seems to work.So i can assure you that this behavior is caused by importing these tasks. Please what could be the cause of this behavior, is it because another app instance exists inside the tasks folder
So i can assure you that this behavior is caused by importing these tasks. Please what could be the cause of this behavior, is it because another app instance exists inside the tasks folder
I have no way to know, sorry. Here I have to believe you, because you haven't provided enough logs/information to confirm that this statement is accurate. Assuming you are correct, then the only way to find out is to debug the module that you are importing.
Hi @miguelgrinberg hope you're doing great, Paul here! (from the geoalchemy issue on flask-migrate a few weeks ago, big fan!). I am having some issues with getting my socketio handlers to work. I noticed a common cause that you've addressed in the past is "not importing them into your blueprint" so i have made sure to do that. My project structure is a lot like the one in this chat example, only i have more blueprints inside of a parent folder, its like this:
Here's what I have in bookings blueprint
init.py
file:I have imported the events as you can see here.
In the bookings
events.py
file i have added the following handlers:Here i anticipate that the connection handler at least works but not any of the handlers are being triggered.
Here's the app factory in
core/__init__.py
And finally the
wsgi.py
file which runs the application:I don't know if this is a consequence of putting all my blueprints in a package, because i see that your chat app example only has one blueprint which is on the same level as the
chat.py
that runs the application. Hopefully this is descriptive enough, i look forward to hearing from you, thank you!.