Thursday, August 25, 2011

PHP WebSocket Server Framework

Finding a bit of gap in the market I decided to implement a PHP based WebSocket server with application framework in PHP. I did find other PHP implementations of Websockets but for the most part these were very simple implementations (often single classes to be modified) rather than a full framework that could be evolved with the standard (which is a moving target) and offer a simple application framework to enable Application protocols to be implemented in isolation from the relative turmoil underneath.

There are of course some excellent Websocket server implementations out there including the excellent PyWebSockets along with a number of very good Node.js offerings including Socket.IO. So the other reason for doing something in PHP was to get some experience below the WebSockets covers .

And to therefore I offer you the PHP-WebSockets-Server which is current hosted under GIT. I plan (as time allows) to evolve this along with the standard and if I can figure out how create an apache MOD for tighter integration. The server currently supports both draft 75 and 76 of the standard along with the newer HyBi protocol. As such it can deal with what most HTML5 clients (and Node.js clients) can through at it, although it has not yet have a rigorous amount of testing under its belt. There are also a few things missing from the shelf the main ones being support for extensions and chunked data. I hope to get to these in early Sept depending on work demands.

I don’t want to repeat what is written in the (Readme) documentation on GIT here but to give a brief outline of how it is architected there are three major levels.

Server Framework

The server framework really deals with how to run the server and to that end there are a couple of methods depending on what platform this is being run on. Basically it can be run from the command line on either Linux or Windows and there is a Daemon script for running as such on Linux.

Library Layer

The main implementation is supported in a PHP library which includes a main server class, interfaces and implementations to deal with the Websocket protocol versions (specifically handshake and data framing) as well as some utility classes.

Application Layer

The server is extended through the creation of Application protocols. All Application protocols must implement the WSApp interface which and is isolated from the protocol mechanics which may change over time. Application Protocols can be specified at the client using the Protocol argument of the WebSocket constructor for example in javascript the following

socket = new WebSocket(host, "Foo");

Would request the Foo application protocol.

The organization above allows for graceful evolution of the WebSockets standard whilst supporting various client interpretations without requiring changes to the application protocol.

As I mentioned above I will try to evolve this over time as I can and as major changes to the standard evolve. I intend to use it for my own prototyping needs and therefore will have some motivation to update.


  1. Hi :)
    nice peace of work, I spended whole day looking for working websocket implementation in php.

    Iam bit confused about interface. How can i send messages from one client to another, there isnt any client list in application. In fakt application doesnt know anything about active users.

    Thx for answer :)

  2. Hi Jeff,

    Very nice implementation.
    It is possible to add feature to broadcast message? ...or what would be the easiest way to implement this?


  3. Hi Jeff,

    very nice implementation.
    is it possible to broadcast a message or how would I most simply implement something like this?


  4. Thanks for the comments, let me try and address them here.

    There is no client library the assumption is that the client is the browser. There are however client packages available for most programming platforms (e.g Socket.IO) just search for websocket client.

    As to broadcast, I am looking into that now. It shouldn't be a big problem, I just need to iterate across the socket list and send the message to each active client for a service. Look for an update soon.

  5. Great, great job!

    Any plans on adding support for secure sockets (wss)?

  6. I will try and pull wss into the next update.

  7. Hi, in my first comment I ment connected browsers. I cant understand how should application react to new client or if client is disconnected.

    So if I may have suggestion :)
    add "public function onConnect(User)" and "public function onDisconnect(User)" into wsapp.interface.php so every application can manage active clients (browsers).

    Good think is also edit onMessage(msg) in wsapp.interface.php to onMessage(msg,User). So application would know about which client is sending message and can react to that.

    This way you can manage behavior in each application. One can broadcast message thru iteration over clients. Second can just send message back to one client. Third can group clients and send messages only to his group etc.

    And one more question, do you have plan about adding flash socket support?

    And if i can ask you