Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to emit websocket message from outside a websocket endpoint?

I'm building a website using Flask in which I also use Websockets using Flask-socketIO, but there's one thing I don't understand.

I built a chat-functionality. When one user sends a message I use websockets to send that message to the server, after which I emit the message to the other user from within that same call:

@socketio.on('newPM', namespace='/test')
@login_required_with_global
def io_newMessage(theJson):
    emit('message', {'message': theJson['message']}, room=str(theJson['toUserId']))

But let's say that I want to emit a message to a user when a file was saved. This means that I need to emit a message from within the view in which the file is POSTed. So according to the flask_socketio docs I can add a namespace in the emit. So I wrote this:

@app.route('/doc', methods=['POST'])
@login_required
def postDoc():
    saveDocument(request.files['file'], g.user.id)
    emit('my response', {'data': 'A NEW FILE WAS POSTED'}, room=current_user.id, namespace='/test')
    return jsonify({'id': str(doc.id)})

But seeing the stacktrace below there still is a problem with the namespace; werkzeug has an AttributeError: 'Request' object has no attribute 'namespace'.

Does anybody know what I'm doing wrong here? Or is this a bug in flask_socketio? All tips are welcome!

Traceback (most recent call last):
  File "/usr/local/lib/python2.7/dist-packages/flask/app.py", line 1820, in wsgi_app
    response = self.make_response(self.handle_exception(e))
  File "/usr/local/lib/python2.7/dist-packages/flask/app.py", line 1403, in handle_exception
    reraise(exc_type, exc_value, tb)
  File "/usr/local/lib/python2.7/dist-packages/flask/app.py", line 1817, in wsgi_app
    response = self.full_dispatch_request()
  File "/usr/local/lib/python2.7/dist-packages/flask/app.py", line 1477, in full_dispatch_request
    rv = self.handle_user_exception(e)
  File "/usr/local/lib/python2.7/dist-packages/flask/app.py", line 1381, in handle_user_exception
    reraise(exc_type, exc_value, tb)
  File "/usr/local/lib/python2.7/dist-packages/flask/app.py", line 1475, in full_dispatch_request
    rv = self.dispatch_request()
  File "/usr/local/lib/python2.7/dist-packages/flask/app.py", line 1461, in dispatch_request
    return self.view_functions[rule.endpoint](**req.view_args)
  File "/usr/local/lib/python2.7/dist-packages/flask_login.py", line 758, in decorated_view
    return func(*args, **kwargs)
  File "/home/vg/app/views.py", line 768, in emitNotificationCount
    emit('menuInit', emitJson, room=current_user.id, namespace='/test')
  File "/usr/local/lib/python2.7/dist-packages/flask_socketio/__init__.py", line 444, in emit
    return request.namespace.emit(event, *args, **kwargs)
  File "/usr/local/lib/python2.7/dist-packages/werkzeug/local.py", line 338, in __getattr__
    return getattr(self._get_current_object(), name)
AttributeError: 'Request' object has no attribute 'namespace'
like image 669
kramer65 Avatar asked May 08 '15 13:05

kramer65


1 Answers

Quoting from Miguel Grinberg's response on an open issue page on the Flask-SocketIO GitHub:

When you want to emit from a regular route you have to use socketio.emit(), only socket handlers have the socketio context necessary to call the plain emit().

So as an example:

from flask_socketio import SocketIO

app = Flask(__name__)
app.config.from_object('config')
socketio = SocketIO(app)

@app.route('/doc', methods=['POST'])
def postDoc():
    saveDocument(request.files['file'], g.user.id)
    socketio.emit('my response', {'data': 'A NEW FILE WAS POSTED'}, room=current_user.id)
    return jsonify({'id': str(doc.id)})
like image 149
Shashank Avatar answered Sep 29 '22 14:09

Shashank