onveyor
Site menu
GitHub
Native Conveyor / Conveyor SubProtocol Native mode

Conveyor SubProtocol

The native JSON action protocol: the handshake, message shape, built-in actions, and custom handlers.

Enable native mode

php
Constants::WEBSOCKET_SUBPROTOCOL => Constants::SOCKET_CONVEYOR

The WebSocket subprotocol value is:

text
socketconveyor.com

Connection info

After a successful native handshake, Conveyor sends a connection-info message:

jsonserver → client
{
  "action": "connection-info",
  "data": "{\"fd\":1,\"event\":\"connection-info\"}"
}

When acknowledgment is enabled, this message also includes an id.

Message shape

Clients send JSON messages with an action field. Most actions also need action-specific fields.

jsonclient → server
{
  "action": "base-action",
  "data": "hello"
}

Built-in actions

base-action

Echoes the message back to the current socket.

json
{
  "action": "base-action",
  "data": "hello"
}

channel-connect

Connects the current socket fd to one channel.

json
{
  "action": "channel-connect",
  "channel": "orders.1"
}

When Constants::WEBSOCKET_SERVER_TOKEN is configured, include either the server token or a temporary channel token in auth:

jsonwith auth
{
  "action": "channel-connect",
  "channel": "orders.1",
  "auth": "temporary-token"
}

broadcast-action

Sends data to other sockets in the current channel. If the sender is not in a channel, Conveyor broadcasts to sockets that are not in any channel.

json
{
  "action": "broadcast-action",
  "data": {
    "body": "order updated"
  }
}

fanout-action

Sends data to every established socket. When Constants::WEBSOCKET_SERVER_TOKEN is configured, auth must contain that server token.

json
{
  "action": "fanout-action",
  "data": "hello everyone"
}

channel-disconnect

Removes the current socket fd from its channel.

json
{
  "action": "channel-disconnect"
}

acknowledge-action

Acknowledges a server message when native acknowledgment is enabled. data carries the message id.

json
{
  "action": "acknowledge-action",
  "data": "message-id"
}

Channel flow

  1. The client connects to the WebSocket server.
  2. The server sends connection-info.
  3. The client sends channel-connect with a channel.
  4. The server records the fd-to-channel association.
  5. The client sends broadcast-action with data.
  6. The server wraps outbound data as action (broadcast-action), data, and fd (the sender's fd).
  7. The server delivers the message to other established sockets in the same channel.

Custom actions

These are native protocol handlers, distinct from hook actions and filters. Pusher mode supports the documented hook callbacks but does not dispatch through Constants::ACTIONS.

Register custom native action handlers through Constants::ACTIONS in conveyorOptions:

phpnative
use Conveyor\Constants;
use Conveyor\ConveyorServer;
use App\WebSocket\SampleAction;

(new ConveyorServer())
    ->port(8989)
    ->conveyorOptions([
        Constants::ACTIONS => [
            new SampleAction(),
        ],
    ])
    ->start();

Each action class implements:

phpinterface
public function execute(array $data): mixed;
public function send(string $data, ?int $fd = null, bool $toChannel = false): void;
public function getName(): string;

Prefer a packaged native client? See the JavaScript and PHP client guides.