Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save charles-dr/c463e97ec82ab7a31d90f38a26691bed to your computer and use it in GitHub Desktop.
Save charles-dr/c463e97ec82ab7a31d90f38a26691bed to your computer and use it in GitHub Desktop.
shared
from flask import request
from flask_socketio import SocketIO
# main app
from application import app
clients = {}
socketio = SocketIO(app)
## this is the function to notify the user through socket.
def notify_user(user_id, event, args):
if str(user_id) in clients:
return socketio.emit(event, args, room = clients[str(user_id)]
@socketio.on('connect')
@session_required #wrapper to check login session, which returns user info
def socket_connected(self):
print(f"[SocketConnected] user '{self.id}'")
clients[str(self.id)] = request.sid
@app.route('/connect/<to_user>')
def connect_to_user(to_user):
# do somthing before notifying to the requested user 'to_user'.
# then notify that a connection is requested
notify_user(user_id = to_user, event = 'CONNECTION_REQUESTED', args={ "message": "Connection requested!" })
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment