Jump to content

Best way to identify chat user to send message to


AntonZelenin

Recommended Posts

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.

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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”.

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

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).

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.