AntonZelenin Posted July 9, 2017 Share Posted July 9, 2017 Which data should I use to identify user in a list of users in a chat? To specify receiver of a messageI guess using it's id from database isn't the best idea, right? Quote Link to comment https://forums.phpfreaks.com/topic/304281-best-way-to-identify-chat-user-to-send-message-to/ Share on other sites More sharing options...
Jacques1 Posted July 9, 2017 Share Posted July 9, 2017 The PHP Freaks crystal ball is currently broken, so it looks like we have to do this the old-fashioned way: You provide the information (the approach you're using, the code etc.), we try to help you. Sorry for the inconvenience. I usually fix problems through mind control. Quote Link to comment https://forums.phpfreaks.com/topic/304281-best-way-to-identify-chat-user-to-send-message-to/#findComment-1548131 Share on other sites More sharing options...
AntonZelenin Posted July 9, 2017 Author Share Posted July 9, 2017 The PHP Freaks crystal ball is currently broken, so it looks like we have to do this the old-fashioned way: You provide the information (the approach you're using, the code etc.), we try to help you. Sorry for the inconvenience. I usually fix problems through mind control. You go to your homepage. see the list of your dialogs, choose one of them, type smth and send this message to your friend. So this dialog must have a unique identifier that is an id of a receiver. It will be kinda <div class='dialog' onclick='choose_dialog()' receiver-id="%SOME_UNIQUE_ID%"> <img src='$img' class='img-circle'/> <div class='user-name'> <b>$name</b> </div> <div class='message'>$message</div> </div> So how could I identify this dialog? I can assign it an id of a user from database, or I can create temporary key that maps user id, or create a table with dialogs and take it's id from there Quote Link to comment https://forums.phpfreaks.com/topic/304281-best-way-to-identify-chat-user-to-send-message-to/#findComment-1548133 Share on other sites More sharing options...
Jacques1 Posted July 9, 2017 Share Posted July 9, 2017 I'm asking about the actual chat backend, not some fancy GUI. You've already implemented a basic mechanism for sending and receiving messages, right? Then how does that work? Or do you have nothing at all? Your current description is far too vague and doesn't make a lot of sense. What is a “receiver” supposed to be in a chat context? Maybe we're talking about different kinds of chats, but in the ones I know, people just add messages to a chat session. There is no “receiver”. Quote Link to comment https://forums.phpfreaks.com/topic/304281-best-way-to-identify-chat-user-to-send-message-to/#findComment-1548135 Share on other sites More sharing options...
AntonZelenin Posted July 9, 2017 Author Share Posted July 9, 2017 Okay, sorry for misleading. It is a messenger, just like telegram or whatsapp. You can send messages to a specific user or a group of users. I have this tables in the database: groupColumns: id, namegroup_usersColumns: group_id, user_idmessagesColumns: id, sender_id, group_id, content When websocket server receives a message it does: $received_data = $this->unmask($buf); $received_data = json_decode($received_data); $message = new TextMessage; $message->set_sender_id($received_data->sender_id); $message->set_group_id($received_data->group_id); $message->set_text($received_data->text); $this->message_saver->to_database($message); And then it have to send the message to all recipients who are online, that is, who are connected to the server. For each socket connection I generate random temporary key to identify it and send it to a user. But I need a permanent one Is it okay to use primary keys from database in front-end? If not - how to bind group and user id from db with id's, that my server uses? Quote Link to comment https://forums.phpfreaks.com/topic/304281-best-way-to-identify-chat-user-to-send-message-to/#findComment-1548136 Share on other sites More sharing options...
Solution Jacques1 Posted July 9, 2017 Solution Share Posted July 9, 2017 (edited) Using a member ID as the message target is fine, unless you don't want to expose this information for some reason. I'm not sure what your “random temporary keys” are supposed to do. Your chats are effectively just private messages, so the user or group ID is the only information needed. The WebSocket server knows all current connections and also knows (or should know) the user behind each connection, so you can simply send the message to the corresponding TCP socket(s). Edited July 9, 2017 by Jacques1 Quote Link to comment https://forums.phpfreaks.com/topic/304281-best-way-to-identify-chat-user-to-send-message-to/#findComment-1548137 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.