Introduction
Getting started
“Extensibility is the key to building future-proof systems. It’s not about predicting the future, but about creating architectures that thrive in the face of change.” Robert C. Martin
Quick start
Install:
1composer require kanata-php/socket-conveyor
After installation, let’s start with the simplest case: ping/pong with the server.
The following file run an OpenSwoole WebSocket server:
1<?php
2
3// server.php
4
5use Conveyor\ConveyorServer;
6
7(new ConveyorServer())->port(8080)->start();
Important: notice that handling the “handshake” event might be necessary; for reference, visit: https://openswoole.com/docs/modules/swoole-websocket-server-on-handshake.
Then you run:
1php server.php
Once this is running, you should be able to connect to it using base Websocket client or any of the clients built for Socket Conveyor. The following is an example using the raw JavaScript Websocket object:
1<script type="text/javascript">
2 let ws = new WebSocket(`ws://127.0.0.1:8080`)
3
4 ws.onmessage = (e) => console.log(e)
5
6 ws.onopen = () => {
7 // connect to channel
8 ws.send(JSON.stringify({ action: 'channel-connect', channel: 'my-channel' }))
9
10 // broadcast to "my-channel"
11 ws.send(JSON.stringify({ action: 'broadcast-action', data: 'My broadcast message.' }))
12 }
13</script>
This client will connect to the WebSocket server, then it will connect to a channel and broadcast a message to it.
To learn more about the available actions in Socket Conveyor, you can find it at the Conveyor SubProtocol.
This is a very basic client connection. Conveyor comes with some client packages in PHP and JavaScript with which you can use all the features of Conveyor, making sure to keep all the best practices – or you can check their source code and help make them better.