Skip to content
This repository has been archived by the owner on Sep 7, 2019. It is now read-only.


Switch branches/tags

Name already in use

A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?

Latest commit


Git stats


Failed to load latest commit information.
Latest commit message
Commit time
November 26, 2013 20:14
November 26, 2013 21:57
August 12, 2018 20:50
May 6, 2019 20:16


⛔️ This project is no longer maintained ⛔️

We urge you to look for a replacement.


WebSocket Server and Client library for PHP. Works with the latest HyBi specifications, as well the older Hixie #76 specification used by older Chrome versions and some Flash fallback solutions.

This project was started to bring more interactive features to



  • Hixie #76 and Hybi #12 protocol versions
  • Flash client support (also serves XML policy file on the same port)
  • Native Firefox, Safari (iPod / iPhone as well), Chrome and IE10 support. With Flash Client every browser supporting Flash works as well (including IE6-9, Opera, Android and other older desktop browsers).
  • Opera (Mobile) supports WebSockets natively but support has been disabled by default. Can be enabled in opera:config.


  • Hybi / Hixie76 support.
  • Event-based Async I/O

Getting started

The easiest way to set up PHPWS is by using it as Composer dependency. Add the following to your composer.json

    "repositories": [
            "type": "vcs",
            "url": ""
    "require": {
        "devristo/phpws": "dev-master"

And run php composer.phar install

To verify it is working create a time.php in your project root

use Devristo\Phpws\Server\WebSocketServer;

$loop = \React\EventLoop\Factory::create();

// Create a logger which writes everything to the STDOUT
$logger = new \Zend\Log\Logger();
$writer = new Zend\Log\Writer\Stream("php://output");

// Create a WebSocket server using SSL
$server = new WebSocketServer("tcp://", $loop, $logger);

$loop->addPeriodicTimer(0.5, function() use($server, $logger){
    $time = new DateTime();
    $string = $time->format("Y-m-d H:i:s");
    $logger->notice("Broadcasting time to all clients: $string");
    foreach($server->getConnections() as $client)

// Bind the server

// Start the event loop

And a client time.html as follows

        <title>WebSocket TEST</title>
        <h1>Server Time</h1>
        <strong id="time"></strong>

            var socket = new WebSocket("ws://localhost:12345/");
            socket.onmessage = function(msg) {
                document.getElementById("time").innerText =;

Now run the time.php from the command line and open time.html in your browser. You should see the current time, broadcasted by phpws at regular intervals. If this works you might be interested in more complicated servers in the examples folder.

Getting started with the Phpws Client

The following is a client for the websocket server hosted at

require_once("vendor/autoload.php");                // Composer autoloader

$loop = \React\EventLoop\Factory::create();

$logger = new \Zend\Log\Logger();
$writer = new Zend\Log\Writer\Stream("php://output");

$client = new \Devristo\Phpws\Client\WebSocket("ws://", $loop, $logger);

$client->on("request", function($headers) use ($logger){
    $logger->notice("Request object created!");

$client->on("handshake", function() use ($logger) {
    $logger->notice("Handshake received!");

$client->on("connect", function($headers) use ($logger, $client){
    $client->send("Hello world!");

$client->on("message", function($message) use ($client, $logger){
    $logger->notice("Got message: ".$message->getData());


Known Issues

  • Lacks ORIGIN checking (can be implemented manually in onConnect using getHeaders(), just disconnect the user when you dont like the Origin header)
  • No support for extension data from the HyBi specs.



  • PHP 5.4

  • Open port for the server

  • PHP OpenSSL module to run a server over a encrypted connection

  • as its a dependency of Zend\Uri

  • Composer dependencies * These will be installed automatically when using phpws as a composer package.

  • Reactphp

  • ZF2 Logger


  • PHP 5.4
  • Server that implements the HyBi (#8-#12) draft version
  • PHP OpenSSL module to connect using SSL (wss:// uris)