Jump to content

Best way to identify chat user to send message to


Go to solution Solved by Jacques1,

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.

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

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

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?

  • Solution

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 by Jacques1
This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.