Hello ! Since installing Symfony UX: Turbo, I have a problem with Pusher. When I browse through multiple conversations and then send a message, it is duplicated by the number of conversations I clicked. There is only one saved message, but the user sees his message being sent in several copies.
Here is the Javascript :
var pusher = new Pusher('ba75523bee28d7c644f2', {
cluster: 'eu',
});
var channel = pusher.subscribe('my-channel-' + JSON.parse('{{ messaging.id | json_encode | raw }}'));
var currentUser = JSON.parse('{{ app.user.id | json_encode | raw }}');
channel.bind('my_event',
function (data) {
$('.lala').append('<li>'+ data['content'] +'</li >');
});
channel.bind('pusher:subscription_succeeded', function (members) { });
Here is the controller :
/**
* @Route("/new-message-{id}", name="newMessage", methods={"POST"})
*
* @throws PusherException|GuzzleException
*/
public function message(Messaging $messaging, Request $request, PusherService $pusherService): Response
{
$user = $this->getUser();
$pusherService->sendMessage();
$message = new Message();
if ($request->isMethod(Request::METHOD_POST) && $request->get('content') != null) {
$message->setContent($request->get('content'));
$entityManager = $this->getDoctrine()->getManager();
$entityManager->persist($message);
$entityManager->flush();
$data['content'] = $message->getContent();
$key = $messaging->getId();
if ($pusherService->sendMessage()->trigger("my-channel-${key}", 'my_event', $data)) {
echo 'success';
} else {
header('', true, 403);
echo 'error';
}
return new JsonResponse(Response::HTTP_OK);
}
}
Thanks for your help ! Here is the full project : GitHub - EvilSpartans/Jobissim