Jul-28-2020, 08:14 PM
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.
The following inputs the user and sid in the socket_users dictionary.
Question
How can I get the message to be sent to the specific recipient?
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.sidThe 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?