M
M
mefisto6612020-01-02 10:57:03
JavaScript
mefisto661, 2020-01-02 10:57:03

How to implement individual chat on SocketIo Flask?

I am writing a social network. How to implement a chat between users tete-a-tete? More precisely: there is a url: user/ - user page, chat in the same place. When sending a request via socketio in js, I can specify the url of a specific user ("{{ url_for('user', id_user=user.id) }}"). On the server side, on flask, how do I need to set the namespace so that the request from the Nth user's page comes to the Nth socket? If you write the namespace manually (user/2), then everything works, but, of course, only for the second user. Is it possible to do this at all? The alternative is to make a separate url (/chat) where all messages from all users will go. In the message, pass the sender's id, get all messages from this sender from the database, and return it as an array. Seems like a bummer)

Answer the question

In order to leave comments, you need to log in

1 answer(s)
M
mefisto661, 2020-01-02
@mefisto661

Implemented as follows: Js creates 2 socket connections (with url`s /chat and {{ url_for('user', id_user=user.id) }}. Socket /chat only sends, socket {{ url_for('user', id_user=user.id) }} only accepts responses from the server There is only one @socketio.on('message') function on the server - writes to the database and returns 2 responses:
send(..... namespace = url_for('user ', id_user=sender_id)
send(.... namespace = url_for('user', id_user=receiver_id).
Sender_id and destination_id are sent over the socket from the client along with the message.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question