A programmer building a significantly more ambitious system than simple Chat might need more control and performance than PHP 4 offers. It would be worthwhile to consider programming environments that have more developed threading and networking features.
Java and C are obvious options, but there are open -source scripting choices as well. Python and, interestingly, Tcl offer approaches that might fit well with the applications requirements. This is especially true if your requirements include both a robust socket feature set and a simple scripting learning curve.
TCL, like PHP, is a server-side command interpreter. Like PHP it is an open-source system that began as the work of one or two academics and now has a real commercial presence. Unlike PHP, Tcl has a fairly quirky syntax that owes little to C++ or any of the 'MLs.
It was invented pretty deliberately in the late 1980s by John Ousterhout (University of California Berkeley) as a generic scripting language called the Tool command language. The language became popular among Unix system administrators. Sun Microsystems hired Ousterhout and gave him a staff. They supported the language as an extremely early web development tool. Eventually the project spun out of Sun into its own small company. It is still headed by Dr. Ousterhout.
The sojourn at Sun was fortunate. Nowhere is networking software better understood , and it was there that Levy and Stanton added superb socket functionality to the language.
This suite of socket commands is available to create a Tcl-based server:
socketserver handler port
This creates a listener socket on the server at the specified port. When a connection request arrives from a remote client, and is successful, the callback handler is launched.
fconfigure channel options
Sets or queries many different I/O flow modes for a given socket ( channel in Tcl jargon). Especially important for our purposes is control over blocking ”which functions fine. There is also control over buffering and translation, and the read-only options sockname and peername which identify the process with whom you are communicating.
fileevent channel state handler
Registers an I/O event handler for a channel that is in nonblocking mode. When an event puts the channel into either the readable or writable state, the appropriate handler script will be called.
Puts Tcl in purely event-driven mode until the value of variable is changed (by one of the event handlers.) Unless this mechanism is used, the sockets cannot operate in non-blocking mode.
puts channel string
Write string to channel
gets channel variable
Read an incoming line from channel and store it in variable.
Tests to see if input from channel has ended.
Close the socket.
Glancing at the functions available to the Tcl programmer leaves us a bit wistful. Our polling PHP implementation is not so elegant: It stomps through the list of clients collecting messages and then again distributes them.
Even within PHP, there are other approaches that might be worth exploring. At the time of writing, PHP had undocumented interface to the Unix select() command, which allows the program to stand back and monitor the activity of a network of sockets. This is a useful tool when creating a high-performance multiplexed server for XML sockets.
Third Party Options
Commercial, open source and experimental XMLSocket Servers are available to brave developers. None is dominate ”even the best is barely stable at this point, but the demand is clear and the market is responding. Among the commercial servers, FlashNexus ”not functional at presstime ”is pursuing an interesting direction. Rather than developing multiplexed XMLSocket Server, they are developing a bridge to the IRC network, where an enormous and extremely stable infrastructure exists which supports thousands of users simultaneously . A similar bridge has been built entirely in PHP by Sascha Schumann. Check online for the latest.