Installation:
composer require bordeux/websocket-bundle
Ensure compatibility with Symfony 3.x (this bundle is archived and may not support newer versions).
Register the Bundle:
Add to app/AppKernel.php:
public function registerBundles()
{
$bundles = [
// ...
new Bordeux\WebsocketBundle\BordeuxWebsocketBundle(),
];
}
Start the WebSocket Server:
php app/console bordeux:websocket:bundle
Run this in a terminal to keep the WebSocket server alive.
Create a WebSocket controller (e.g., src/Acme/DemoBundle/Websocket/DemoWebsocket.php):
namespace Acme\DemoBundle\Websocket;
use Bordeux\WebsocketBundle\Websocket\Websocket;
class DemoWebsocket extends Websocket
{
public function configureRoutes(RouteCollection $collection)
{
$collection->add('demo_ws', new Route('/ws/demo', []));
}
public function onOpen(Client $client)
{
$client->send(new Message('Connected!'));
}
public function onMessage(Client $client, Message $message)
{
$client->send(new Message('You said: ' . $message->getPayload()));
}
}
/ws/demo (adjust in configureRoutes).ws://yourdomain.com/ws/demo.<YourBundle>/Websocket/<Name>Websocket.php.configureRoutes(RouteCollection): Define WebSocket routes (e.g., /ws/user/{id}).onOpen(Client): Handle new connections (e.g., send welcome message).onMessage(Client, Message): Process incoming messages.onClose(Client): Cleanup on disconnection.{id}) for user-specific WebSockets.
$collection->add('user_ws', new Route('/ws/user/{id}', [], ['id' => '\d+']));
onOpen:
public function onOpen(Client $client)
{
$request = $client->getRequest();
$token = $request->query->get('token');
if (!$this->isValidToken($token)) {
$client->close();
return;
}
$client->send(new Message('Authenticated!'));
}
private static $clients = [];
public function onOpen(Client $client)
{
self::$clients[$client->getId()] = $client;
}
public function broadcast(Message $message)
{
foreach (self::$clients as $client) {
$client->send($message);
}
}
EntityManager) into the WebSocket class:
use Symfony\Component\DependencyInjection\ContainerInterface;
class DemoWebsocket extends Websocket
{
private $container;
public function __construct(ContainerInterface $container)
{
$this->container = $container;
}
public function onMessage(Client $client, Message $message)
{
$em = $this->container->get('doctrine.orm.entity_manager');
// Use $em to interact with the database.
}
}
Note: Override setContainer() if needed (see Gotchas).Message with binary payloads:
$client->send(new Message($binaryData, Message::BINARY));
use Psr\Log\LoggerInterface;
class DemoWebsocket extends Websocket
{
private $logger;
public function __construct(LoggerInterface $logger)
{
$this->logger = $logger;
}
public function onMessage(Client $client, Message $message)
{
$this->logger->info('Message received', ['payload' => $message->getPayload()]);
}
}
$websocket = new DemoWebsocket();
$websocket->setContainer($this->container); // In a service or command.
/ws/).public function onClose(Client $client)
{
unset(self::$clients[$client->getId()]);
}
Websocket to add protocol-specific logic.onMessage).use Symfony\Component\EventDispatcher\EventDispatcherInterface;
class DemoWebsocket extends Websocket
{
private $dispatcher;
public function __construct(EventDispatcherInterface $dispatcher)
{
$this->dispatcher = $dispatcher;
}
public function onMessage(Client $client, Message $message)
{
$event = new WebsocketEvent($client, $message);
$this->dispatcher->dispatch($event, 'websocket.message');
}
}
Client and Message classes to test logic.$client = new \WebSocket\Client('ws://localhost/ws/demo');
$client->send('test');
$response = $client->receive();
How can I help you explore Laravel packages today?