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.phpThe 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:
- The client connects to the WebSocket server.
- Conveyor sends
connection-info. - The client sends
channel-connect. - The client sends
broadcast-action. - 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 => trueAcknowledgments
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-tokenNext steps
Pusher ProtocolWhen Laravel Echo and
pusher-js clients need the standard Pusher wire format.
Laravel Echo / ReverbA full Laravel and Echo setup.
Conveyor SubProtocolLow-level message examples.
Prefer a packaged native client? See the JavaScript and PHP client guides.