Usage

Socket Conveyor native mode uses a small JSON action protocol for direct WebSocket clients.


Native Conveyor mode

Use native mode when clients send Conveyor action messages directly.

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();

Run it:

1php server.php

The native subprotocol value is socketconveyor.com.

Native client flow

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

Normal native channel flow:

  1. The client connects to the WebSocket server.
  2. Conveyor sends connection-info.
  3. The client sends channel-connect.
  4. The client sends broadcast-action.
  5. Conveyor sends to other sockets in the same channel.

Native actions

base-action echoes data back to the current socket.

channel-connect connects the current socket to one channel.

broadcast-action sends data to the current channel. If the sender is not connected to a channel, it broadcasts to sockets that are not in channels.

fanout-action sends data to every established socket. If a server token is configured, it requires auth with that token.

channel-disconnect removes the current socket from its channel.

acknowledge-action lets clients acknowledge server messages when acknowledgment is enabled.

See Conveyor SubProtocol for message examples.

Native options

Enable presence:

1Constants::USE_PRESENCE => true

Enable acknowledgments:

1Constants::USE_ACKNOWLEDGMENT => true,
2Constants::ACKNOWLEDGMENT_ATTEMPTS => 3,
3Constants::ACKNOWLEDGMENT_TIMOUT => 0.5,

Protect native WebSocket handshakes and protected actions:

1Constants::WEBSOCKET_SERVER_TOKEN => 'local-server-token'

When a server token is set, native clients connect with:

1ws://127.0.0.1:8989?token=local-server-token

Next steps