<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
fluffydiscord / roadrunner-symfony-bundle example snippets
use Symfony\Component\HttpFoundation\StreamedResponse;
class MyController
{
#[Route("/stream")]
public function myStreamResponse()
{
return new StreamedResponse(
function () {
// replace all 'echo' or any outputs with 'yield'
// echo "data";
yield "data";
}
);
}
}
namespace App\EventListener;
use App\Centrifuge\Event\ConnectEvent;
use RoadRunner\Centrifugo\Payload\ConnectResponse;
use Symfony\Component\EventDispatcher\Attribute\AsEventListener;
#[AsEventListener(event: ConnectEvent::class, method: "handleConnect")]
readonly class ChatListener
{
public function handleConnect(ConnectEvent $event): void
{
// original Centrifugo request passed from RoadRunner
$request = $event->getRequest();
// auth your user or whatever you want
$authToken = $request->getData()["authToken"] ?? null;
$user = ...
// stop propagating to other listeners,
// you have successfully connected your user
$event->stopPropagation();
// send response using the $event->setResponse($myResponse)
$event->setResponse(new ConnectResponse(
user: $user->getId(),
data: [
"messages" => ... // initial data client receives when connected
],
));
}
}
namespace App\Entity\User;
use Doctrine\DBAL\Types\Types;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Security\Core\User\EquatableInterface;
use Symfony\Component\Security\Core\User\PasswordAuthenticatedUserInterface;
use Symfony\Component\Security\Core\User\UserInterface;
class User implements UserInterface, PasswordAuthenticatedUserInterface, EquatableInterface
{
#[ORM\Id]
private ?int $id = null;
#[ORM\Column(type: Types::TEXT, unique: true)]
private ?string $email = null;
#[ORM\Column(type: Types::TEXT)]
private ?string $password = null;
// serialize ony these three fields
public function __serialize(): array
{
return [
"id" => $this->id,
"email" => $this->email,
"password" => $this->password,
];
}
// unserialize ony these three fields
public function __unserialize(array $data): void
{
$this->id = $data["id"] ?? null;
$this->email = $data["email"] ?? null;
$this->password = $data["password"] ?? null;
}
// check only the three serialized fields
public function isEqualTo(mixed $user): bool
{
if (!$user instanceof self) {
return false;
}
return $this->id === $user->getId()
&&
$this->password === $user->getPassword()
&&
$this->email === $user->getEmail()
;
}
}
class MyType extends AbstractType
{
// your buildForm() and what not
// ...
// invalid use of setDefaults()
public function configureOptions(OptionsResolver $resolver): void
{
$resolver->setDefaults([
// loads current user
// and reuses his email forever until worker restarts
// everyone, even in anonymous browser tabs or different sessions,
// will see their email
"label" => $this->security->getUser()->getEmail(),
]);
}
}
// with this the user email will
// stay within this single request
// and won't be leaked to subsequent worker requests
$correctForm = $this->createForm(MyType::class, options: [
"label" => $this->getUser()->getEmail(),
]);
shell
composer
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.