1. Go to this page and download the library: Download micilini/php-websockets library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
use Micilini\PhpSockets\Chat\ChatServer;
use Micilini\PhpSockets\Config\ChatConfig;
use Micilini\PhpSockets\Config\ServerConfig;
$server = ChatServer::create(
ServerConfig::new(host: '127.0.0.1', port: 8080),
ChatConfig::new(),
);
$server->run();
$server->on('user.joined', function (array $event): void {
// User joined the chat.
});
$server->on('user.left', function (array $event): void {
// User left the chat.
});
$server->on('message.received', function (array $event): void {
// A chat message was received.
});
$server->on('room.created', function (array $event): void {
// A private room was created.
});
$server->on('bot.responded', function (array $event): void {
// A bot generated a response.
});
use Micilini\PhpSockets\Database\MigrationRunner;
use Micilini\PhpSockets\Storage\Pdo\PdoConnectionFactory;
$pdo = PdoConnectionFactory::sqlite(__DIR__ . '/storage/phpsockets.sqlite');
(new MigrationRunner($pdo))->run('sqlite');
use Micilini\PhpSockets\Contracts\BotInterface;
use Micilini\PhpSockets\Chat\Bot\BotContext;
use Micilini\PhpSockets\Chat\Bot\BotResponse;
final class EchoBot implements BotInterface
{
public function name(): string
{
return 'EchoBot';
}
public function handle(BotContext $context): ?BotResponse
{
$text = trim($context->text());
if (!str_starts_with($text, '/echo ')) {
return null;
}
return BotResponse::text(substr($text, 6));
}
}
$server->bots()->register(new EchoBot());
bash
composer
bash
git clone https://github.com/micilini/php-websockets.git
cd php-websockets
composer install