Home > Enterprise >  Symfony 5 Private chat with Pusher
Symfony 5 Private chat with Pusher

Time:09-25

I've created a simple group chat application with Symfony 5, and now I'm trying to add Socket with Pusher.

For the moment it's working, but because I'm using a single channel, everytime a user post a message, this message appears on every conversation. That's why I would like to pass the messaging_id in the channel name, but it's not working.

I read on the doc I should use the prefix private- in my channel name, but with this the console is asking me to auth the user, and I don't know how do it because my user is already connected.

Here is my logic controller :

 // Pusher
        $options = array(
            'cluster' => 'eu',
            'useTLS' => true
        );
        $pusher = new Pusher(
            'ba75523bee28d7c644f2',
            '9597b6daf0fb4e20fda2',
            '1266737',
            $options
        );

        //New message
        $message = new Message();
        $form = $this->createForm(MessageType::class, $message);
        $form->handleRequest($request);

        if ($form->isSubmitted() && $form->isValid()) {
            $message->setAuthor($user);
            $message->setMessaging($messaging);
            $entityManager = $this->getDoctrine()->getManager();
            $entityManager->persist($message);
            $entityManager->flush();

            $data = $message->getContent();

            if ($pusher->trigger('my-channel', 'my_event', $data)) {
                echo 'success';
            } else {
                header('', true, 403);
                echo 'error';
            }

            return new JsonResponse(Response::HTTP_OK);
        }

Here is my Javascript :

Pusher.logToConsole = true;

var pusher = new Pusher('ba75523bee28d7c644f2', {
    cluster: 'eu',
});

var channel = pusher.subscribe('my-channel');

channel.bind('my_event',
    function (data) {
        console.log(data);
        $('.lala').append('<li class = "chat-left"><div class="chat-text">'   data   '</div>');
    });

channel.bind('pusher:subscription_succeeded', function (members) {
    console.log('successfully subscribed!');
});

function ajaxCall(ajax_url, ajax_data) {
    $.ajax({
        type: "POST",
        url: ajax_url,
        dataType: "json",
        data: ajax_data,
        success: function (response) {
            console.log(response);
        },
        error: function (msg) { }
    });
}

Here is the error if I use the private- prefix : enter image description here

To summarize, I would like every message to only appear in the good conversation (Messages are of course saved in the database in the right conversation). Thank you for your help !

CodePudding user response:

You don't have to use private channels to have a channel per chat, but it does make sense - this would allow you to ensure the only users that are a member of the chat are able to subscribe to the channel (and therefore receive messages).

The error message received appears to indicate an error with the code at your auth endpoint. You can find details of the auth process and examples of auth endpoint code at https://pusher.com/docs/channels/server_api/authenticating-users/.

If you are using the PHP library than the code needed to generate an auth token is detailed in the Github repo. This will need to be returned in response to the auth request

  • Related