diff --git a/flask_socketio/__init__.py b/flask_socketio/__init__.py index 5290edfe..f86832f6 100644 --- a/flask_socketio/__init__.py +++ b/flask_socketio/__init__.py @@ -398,6 +398,16 @@ def ping(): if not include_self and not skip_sid: skip_sid = flask.request.sid callback = kwargs.pop('callback', None) + if callback: + # wrap the callback so that it sets app app and request contexts + sid = flask.request.sid + original_callback = callback + + def _callback_wrapper(*args): + return self._handle_event(original_callback, None, namespace, + sid, *args) + + callback = _callback_wrapper self.server.emit(event, *args, namespace=namespace, room=room, skip_sid=skip_sid, callback=callback, **kwargs)