Answer the question
In order to leave comments, you need to log in
How to create a private chat in symfony?
I am developing a large project on the latest version of symfony.
It is necessary to implement messaging between users and notifications.
For this I decided to use Ratchet.
I did everything as in this article and it works.
But this is a general chat. And how to write private? Sockets\Chat
it starts at server startup and I can’t access the container, which means I can’t access the user.
I think that I started to do everything wrong. And how is it necessary? Thank you.
Answer the question
In order to leave comments, you need to log in
1. Turn on your head, stop hoping that there is a ready-made solution for everything you want.
2. Write chat logic that will send messages only to the person they are addressed to.
3. If you want help with non-working code - bring the code.
I've been breaking my head for 2 days, here's the solution: immediately after connecting to the client, we send an id (well, or some property that will serve as a user identifier), taken from anywhere, for example, from input[hidden].
conn.onopen = function(e) {
conn.send(JSON.stringify({userId : document.getElementById('user_id').value}));
};
ConnectionInterface $conn
public function onMessage(ConnectionInterface $from, $msg)
{
$data = json_decode($msg);
if (property_exists($data, 'userId')) {
$userId = $data->userId;
$from->userId = $userId;
$this->users[$userId] = $from;
} else {
$this->users[$data->to]->send($msg);
}
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question