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
Request object has no attribute namespace/Client says its connected but server doesn't #40
Comments
When you want to emit from a regular route you have to use |
Hey Miguel, I love your work, but as a first time user of this library this issue did catch me. "Note that socketio.send() and socketio.emit() are not the same functions as the context-aware send() and emit()." and obviously I found this answer in Google, but it may be worth it to make a small note in the flask-socketio documentation highlighting this potential stumbling block. Anyway, I'm looking forward to your new flask mega-tutorial, and thanks again for all you do. |
@NotLeet If you are working with Flask, then you should, in general, use The So what do you think the documentation should say? I would rather you always use |
Hi, been struggling with whether to use socketio.emit() and emit() myself...have a Flask application where I am just trying to emit a signal server side to the client to confirm that a MongoDB insert() method has indeed fired and completed. After hours of pulling my hair out, I finally came across this thread, and noticed the use of just emit(), which I have implemented (see below):
Still finding my feet with Flask, so pardon any bad coding on my behalf. This emit signal is however returning an error: AttributeError: 'Request' object has no attribute 'sid' PS: This is my code client side...
|
@linuxson27 the Are you storing the |
Hi Miguel, I am trying to call socketio.emit function but it did not work. here is the code:
what I am doing wrong? I did not share the complete code |
@tahir80 what do you mean by "it didn't work"? There isn't anything obviously wrong in the code that you are showing me, the problem must be somewhere else. |
Here is my code:
from flask import Flask
from flask import render_template, redirect, url_for
from flask import request
from flask import Response
from flask.ext.socketio import SocketIO, emit
import os
import workout as w
tmpl_dir = os.path.join(os.path.dirname(os.path.abspath(file)), 'templates')
app = Flask(name, template_folder=tmpl_dir)
app.config['SECRET_KEY'] = 'secret!'
socketio = SocketIO(app)
@app.route('/')
def home():
print ("Home")
return render_template('home.html')
@app.route('/workout', methods = ['POST'])
def workout():
emit('movement', 'That worked')
w.start_workout(5, 10)
return redirect(url_for('home'))
@socketio.on('connect', namespace='/')
def connect():
print ("We have connected to socketio")
if (name == "main"):
app.debug = True
socketio.run(app)
My Summary
we_are_connected is not firing although socket.on(connect, ....) in the client is executing the callback
Error(The part of it that matters)
emit('movement', 'That worked')
File "/usr/lib/python2.7/site-packages/flask_socketio/init.py", line 230, in emit
return request.namespace.emit(event, _args, *kwargs)
File "/usr/lib/python2.7/site-packages/werkzeug/local.py", line 338, in getattr
return getattr(self.get_current_object(), name)
AttributeError: 'Request' object has no attribute 'namespace'
{'CONTENT_LENGTH': '23',
'CONTENT_TYPE': 'application/x-www-form-urlencoded',
'GATEWAY_INTERFACE': 'CGI/1.1',
'HTTP_ACCEPT': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,/;q=0.8',
'HTTP_ACCEPT_ENCODING': 'gzip,deflate,sdch',
'HTTP_ACCEPT_LANGUAGE': 'en-US,en;q=0.8',
'HTTP_CACHE_CONTROL': 'max-age=0',
'HTTP_CONNECTION': 'keep-alive',
'HTTP_HOST': 'localhost:5000',
'HTTP_ORIGIN': 'http://localhost:5000',
'HTTP_REFERER': 'http://localhost:5000/',
'HTTP_USER_AGENT': 'Mozilla/5.0 (X11; Linux i686) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/35.0.1916.153 Safari/537.36',
'PATH_INFO': '/workout',
'QUERY_STRING': '',
'REMOTE_ADDR': '127.0.0.1',
'REMOTE_PORT': '52426',
'REQUEST_METHOD': 'POST',
'SCRIPT_NAME': '',
'SERVER_NAME': 'localhost.localdomain',
'SERVER_PORT': '5000',
'SERVER_PROTOCOL': 'HTTP/1.1',
'SERVER_SOFTWARE': 'gevent/1.0 Python/2.7',
'werkzeug.request': <Request 'http://localhost:5000/workout' [POST]>,
'wsgi.errors': <open file '', mode 'w' at 0xb73780d0>,
'wsgi.input': <gevent.pywsgi.Input object at 0xb6872c8c>,
'wsgi.multiprocess': False,
'wsgi.multithread': False,
'wsgi.run_once': False,
'wsgi.url_scheme': 'http',
'wsgi.version': (1, 0)} failed with AttributeError
The text was updated successfully, but these errors were encountered: