Python Forum
Sending How to send private message with Flask-Socketio and Vue
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Sending How to send private message with Flask-Socketio and Vue
#1
I am trying to send friend request to the clients with Flask-socketIO.
I have been following the instructions for the backend at https://www.youtube.com/watch?v=mX7hPZidPPY
and for the frontend at https://www.youtube.com/watch?v=JEYEpledOxs


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: {
login(){
self.privatesocket.emit('username',self.username_login);
}
}

created(){
this.socket = io("http://127.0.0.1:5000")
this.privatesocket = io("http://127.0.0.1:5000/private")
}


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: {
send_message(){
var res = [{'username': 'user2'}, {'message': "want to be friends?"}]
this.privatesocket.emit('private_message',res);
}
},
created(){
this.socket = io("http://127.0.0.1:5000")
this.privatesocket = io("http://127.0.0.1:5000/private")
}

mounted(){
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.


Question


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


Possibly Related Threads…
Thread Author Replies Views Last Post
Question [Solved] Private static files SpongeB0B 1 1,031 Jul-16-2022, 05:36 AM
Last Post: SpongeB0B
  [flask] mail.send giving error SheeppOSU 2 2,831 May-31-2019, 09:31 PM
Last Post: SheeppOSU
  flask, I want to send a json from the front end and update it to the postgresql DB wi passion044 1 4,016 Apr-12-2018, 08:40 AM
Last Post: thomasp

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020