With nodejs, it is very easy to create a non-blocking TCP server. Example from nodejs.org:
var net = require('net'); var server = net.createServer(function (socket) { socket.write("Echo server\r\n"); socket.pipe(socket); }); server.listen(1337, "127.0.0.1")
nodejs handles the material select () - / poll () - / epoll () for you, the socket routines and the main loop are implemented in C, so it is very fast and efficient.
nodejs is great, but I would like to implement a high-performance TCP socket server in PHP, because I'm a PHP guy :)
So, one thing I've already tried is to implement socket routines in PHP, with socket_create_listen , socket_accept , socket_select , etc. and the main loop in PHP. This works very well, but I donβt think it is very efficient because I have to use socket_select , which internally calls the select C function, but epoll will be better, I think (I use Linux), but epoll not available as a PHP function. ( phpsocketdaemon and phpmio are 2 projects I have discovered that they implement socket routines for you).
Can this be done with the nodejs method? I am thinking of a PHP module that implements the loop and socket routines in C and calls the PHP callback functions for events ( onread , onerror ..). Or is it not worth the effort?
source share