WebSockets

made simple

<?php
include __DIR__ . '/vendor/autoload.php';
Conveyor\ConveyorServer::start(port: 8080);

WebSockets

made simple

<?php
include __DIR__ . '/vendor/autoload.php';
Conveyor\ConveyorServer::start(port: 8080);

Socket Conveyor is an open-source tool that simplifies real-time solutions. It allows you to build WebSocket servers capable of handling rules and customizations independently.


Be concerned about what matters.

Socket Conveyor aims to transform your experience with WebSockets, making it super simple.

Read More…


Some Usage Examples

Start a simple WebSocket Server

Write your server: (server.php)

<?php

include __DIR__ . '/vendor/autoload.php';

Conveyor\ConveyorServer::start();

Start your server:

# shell
php server.php

Start an extensible WebSocket Server

Create a server with custom settings and accepting HTTP requests:

<?php

use Conveyor\ConveyorServer;
use Conveyor\Events\PreServerStartEvent;
use OpenSwoole\Http\Request;
use OpenSwoole\Http\Response;

ConveyorServer::start(
    port: 8080,
    eventListeners: [
        ConveyorServer::EVENT_PRE_SERVER_START => fn(PreServerStartEvent $event) =>
            $event->server->on('request', function (
                Request $request,
                Response $response,
            ) {
                $response->end('<div>Hello World!</div>');
            }),
    ],
    serverOptions: [
        'document_root' => __DIR__,
        'enable_static_handler' => true,
        'static_handler_locations' => [ '/public' ], // for js, css and iamges...
    ],
);

In the server above, you’ll have a WebSocket server serving at port 8080, a “Hello World” webpage with static files out of the public folder, and able to handle at the same port WebSocket connections with all the Socket Conveyor features.

Start a simple client to connect to your WebSocket Server

You can accomplish Conveyor’s API to use all its features. A simple manner to get there is to use the client built for it:

Install it with composer:

# shell
composer require kanata-php/conveyor-server-client

Add the client to your application:

<?php

use Kanata\ConveyorServerClient\Client;
use OpenSwoole\Coroutine\System;
use OpenSwoole\Process;
use OpenSwoole\Timer;

$process = new Process(function (Process $worker) {
    (new Client([
        'port' => 8080,
        'channel' => 'test-channel',
        'onMessageCallback' => function (Client $client, string $message) {
            echo 'Message received: ' . $message . PHP_EOL;
        },
        'onReadyCallback' => function (Client $client) {
            $counter = 1;
            Timer::tick(1000, function () use ($client, &$counter) {
                $client->send('Message ' . $counter);
                $counter++;
            });
        },
    ]))->connect();
});
$clientPid = $process->start();

co::run(function() use ($clientPid) {
    $info = System::waitSignal(SIGINT, -1);
    Process::kill($clientPid);
    echo 'Finished' . PHP_EOL;
});

With that setup, your application, whenever this runs, will broadcast a message every second and display messages received by other clients. Also, at the end, we make sure to kill the subprocess if the user kills the main process.

Broadcast with Laravel

There is a Laravel Driver for Conveyor, so you can broadcast using Socket Conveyor’s server. That will work seamlessly with your frontend or backend specs, keeping it fast out of the box. You can read more about it in the Documentation.

After its installation, just broadcast as usual.

Find out more about it here: https://github.com/kanata-php/conveyor-laravel-broadcaster.

To learn more about Laravel Broadcasting, check it here: https://laravel.com/docs/10.x/broadcasting.

Start a simple WebSocket Server

Write your server: (server.php)

Then, start the server:

<?php

include __DIR__ . '/vendor/autoload.php';

Conveyor\ConveyorServer::start();
# shell
php server.php

Start an extensible WebSocket Server

Create a server with custom settings and accepting HTTP requests.

In the following server, you’ll have a WebSocket server serving at port 8080, a “Hello World” webpage with static files out of the public folder, and able to handle at the same port WebSocket connections with all the Socket Conveyor features.

<?php

use Conveyor\ConveyorServer;
use Conveyor\Events\PreServerStartEvent;
use OpenSwoole\Http\Request;
use OpenSwoole\Http\Response;

ConveyorServer::start(
    port: 8080,
    eventListeners: [
        ConveyorServer::EVENT_PRE_SERVER_START => fn(PreServerStartEvent $event) =>
            $event->server->on('request', function (
                Request $request,
                Response $response,
            ) {
                $response->end('<div>Hello World!</div>');
            }),
    ],
    serverOptions: [
        'document_root' => __DIR__,
        'enable_static_handler' => true,
        'static_handler_locations' => [ '/public' ], // for js, css and iamges...
    ],
);

Start a simple client to connect to your WebSocket Server

You can accomplish Conveyor’s API to use all its features. A simple manner to get there is to use the client built for it:

1 – Install it with composer.

2 – Add the client to your application.

With that setup, your application, whenever this runs, will broadcast a message every second and display messages received by other clients. Also, at the end, we make sure to kill the subprocess if the user kills the main process.

# shell
composer require kanata-php/conveyor-server-client
<?php

use Kanata\ConveyorServerClient\Client;
use OpenSwoole\Coroutine\System;
use OpenSwoole\Process;
use OpenSwoole\Timer;

$process = new Process(function (Process $worker) {
    (new Client([
        'port' => 8080,
        'channel' => 'test-channel',
        'onMessageCallback' => function (Client $client, string $message) {
            echo 'Message received: ' . $message . PHP_EOL;
        },
        'onReadyCallback' => function (Client $client) {
            $counter = 1;
            Timer::tick(1000, function () use ($client, &$counter) {
                $client->send('Message ' . $counter);
                $counter++;
            });
        },
    ]))->connect();
});
$clientPid = $process->start();

co::run(function() use ($clientPid) {
    $info = System::waitSignal(SIGINT, -1);
    Process::kill($clientPid);
    echo 'Finished' . PHP_EOL;
});

Broadcast with Laravel

There is a Laravel Driver for Conveyor, so you can broadcast using Socket Conveyor’s server. That will work seamlessly with your frontend or backend specs, keeping it fast out of the box. You can read more about it in the Documentation.

After its installation, just broadcast as usual.

Find out more about it here: https://github.com/kanata-php/conveyor-laravel-broadcaster.

To learn more about Laravel Broadcasting, check it here: https://laravel.com/docs/10.x/broadcasting.


Growing Ecosystem

Socket Conveyor

PHP
Client

JS Client

Jacked Server

Conveyor Laravel Broadcaster


Stay Updated!

3 + 6 =

Your subscription could not be saved. Please try again.
Your subscription has been successful.