onveyor
Site menu
GitHub
Introduction / Usage Native mode

Usage

Native mode uses a small JSON action protocol for direct WebSocket clients.

Native Conveyor mode

Use this mode when clients send Conveyor action messages directly.

server.phpnative
<?php

use Conveyor\Constants;
use Conveyor\ConveyorServer;

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

(new ConveyorServer())
    ->host('127.0.0.1')
    ->port(8989)
    ->serverOptions([
        'worker_num' => 1,
        'task_worker_num' => 1,
    ])
    ->conveyorOptions([
        Constants::WEBSOCKET_SUBPROTOCOL => Constants::SOCKET_CONVEYOR,
        Constants::USE_PRESENCE => false,
        Constants::USE_ACKNOWLEDGMENT => false,
    ])
    ->start();
shell
php server.php

The native subprotocol value is socketconveyor.com.

Native client flow

javascriptnative
const ws = new WebSocket('ws://127.0.0.1:8989')

ws.onmessage = event => console.log(event.data)

ws.onopen = () => {
  ws.send(JSON.stringify({
    action: 'channel-connect',
    channel: 'chat.room.1',
  }))

  ws.send(JSON.stringify({
    action: 'broadcast-action',
    data: { body: 'hello' },
  }))
}

The normal channel flow is:

  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 the message to other sockets in the same channel.

Native actions

base-actionEchoes data back to the current socket.
channel-connectConnects the current socket to one channel.
broadcast-actionSends data to the current channel. If the sender is in no channel, it goes to sockets that are not in any channel.
fanout-actionSends data to every established socket. If a server token is configured, the action requires auth with that token.
channel-disconnectRemoves the current socket from its channel.
acknowledge-actionLets clients acknowledge server messages when acknowledgment is enabled.

Message examples are in Conveyor SubProtocol.

Native options

Presence

php
Constants::USE_PRESENCE => true

Acknowledgments

php
Constants::USE_ACKNOWLEDGMENT => true,
Constants::ACKNOWLEDGMENT_ATTEMPTS => 3,
Constants::ACKNOWLEDGMENT_TIMOUT => 0.5,

Server token

Set a server token to protect WebSocket handshakes and protected actions:

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

When a token is set, native clients pass it as a token query parameter:

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

Next steps

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