A bundle for websocket using swoole
- Swoole or OpenSwoole
- PHP >= 8.1
- Symfony >= 6.1
Add the package
composer require cydrickn/swoole-websocket-bundle
php ./bin/console websocket:server
Run the websocket server
Options | Details | Default |
---|---|---|
host | The host of the server | 127.0.0.1 |
port | The port of the server | 8000 |
This bundle will have an event where you can listen
This event will be trigger once a client have been connected to the server.
This event will be trigger once the client send a message
This event will be trigger once a client have been disconnected
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);
});
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
For runtime configuration you can add it to your public/index.php
Key | Details | Default |
---|---|---|
host | The host of the server | 127.0.0.1 |
port | The port of the server | 8000 |
mode | The mode for server | 2 / SWOOLE_PROCESS |
sock_type | The socket type for server | 1 / SWOOLE_SOCK_TCP |
settings | The setting is base on swoole configuration | [] / Empty Array |
serve_http | Include to serve HTTP | false |
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
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,
],
];
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,
],
];
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);
}
}