Installation Add the bundle via Composer:
composer require drosalys-web/web-socket-bundle
Register the bundle in config/bundles.php:
return [
// ...
Drosalys\WebSocketBundle\DrosalysWebSocketBundle::class => ['all' => true],
];
Configuration Publish the default config:
php bin/console drosalys:websocket:install
Edit config/packages/drosalys_websocket.yaml to define your WebSocket server settings (e.g., host, port, SSL).
First Use Case: Basic Client Connection Use the provided JavaScript client to connect to your WebSocket endpoint:
const socket = new WebSocket('ws://your-symfony-app.com/ws');
socket.onopen = () => console.log('Connected!');
socket.onmessage = (event) => console.log('Message:', event.data);
Symfony Integration
Enable the WebSocket route in your routes.yaml:
drosalys_websocket:
path: /ws
methods: [GET]
Event-Driven Architecture
use Drosalys\WebSocketBundle\Event\WebSocketEvent;
$event = new WebSocketEvent('chat_message', ['text' => 'Hello!']);
$this->get('event_dispatcher')->dispatch(WebSocketEvent::NAME, $event);
Authentication & Authorization
# config/packages/security.yaml
firewalls:
websocket:
pattern: ^/ws
stateless: true
anonymous: false
provider: app_user_provider
Broadcasting to Specific Clients
$this->get('drosalys_websocket.client_manager')->broadcast(
'user_123',
'private_message',
['content' => 'Hi there!']
);
Handling Messages
use Drosalys\WebSocketBundle\Message\WebSocketMessage;
class ChatMessageHandler implements WebSocketMessageHandlerInterface
{
public function handle(WebSocketMessage $message) {
// Process $message->getData() and broadcast responses
}
}
services.yaml:
services:
App\Handler\ChatMessageHandler:
tags: ['drosalys_websocket.message_handler']
Integration with Symfony Controllers
use Drosalys\WebSocketBundle\Service\WebSocketService;
class ChatController extends AbstractController
{
public function __construct(private WebSocketService $wsService) {}
public function sendNotification() {
$this->wsService->broadcast('all', 'notification', ['text' => 'New update!']);
}
}
CORS Issues
# config/packages/drosalys_websocket.yaml
cors:
allow_origin: ['http://your-frontend.com']
Connection Limits
max_connections: 1000
Message Serialization
serializer:
class: App\Serializer\CustomWebSocketSerializer
Symfony Cache Conflicts
php bin/console cache:clear
Debugging WebSocket Events
config/packages/drosalys_websocket.yaml:
debug: true
var/log/dev.log for WebSocket-related errors.Use Topics for Scalability
chat, notifications) to avoid flooding all clients.Leverage Symfony’s Dependency Injection
WebSocketService or ClientManager into services/controllers for reusable logic.Custom Middleware
use Drosalys\WebSocketBundle\Middleware\WebSocketMiddlewareInterface;
class AuthMiddleware implements WebSocketMiddlewareInterface
{
public function handle(WebSocketRequest $request, callable $next) {
if (!$request->isAuthenticated()) {
throw new \RuntimeException('Unauthorized');
}
return $next($request);
}
}
services.yaml:
tags: ['drosalys_websocket.middleware']
Load Testing
SSL/TLS Configuration
ssl:
enabled: true
cert_file: '%kernel.project_dir%/config/ssl/cert.pem'
key_file: '%kernel.project_dir%/config/ssl/key.pem'
Fallback for Older Browsers
if (!window.WebSocket) {
window.WebSocket = require('websocket-polyfill');
}
How can I help you explore Laravel packages today?