WebSocket Server

Latest version: pecl install openswoole-22.1.2 | composer require openswoole/core:22.1.5

Introduction

A WebSocket server is a network communication protocol which supports full-duplex communication over a TCP connection. A WebSocket server usually operations on traditional HTTP ports like 80 or 443, this makes it compatible with the HTTP protocol, but you can select other ports to run on. Compared with the HTTP protocol, which is stateless, a WebSocket Server can maintain a persistent connection, making it a stateful protocol. The connection between the client and server is kept alive.

We can create a high performance WebSocket Server with PHP and Swoole. The WebSocket Server maintains a persistent connection with each client and sends messages to clients when the connection is established. Swoole provides you with an asynchronous WebSocket server with just a few lines of code and operates a multi-process model.

Use Cases

Some use cases and applications where WebSockets are useful compared to HTTP:

  • Real-time web systems: A WebSocket server is great for real-time systems, data can be sent in real-time very quickly to a client, continuously sending and receiving data whilst staying connected. Great for displaying real-time data on a web dashboard for example.

  • Gaming servers: A game which runs on a network needs high performance so online multiplayer sessions don't lag, WebSocket servers are a great use case for this as they don't need to reconnect every time like HTTP does, it maintains a stateful connection. Data can be continuously sent and received in the background with the server(s).

  • Chat applications: Talking between two parties is great when it is happening asynchronously, especially when sending images or videos etc. This is why WebSockets are great for chat applications. A connection gets established once and can continue to be reused. Good for chat notifications and online statuses etc.

Considerations

  • Web browsers such as Google Chrome or FireFox have native support for WebSocket connections through JavaScript

  • A OpenSwoole WebSocket server can be asynchronous and you are able to make use of coroutines within WebSocket events

  • Clients which do not support the WebSocket protocol will not be able to communicate with the server, you can use $Server->getClientInfo($fd, $workerId) to obtain client information where an element called websocket_status exists and can be used to determine connection type

Events

Methods

You can use the methods from the OpenSwoole\Server within a WebSocket Server.

Classes

Server Configuration

Quick Start Example

<?php

use OpenSwoole\WebSocket\Server;
use OpenSwoole\Http\Request;
use OpenSwoole\WebSocket\Frame;

$server = new Server("0.0.0.0", 9502);

$server->on("Start", function(Server $server)
{
    echo "OpenSwoole WebSocket Server is started at http://127.0.0.1:9502\n";
});

$server->on('Open', function(Server $server, OpenSwoole\Http\Request $request)
{
    echo "connection open: {$request->fd}\n";

    $server->tick(1000, function() use ($server, $request)
    {
        $server->push($request->fd, json_encode(["hello", time()]));
    });
});

$server->on('Message', function(Server $server, Frame $frame)
{
    echo "received message: {$frame->data}\n";
    $server->push($frame->fd, json_encode(["hello", time()]));
});

$server->on('Close', function(Server $server, int $fd)
{
    echo "connection close: {$fd}\n";
});

$server->on('Disconnect', function(Server $server, int $fd)
{
    echo "connection disconnect: {$fd}\n";
});

$server->start();
Last updated on September 1, 2022