Skip to content

Instantly share code, notes, and snippets.

@spinningD20
Created March 6, 2014 16:38
Show Gist options
  • Save spinningD20/9393775 to your computer and use it in GitHub Desktop.
Save spinningD20/9393775 to your computer and use it in GitHub Desktop.
from flask.ext.socketio import SocketIO, emit
# flask app configuration, other flask stuff in between. other stuff cut out for brevity
@socketio.on('my broadcast event', namespace='/live')
def live_message():
emit('my response', {'data': statuscontent()}, broadcast=True)
print(request.namespace, 'broadcast socketio')
@socketio.on('connect', namespace='/live')
def live_connect():
global update_clients
update_clients = request.namespace
live_message() #the emit for my broadcast event worked here, but I just left it uniform
print('client connected')
@socketio.on('disconnect', namespace='/live')
def live_disconnect():
print('Client disconnected')
@app.route('/punchnfc/<nfc>/')
def punchnfc(nfc):
global update_clients
# punch stuff here to prepare it for db
db.session.add(new_punch)
db.session.commit()
request.namespace = update_clients
live_message()
'''
when using emit like so:
(emit('my response', {'data': statuscontent()}, namespace=update_clients, broadcast=True)
it would error, saying request object has no attribute namespace, so the workaround
is to set this current request.namespace to update_clients. Another interesting thing,
if I recorded the entire request object into update_clients on socketio, and tried printing
update_clients.namespace, it would also complain that it had no namespace - which made me think
that it wasn't a separate request object once it gets to this point in the flask function
'''
return render_template('punch.html', **context)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment