Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

socketio.emit with callback doesn't work outside event context #1054

Closed
FedeParola opened this issue Sep 9, 2019 · 2 comments
Closed

socketio.emit with callback doesn't work outside event context #1054

FedeParola opened this issue Sep 9, 2019 · 2 comments
Assignees
Labels

Comments

@FedeParola
Copy link

socketio.emit with a callback raises an error if called outside a SocketIO event handler.
If I call it in a normal route handler, for example:

@app.route('/test', methods=['GET'])
def test():
    def ack(data):
           print(data)
    socketio.emit('hello', 'hello', room=client_sid, callback=ack)

it throws error

File "C:\Program Files (x86)\Python37-32\lib\site-packages\flask_socketio\__init__.py", line 408, in emit
    sid = flask.request.sid
File "C:\Program Files (x86)\Python37-32\lib\site-packages\werkzeug\local.py", line 348, in __getattr__
    return getattr(self._get_current_object(), name)
AttributeError: 'Request' object has no attribute 'sid'

For now I have resolved setting request.sid to the session id of the client I want to send my event to (same as the room), but I don't know if this could cause problems.

@miguelgrinberg
Copy link
Owner

This is actually a bug. When the emit is done outside of an event handler the callback also needs to be invoked w/o a context.

@miguelgrinberg miguelgrinberg self-assigned this Sep 9, 2019
@EvanBldy
Copy link

I have the same problem, I fix this in my code with request.sid=the sid.
I'm interested in a fix for this.
Cordially

htouvet added a commit to tranquilit/WAPT that referenced this issue Nov 18, 2019
…tIO#1054

AttributeError: 'Request' object has no attribute 'sid'

* be sure db is closed before trying to open it (for dev mode)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants