Socket server in PHP

Posted on March 29, 2010

A socket UK plug

Recently I had to develop a chat room system very quickly. I absolutely wanted to use my favorite language: PHP. So I had to figure out how do sockets work in PHP, and how to give PHP a daemon behavior (read: Give PHP a daemon behavior).

I found many resources on the Internet, more or less exact, more or less commented… All the articles I read were going around the same solution, and actually by having read some other articles about sockets in C and Java afterwards, it’s indeed the same logic for the 3 languages.

I won’t explain the principles of sockets, nor how to implement them in PHP, it’s already fully documented on the Net (see at the bottom of the article). However, as I wrote a class to help me creating socket servers in PHP, I’m going to share it and explain how to use it.

Step 1: download the class file

Download the Socket Server class:

Step 2: extend ClsSocketServer

class MyChatServer extends ClsSocketServer { }

Step 3: instantiate and run the start() method

$chatServer = new MyChatServer('localhost',9999,20);
You’re done. The arguments for the constructor are easily guessable: the host on which your socket server will run, the port it will use and the maximum number of connected clients allowed.

Step 4: give your server the behavior you like

Now you have a socket server, you want to give it the behavior you like (making a chat room server, which is the easiest thing to do, or implementing a game server which is more tricky… the only limit being your imagination). These are the methods you can overload:


Don’t forget to call parent::_construct() when you overload the constructor! Otherwise the server won’t even start. /**
* Constructor
* @param string $address
* @param int $port
* @param int $maxClients
* @return ClsSocketServer
public function __construct($address,$port,$maxClients);


This method is called when a new client connects to your server. $socket is the resource link to the client’s socket (each client has one resource link) /**
* Method called after a new client is connected
* @param resource $socket
abstract protected function onClientConnected($socket);


This method is called when a client disconnects from your server (he closes the connection). You can also disconnect him in your script with the disconnect($socket) method. $socket is the resource link to the client’s socket. /**
* Method called after a new client is disconnected
* @param resource $socket
abstract protected function onClientDisconnected($socket);


This method is called when a client sends some data. $socket is the resource link of that client, $data is the string that the client sent. /**
* Method called after a value had been read
* @abstract
* @param resource $socket
* @param string $data
abstract protected function onDataReceived($socket,$data);
The following methods are the methods you can use in your class, but you shouldn’t overload them:


Call this method to send data to a client. $data is the string you want to send to him. /**
* Send data to a client
* @param resource $client
* @param string $data
* @return bool
protected function send($client,$data);


This method will send the string $data to all the connected clients. /**
* Send data to everybody
* @param string $data
* @return bool
protected function sendBroadcast($data);


This method will disconnect a client /**
* Disconnect a client
* @param resource $client
* @return bool
protected function disconnect($client);


This method will start the server /**
* Start the server
public function start();


This method will stop the server. All the connected clients will be disconnected. /**
* Stop the server: disconnect all the coonected clients, close the master socket
public function stop();
Et voil√†! Lots of things can be improved, and features can be added, but this might be a good beginning of framework that will fit to many socket-based servers in PHP. I’m waiting for your comments and ideas if any.


About the author

Cyril Mazur is a serial web entrepreneur with experience in various fields: online dating, forex & finance, blogging, online advertising... who enjoys building things that people like to use.


  1. on January 19, 2011
    i run this code as server, but i can't connect as a client when I'm trying to connect via client and although the server running and waiting clients to connect it gives can't connect on the given port.
    could you provide with client code?

  2. on February 18, 2011
    I am new to Flash having enough experience. I used the above code and try. It working well in the flash file means when hitting ctrl+enter its working good. But after publishing if i use the same in HTML it says Security Error. The Actual error am getting while running the html file is
    "Flash is trying to connect...<b>FLASH HAS ENCOUNTERED A SECURITY ERROR.</b>"

    Can anyone know how to solve this issue?

    Also let me know some other similar socket server for PHP.

    Thanks in advance.

  3. on November 9, 2011

    I have tried it but it doesnt work. For some years I have written an simple socket server. I know it runs inside of an infinite loop, but this server not. I run the source and it loads very quick. And it gives some errors when I execute example.php:

    Fatal error: Access level to ClsMyExampleServer::onDataReceived() must be protected (as in class ClsSocketServer) or weaker in C:\MoWeS\www\PhpProject1\server\ClsMyExampleServer.php on line 43

    After this error I have change the protected to public and the effect was that which was written above.


  4. on May 8, 2014
    Very Nice Tutorial.
    Hope we could read even more of these.

    For all others will try this piece of code.
    You must initiate the exempel Class and not the abstract class.
    There is no reason to change very importend protected flag of the class.

  5. on April 1, 2015
    It helped me alot. Thanks for shared :)

Leave a Reply