-
demo 1: a chat demo using a simple websocket server
-
demo 2: a chat demo using a wamp server (this is wamp v1)
For the demo 1 there is no need for a special library as the native js functions are sufficient to handle connection and events.
For the demo 2, since the intended clients are browser clients I will be using a js client, in this case it is autobahn v 0.8.2
Currently (December 2023) autobahn-js version is 22.11.1 and it is intended to work with WAMP V2 protocol.
Ratchet is considered old software, and there are other alternatives to implement websockets using PHP.
check this question in stackoverflow
If you want to implement a newer alternative to php websockets I would recommend to take a look at laravel or voryx/thruway. (I've got a thruway implementation demo if you are interested)
That being said, I built this demo as an exercise in order to understand the basics of Ratchet, and I hope it helps anyone to get things going.
just make a clone of this repo:
git clone url.git
In your CLI go to the project directory where you cloned this repo
To run the Chat server:
php bin/chat_launcher.php &
To run the wamp server:
php bin/wamp_launcher.php &
Using ampersand at the end of the command will execute the server in detached mode, freeing your CLI.
To stop your server do:
jobs
The number in square brackets is the job_id, to kill the job do:
kill % job_id
- private id (topic name)
- private subscribers (SplObjectStorage for connections)
- getId(): string
- broadcast($msg, array $exclude = array(), array $eligible = array()): Topic
- has(ConnectionInterface $conn): boolean
- add(ConnectionInterface $conn): Topic
- remove(ConnectionInterface $conn): Topic
- getIterator(): null|subscribers
- count(): int
- private instance (LoopInterface)
- private stopped (bool)
- get(): LoopInterface
- set(): void
- addReadStream(resource $stream, callable $listener): void
- addWriteStream(resource $stream, callable $listener): void
- removeReadStream(resource $stream): void
- removeWriteStream(resource $stream): void
- addTimer(float $interval, callable $callback): TimerInterface
- addPeriodicTimer(float $interval, callable $callback): TimerInterface
- cancelTimer(TimerInterface $timer): void
- futureTick(callable $listener): void
- addSignal(int $signal, callable $listener): void
- removeSignal(int $signal, callable $listener): void
- run(): void
- stop(): void
$loop->addReadStream($stream, function ($stream) use ($name) {
echo $name . ' said: ' . fread($stream);
});
$loop->addWriteStream($stream, function ($stream) use ($name) {
fwrite($stream, 'Hello ' . $name);
});
$loop->addTimer(0.8, function () {
echo 'world!' . PHP_EOL;
});
$loop->addTimer(0.3, function () {
echo 'hello ';
});
function hello($name, LoopInterface $loop){
$loop->addTimer(1.0, function () use ($name) {
echo "hello $name\n";
});
}
hello('Tester', $loop);
$timer = $loop->addPeriodicTimer(0.1, function () {
echo 'tick!' . PHP_EOL;
});
$loop->addTimer(1.0, function () use ($loop, $timer) {
$loop->cancelTimer($timer);
echo 'Done' . PHP_EOL;
});
function hello($name, LoopInterface $loop)
{
$n = 3;
$loop->addPeriodicTimer(1.0, function ($timer) use ($name, $loop, &$n) {
if ($n > 0) {
--$n;
echo "hello $name\n";
} else {
$loop->cancelTimer($timer);
}
});
}
hello('Tester', $loop);
$name = "FancyName";
function hello($name, LoopInterface $loop)
{
$loop->futureTick(function () use ($name) {
echo "hello $name\n";
});
}
hello('Tester', $loop);
$loop->futureTick(function () {
echo 'b';
});
$loop->futureTick(function () {
echo 'c';
});
echo 'a';
$loop->addSignal(SIGINT, function (int $signal) {
echo 'Caught user interrupt signal' . PHP_EOL;
});
$loop->removeSignal(SIGINT, $listener);
$loop->run();
$loop->addTimer(3.0, function () use ($loop) {
$loop->stop();
});
- getRemoteAddress(): string
- getLocalAddress(): string
$address = $connection->getRemoteAddress();
echo 'Connection with ' . $address . PHP_EOL;
// $address will output `tcp://127.0.0.1:8080`, `tcp://[::1]:80`, `tls://127.0.0.1:443`
$address = $connection->getLocalAddress();
$ip = trim(parse_url($address, PHP_URL_HOST), '[]');
echo 'Connection with ' . $ip . PHP_EOL;