Quick Start

Start a native Socket Conveyor server, connect a WebSocket client, and send one channel broadcast.


1. Create the server

Create server.php:

1<?php
2
3use Conveyor\Constants;
4use Conveyor\ConveyorServer;
5
6require __DIR__ . '/vendor/autoload.php';
7
8(new ConveyorServer())
9    ->host('127.0.0.1')
10    ->port(8989)
11    ->serverOptions([
12        'worker_num' => 1,
13        'task_worker_num' => 1,
14    ])
15    ->conveyorOptions([
16        Constants::WEBSOCKET_SUBPROTOCOL => Constants::SOCKET_CONVEYOR,
17        Constants::USE_PRESENCE => false,
18        Constants::USE_ACKNOWLEDGMENT => false,
19    ])
20    ->start();

2. Run it

1php server.php

ConveyorServer::start() initializes default persistence tables, creates an OpenSwoole WebSocket server, selects the configured subprotocol, and starts listening.

3. Connect a client

1<script>
2  const ws = new WebSocket('ws://127.0.0.1:8989')
3
4  ws.onmessage = event => {
5    console.log('received', event.data)
6  }
7
8  ws.onopen = () => {
9    ws.send(JSON.stringify({
10      action: 'channel-connect',
11      channel: 'chat.room.1',
12    }))
13
14    ws.send(JSON.stringify({
15      action: 'broadcast-action',
16      data: { body: 'hello' },
17    }))
18  }
19</script>

When the connection succeeds, Conveyor sends a connection-info message. After channel-connect, broadcast-action sends to other established sockets in the same channel.

4. Go further