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 !