T
T
TANK_IST2017-09-04 22:01:02
symfony
TANK_IST, 2017-09-04 22:01:02

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\Chatit 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

2 answer(s)
A
Alexey Skobkin, 2017-09-04
@skobkin

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.

Y
Yuri Melgosh, 2018-01-07
@estet-osa

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}));
};

Next, you need to dynamically write this id to the property of the object: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);
        }
}

That is, we parse the Json that has arrived to us, and see if there is a special userId flag , then we simply glue it to our connection object. I think it's understandable, the rest you will fantasize yourself :)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question