Comments Blog About Development Research Sites

Writing a (multithreaded) socket server in PHP

Jul 15, 2009
Recently I came across this question on a forum I frequent: can you use sockets in PHP, and why would you?

PHP has sockets like many other languages. Their use is diverse, from creating gameservers to chatrooms to simple proxies. You can use a Flash client to talk to your PHP server directly through sockets, bypassing a huge load of overhead created by a webserver. Additionally, using PHP sockets you can create a website that recieves updates not by constant polling, like AJAX, but by a push from the server - this technique is sometimes called COMET and is very, very awesome in many regards.

A while ago I worked on a socket gameserver in PHP, and a basic version of it can still be found here - complete with connectserv, monsterserver and authserv. Some parts are quite well finished and polished, others are barely working, let alone documented, but you should be able to get the general idea from it if you so wish. However, I have since discontinued work on that project for a simple reason: PHP is, by nature, singlethreaded.

You can run PHP in commandline, bypassing a webserver alltogether. You can even start a new PHP instance from within a PHP instance, giving you an alternative to using threads (which are unsuported on windows and only marginally suported under linux). A quick search learns us that indeed some people have tried this approach, but as he justly notes at the end of that article, it is not true multithreading. Overall, opening several PHP instances is a poor substitute, especially since you can only open a socket listener on an empty port. Inherent in this then is the fact that your clients will inevitably be waiting on eachother - not so much if you code well, but still a long or slow databurst from one client will delay the others. There are workarounds, of course, but few and laborous.

I have said, and still claim, that PHP can do pretty much anything. But a socketserver in PHP is simply using the wrong tool for the job - as an old saying goes, if all you have is a hammer, everything looks like a nail. A while ago I started work on a new socketserver, this time in JAVA, and it has been simply awesome. Multithreading is a breeze, the language feels almost designed for the purpose of creating socketservers with (which might actually even be the case) and the time you waste writing exception handlers is gained by not having to work around an inherent problem in PHP.

Long story short: yes, you can use Sockets in PHP, but no, you really shouldn't.

FragFrog out!

Oct 17, 2009 lovespring

thanks, so I know.

Apr 27, 2010 YEAH...

I used a finished server that worked kinda wel.. i think it was caled PEAR or something like that. However no provider will give you the required freedom to set it up. So you bassically need your own server, which means that you can use java so why use PHP. PHP is easy and all but JAVA is better for a socketserver.

May 13, 2010 Matthijs

PEAR is a collection of (open source) scripts and libraries for PHP - didn't know they had a socketserver as well, but I guess I shouldn't be surprised by that :]

Currently using the JAVA socketserver in a production environment, pretty awesome stuff.

New comment

Your name:
Comment: