Core concepts

Conveyor Server Events

“Each part responds to the events with precision, creating a harmonious and responsive system.”


Conveyor Server comes with events that you can add listeners to and customize the whole mechanism.

To add a listener to an event, you can add callbacks to the events at the eventListeners parameter at the static (new Conveyor\ConveyorServer())->start method.

1<?php
2
3include __DIR__ . '/vendor/autoload.php';
4
5use Conveyor\ConveyorServer;
6use Conveyor\Events\PreServerStartEvent;
7
8(new ConveyorServer())
9    ->eventListeners([
10        ConveyorConstants::EVENT_PRE_SERVER_START => function (PreServerStartEvent $event) {
11            // do something at the start of the server
12        },
13    ])
14    ->start();

Here is the list of events:

Conveyor\ConveyorServer::EVENT_PRE_SERVER_START

This event happens before the server starts. This can be used to handle HTTP connections or to customize other server events. To know more, check: Handling HTTP Requests.

The listener will receive an instance of the following class as input:

1<?php
2
3namespace Conveyor\Events;
4
5use OpenSwoole\WebSocket\Server;
6
7class PreServerStartEvent
8{
9    public function __construct(
10        public Server $server,
11    ) {
12    }
13}

Conveyor\ConveyorServer::EVENT_SERVER_STARTED

This event happens after the server has started. The listener will receive an instance of the following class as input:

1<?php
2
3namespace Conveyor\Events;
4
5use OpenSwoole\WebSocket\Server;
6
7class ServerStartedEvent
8{
9    public function __construct(
10        public Server $server,
11    ) {
12    }
13}

Conveyor\ConveyorServer::EVENT_PRE_SERVER_RELOAD

This event happens before the server reloads. The listener will receive an instance of the following class as input:

1<?php
2
3namespace Conveyor\Events;
4
5use OpenSwoole\WebSocket\Server;
6
7class PreServerReloadEvent
8{
9    public function __construct(
10        public Server $server,
11    ) {
12    }
13}

Conveyor\ConveyorServer::EVENT_POST_SERVER_RELOAD

This event happens after the server reloads. The listener will receive an instance of the following class as input:

1<?php
2
3namespace Conveyor\Events;
4
5use OpenSwoole\WebSocket\Server;
6
7class PostServerReloadEvent
8{
9    public function __construct(
10        public Server $server,
11    ) {
12    }
13}

Conveyor\ConveyorServer::EVENT_MESSAGE_RECEIVED

This event happens whenever a message is received. The listener will receive an instance of the following class as input:

1<?php
2
3namespace Conveyor\Events;
4
5use OpenSwoole\WebSocket\Server;
6
7class MessageReceivedEvent
8{
9    public function __construct(
10        public Server $server,
11        public string $data,
12    ) {
13    }
14}

Conveyor\ConveyorServer::EVENT_BEFORE_MESSAGE_HANDLED

This event happens before a received message is handled. The listener will receive an instance of the following class as input:

1<?php
2
3namespace Conveyor\Events;
4
5use OpenSwoole\WebSocket\Server;
6
7class BeforeMessageHandledEvent
8{
9    public function __construct(
10        public Server $server,
11        public string $data,
12        public int $fd,
13    ) {
14    }
15}

Conveyor\ConveyorServer::EVENT_AFTER_MESSAGE_HANDLED

This event happens when a received message is handled. The listener will receive an instance of the following class as input:

1<?php
2
3namespace Conveyor\Events;
4
5use OpenSwoole\WebSocket\Server;
6
7class AfterMessageHandledEvent
8{
9    public function __construct(
10        public Server $server,
11        public string $data,
12        public int $fd,
13    ) {
14    }
15}
Previous
HTTP Requests