cydrickn / swoole-websocket-bundle
Swoole Websocket for Symfony
Installs: 46
Dependents: 0
Suggesters: 0
Security: 0
Stars: 4
Watchers: 1
Forks: 1
Open Issues: 0
Type:symfony-bundle
Requires
- php: ^8.1
- symfony/console: ^6.1
- symfony/framework-bundle: ^6.1
Requires (Dev)
- openswoole/ide-helper: ^4.11
Suggests
- symfony/runtime: When you plan to use this bundle with runtime
README
A bundle for websocket using swoole
Requirements
- Swoole or OpenSwoole
- PHP >= 8.1
- Symfony >= 6.1
Intallation
Add the package
composer require cydrickn/swoole-websocket-bundle
Run the server
php ./bin/console websocket:server
Commands
websocket:server
Run the websocket server
Events
This bundle will have an event where you can listen
\Cydrickn\SwooleWebsocketBundle\Event\OpenEvent
This event will be trigger once a client have been connected to the server.
\Cydrickn\SwooleWebsocketBundle\Event\MessageEvent
This event will be trigger once the client send a message
\Cydrickn\SwooleWebsocketBundle\Event\CloseEvent
This event will be trigger once a client have been disconnected
Client connection using Javascript
For the basic way on connecting to websocket you can use the Browser Websocket
const socket = new WebSocket('ws://localhost:8080'); // Connection opened socket.addEventListener('open', (event) => { socket.send('Hello Server!'); }); // Listen for messages socket.addEventListener('message', (event) => { console.log('Message from server ', event.data); });
Use it in runtime
Using the command is fine, but if you happen you want to serve this using symfony runtime you can do so.
- First you need to include symfony/runtime
composer require symfony/runtime
- Next run your
public/index.php
with env APP_RUNTIMEAPP_RUNTIME=\\Cydrickn\\SwooleWebsocketBundle\\Runtime\\Runtime php ./public/index.php
Runtime Configuration
For runtime configuration you can add it to your public/index.php
Example:
#!/usr/bin/env php <?php use App\Kernel; $_SERVER['APP_RUNTIME_OPTIONS'] = [ 'host' => '0.0.0.0', 'port' => 8000, 'mode' => SWOOLE_PROCESS, 'settings' => [ \Swoole\Constant::OPTION_WORKER_NUM => swoole_cpu_num() * 2, \Swoole\Constant::OPTION_ENABLE_STATIC_HANDLER => true, \Swoole\Constant::OPTION_DOCUMENT_ROOT => dirname(__DIR__).'/public' ], ]; require_once dirname(__DIR__).'/vendor/autoload_runtime.php'; return function (array $context) { return new Kernel($context['APP_ENV'], (bool) $context['APP_DEBUG']); };
Also instead of having to server for websocket and http, you can just enable the
Serve HTTP
, by setting theserve_http
to true
Hot reloading
We already include a hor reloading for this bundle. To enable it you need to first require cydrickn/php-watcher
Hot reloading is only available if you are using runtime
composer require cydrickn/php-watcher
Now add to your APP_RUNTIME_OPTIONS
the hotReload
The basePath
should be your project folder
$_SERVER['APP_RUNTIME_OPTIONS'] = [ 'host' => '0.0.0.0', 'port' => 8000, 'mode' => SWOOLE_PROCESS, 'hotReload'=> [ 'enabled' => true, 'basePath' => dirname(__DIR__), ], 'settings' => [ \Swoole\Constant::OPTION_WORKER_NUM => swoole_cpu_num() * 2, \Swoole\Constant::OPTION_ENABLE_STATIC_HANDLER => true, ], ];
Socket IO
In this bundle you can also enable using socket.io implementation, to enable it first you need to install cydrickn/socketio
This implementation is only available if you are using runtime
composer require cydrickn/socketio
Now add to your APP_RUNTIME_OPTIONS
set the socketio
to true
$_SERVER['APP_RUNTIME_OPTIONS'] = [ 'socketio' => true, 'host' => '0.0.0.0', 'port' => 8000, 'mode' => SWOOLE_PROCESS, 'settings' => [ \Swoole\Constant::OPTION_WORKER_NUM => swoole_cpu_num() * 2, \Swoole\Constant::OPTION_ENABLE_STATIC_HANDLER => true, ], ];
Adding a route to socket io
For pure socket io you will do this
$server->on('connection', function (\Cydrickn\SocketIO\Socket $socket) { $socket->emit('hello', 'world'); }); $server->on('chat', function (\Cydrickn\SocketIO\Socket $socket, $message) { $socket->broadcast()->emit('chat', $message); });
You will use the Attribute Route of this bundle Cydrickn\SwooleWebsocketBundle\Attribute\RouteAttribute
In any of your service
<?php namespace App\Controller; use Cydrickn\SwooleWebsocketBundle\Attribute\RouteAttribute; class SocketController { #[RouteAttribute(path: 'connection')] public function connection(\Cydrickn\SocketIO\Socket $socket) { $socket->emit('hello', 'world'); } #[RouteAttribute(path: 'chat')] public function anotherMethod(\Cydrickn\SocketIO\Socket $socket, $message) { $socket->broadcast()->emit('chat', $message); } }