Python Forum

Full Version: Sending How to send private message with Flask-Socketio and Vue
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
I am trying to send friend request to the clients with Flask-socketIO.
I have been following the instructions for the backend at
and for the frontend at

Vue / Frontend

On the Vue side I have the following.
When I click login the username ends up in the backend "socket_users" dictionary. The button then routes me to the Send message page where a button with "send_message" is.
When I click the "send_message" button the message should be sent to the specified client "user2"

Frontend - login page

Below is where I press login to send the user to the socketIO

methods: {

this.socket = io("")
this.privatesocket = io("")

Front end send message page

This where if I click a button then th function "send message" will run and then the message to the other client ("user2")

methods: {
var res = [{'username': 'user2'}, {'message': "want to be friends?"}]
this.socket = io("")
this.privatesocket = io("")

this.privatesocket.on('new_private_message', data =>{
this.$alert("Friend request")

I have no problem sending when I send to everyone but it is when I send to an individual and when I include "room=recipient_session_id" in "@socketio.on('private_message', namespace ='/private')" which is when nothing is being sent.

In my python script I instantiate the app and the socketio as below.

app = Flask(__name__, static_folder = "static")
app.config['SECRET_KEY'] = 'secretkey'
app.config['CORS_HEADERS'] = 'Content-Type'
cors = CORS(app, resources={r"/": {"origins": origin_var}})

socketio = SocketIO(app, cors_allowed_origins="*")

socket_users = {}   //Socket_users is the dictionary where I input the users that will be using the socket.

The following inputs the user and sid in the socket_users dictionary.

@socketio.on('username',namespace ='/private')
def receive_username(username):
    socket_users[username] = request.sid
The following is where I check that the user is in the socket_users dictionary.

@socketio.on('private_message', namespace ='/private')
def private_message(payload):
    recipient_session_id = socket_users[payload[0]['username']]
    message = payload[1]['message']
    emit('new_private_message', {"message":message},room=recipient_session_id)
If I run the following I don't get the alert to run on the client side for the specific recipient but if I add broadcast=True or remove "room=recipient_session_id" all together then it works.


How can I get the message to be sent to the specific recipient?