Node.JS
So, despite the fact that I know excessively little about how to work Node.JS, especially when coming from a PHP background - I am beyond obsessed with the language.
So it thrills me to no end that there is now a Node.JS executable for Windows.
Of course, things kind of suck when you don't have the super simple power of NPM, but that's life for ya.
I understand the basics of Node.JS (If you've ever touched JavaScript, its hard NOT to); but some fine details such as templating elude me. Maybe you're not supposed to template? Maybe you're just supposed to serve up some static HTML that then queries the Node.JS server for the information it needs? Oh.. now there's a thought.. program a single frontend that just talks back and forth with the backend. Kind of like Google+, actually.
Is that what I'm supposed to do? God I love this language.
Something Chrome Needs
There is a very common type of extension for Google Chrome, and that happens to be the “Notifier” type.
You have GMail Notifiers, Google Voice Notifiers, Google Reader, Google Docs, OWA, Facebook, Twitter, etc etc. Wouldn’t it be nice if Google Chrome just had a single Notification Center with a fantastic User Interface for showing notifications from whatever services register themselves with it?
I think it would, and over the summer – if I have spare time – I think I’m going to program an extension that will can accept additions for showing notifications. I’ll then ask around for help and/or program some basic services for it to use, and post it to the Chrome WebApp Store.
My next questions are:
- Would you use this? Is it a good idea?
- Would you code for this?
- Would you purchase this for $1?
At the moment, I’ve got plenty of cool programming projects, and I’m not making money with any of them. GVOMS is almost entirely free (no companies have purchased a license to use), and anything else I’ve done so far has been free and open source.
So unless people are going to start donating to me – which I’m pretty sure you aren’t – I need to find some way to make money. I’m a college student, after all.
And I don’t think $1 would be too much to ask for a unified notification center in Google Chrome, do you?
How to create a socket server in PHP

EDIT: If you need to submit bugs or want to improve my work, its now available on github!
Ever tried searching for information on how to properly create a multi-client socket server in PHP? You’ll get plenty of results with outdated and messy source code, some of which won’t even work.
This was the my state a couple days ago when I decided that I wanted to build an IRC server. The why is not important… (For the fun of building an IRC Server). So I googled around a hell of a lot bit until I finally found some code that worked on its own, and quickly built a semi-functional IRC server using it, and headed off to sleep at 5am.
The next day I was very, very happy with the results of my hard labor, but it wasn’t good enough, so I started re-writing it from scratch as an Object, and thus I created class::IRCServer.
Today, once I felt that I was finished screwing around with my newly built IRCd, I decided to modify the function enough to be used on its own as a socket server, to share with the world.
And thus, class::SocketServer was created.
< ?php /*! @class SocketServer @author Navarr Barnier @abstract A Framework for creating a multi-client server using the PHP language. */ class SocketServer { /*! @var config @abstract Array - an array of configuration information used by the server. */ protected $config; /*! @var hooks @abstract Array - a dictionary of hooks and the callbacks attached to them. */ protected $hooks; /*! @var master_socket @abstract resource - The master socket used by the server. */ protected $master_socket; /*! @var max_clients @abstract unsigned int - The maximum number of clients allowed to connect. */ public $max_clients = 10; /*! @var max_read @abstract unsigned int - The maximum number of bytes to read from a socket at a single time. */ public $max_read = 1024; /*! @var clients @abstract Array - an array of connected clients. */ public $clients; /*! @function __construct @abstract Creates the socket and starts listening to it. @param string - IP Address to bind to, NULL for default. @param int - Port to bind to @result void */ public function __construct($bind_ip,$port) { set_time_limit(0); $this->hooks = array(); $this->config["ip"] = $bind_ip; $this->config["port"] = $port; $this->master_socket = socket_create(AF_INET, SOCK_STREAM, 0); socket_bind($this->master_socket,$this->config["ip"],$this->config["port"]) or die("Issue Binding"); socket_getsockname($this->master_socket,$bind_ip,$port); socket_listen($this->master_socket); SocketServer::debug("Listenting for connections on {$bind_ip}:{$port}"); } /*! @function hook @abstract Adds a function to be called whenever a certain action happens. Can be extended in your implementation. @param string - Command @param callback- Function to Call. @see unhook @see trigger_hooks @result void */ public function hook($command,$function) { $command = strtoupper($command); if(!isset($this->hooks[$command])) { $this->hooks[$command] = array(); } $k = array_search($function,$this->hooks[$command]); if($k === FALSE) { $this->hooks[$command][] = $function; } } /*! @function unhook @abstract Deletes a function from the call list for a certain action. Can be extended in your implementation. @param string - Command @param callback- Function to Delete from Call List @see hook @see trigger_hooks @result void */ public function unhook($command = NULL,$function) { $command = strtoupper($command); if($command !== NULL) { $k = array_search($function,$this->hooks[$command]); if($k !== FALSE) { unset($this->hooks[$command][$k]); } } else { $k = array_search($this->user_funcs,$function); if($k !== FALSE) { unset($this->user_funcs[$k]); } } } /*! @function loop_once @abstract Runs the class's actions once. @discussion Should only be used if you want to run additional checks during server operation. Otherwise, use infinite_loop() @param void @see infinite_loop @result bool - True */ public function loop_once() { // Setup Clients Listen Socket For Reading $read[0] = $this->master_socket; for($i = 0; $i < $this->max_clients; $i++) { if(isset($this->clients[$i])) { $read[$i + 1] = $this->clients[$i]->socket; } } // Set up a blocking call to socket_select if(socket_select($read,$write = NULL, $except = NULL, $tv_sec = 5) < 1) { // SocketServer::debug("Problem blocking socket_select?"); return true; } // Handle new Connections if(in_array($this->master_socket, $read)) { for($i = 0; $i < $this->max_clients; $i++) { if(empty($this->clients[$i])) { $temp_sock = $this->master_socket; $this->clients[$i] = new SocketServerClient($this->master_socket,$i); $this->trigger_hooks("CONNECT",$this->clients[$i],""); break; } elseif($i == ($this->max_clients-1)) { SocketServer::debug("Too many clients... "); } } } // Handle Input for($i = 0; $i < $this->max_clients; $i++) // for each client { if(isset($this->clients[$i])) { if(in_array($this->clients[$i]->socket, $read)) { $input = socket_read($this->clients[$i]->socket, $this->max_read); if($input == null) { $this->disconnect($i); } else { SocketServer::debug("{$i}@{$this->clients[$i]->ip} --> {$input}"); $this->trigger_hooks("INPUT",$this->clients[$i],$input); } } } } return true; } /*! @function disconnect @abstract Disconnects a client from the server. @param int - Index of the client to disconnect. @param string - Message to send to the hooks @result void */ public function disconnect($client_index,$message = "") { $i = $client_index; SocketServer::debug("Client {$i} from {$this->clients[$i]->ip} Disconnecting"); $this->trigger_hooks("DISCONNECT",$this->clients[$i],$message); $this->clients[$i]->destroy(); unset($this->clients[$i]); } /*! @function trigger_hooks @abstract Triggers Hooks for a certain command. @param string - Command who's hooks you want to trigger. @param object - The client who activated this command. @param string - The input from the client, or a message to be sent to the hooks. @result void */ public function trigger_hooks($command,&$client,$input) { if(isset($this->hooks[$command])) { foreach($this->hooks[$command] as $function) { SocketServer::debug("Triggering Hook '{$function}' for '{$command}'"); $continue = call_user_func($function,$this,$client,$input); if($continue === FALSE) { break; } } } } /*! @function infinite_loop @abstract Runs the server code until the server is shut down. @see loop_once @param void @result void */ public function infinite_loop() { $test = true; do { $test = $this->loop_once(); } while($test); } /*! @function debug @static @abstract Outputs Text directly. @discussion Yeah, should probably make a way to turn this off. @param string - Text to Output @result void */ public static function debug($text) { echo("{$text}\r\n"); } /*! @function socket_write_smart @static @abstract Writes data to the socket, including the length of the data, and ends it with a CRLF unless specified. @discussion It is perfectly valid for socket_write_smart to return zero which means no bytes have been written. Be sure to use the === operator to check for FALSE in case of an error. @param resource- Socket Instance @param string - Data to write to the socket. @param string - Data to end the line with. Specify a "" if you don't want a line end sent. @result mixed - Returns the number of bytes successfully written to the socket or FALSE on failure. The error code can be retrieved with socket_last_error(). This code may be passed to socket_strerror() to get a textual explanation of the error. */ public static function socket_write_smart(&$sock,$string,$crlf = "\r\n") { SocketServer::debug("< -- {$string}"); if($crlf) { $string = "{$string}{$crlf}"; } return socket_write($sock,$string,strlen($string)); } /*! @function __get @abstract Magic Method used for allowing the reading of protected variables. @discussion You never need to use this method, simply calling $server->variable works because of this method's existence. @param string - Variable to retrieve @result mixed - Returns the reference to the variable called. */ function &__get($name) { return $this->{$name}; } } /*! @class SocketServerClient @author Navarr Barnier @abstract A Client Instance for use with SocketServer */ class SocketServerClient { /*! @var socket @abstract resource - The client's socket resource, for sending and receiving data with. */ protected $socket; /*! @var ip @abstract string - The client's IP address, as seen by the server. */ protected $ip; /*! @var hostname @abstract string - The client's hostname, as seen by the server. @discussion This variable is only set after calling lookup_hostname, as hostname lookups can take up a decent amount of time. @see lookup_hostname */ protected $hostname; /*! @var server_clients_index @abstract int - The index of this client in the SocketServer's client array. */ protected $server_clients_index; /*! @function __construct @param resource- The resource of the socket the client is connecting by, generally the master socket. @param int - The Index in the Server's client array. @result void */ public function __construct(&$socket,$i) { $this->server_clients_index = $i; $this->socket = socket_accept($socket) or die("Failed to Accept"); SocketServer::debug("New Client Connected"); socket_getpeername($this->socket,$ip); $this->ip = $ip; } /*! @function lookup_hostname @abstract Searches for the user's hostname and stores the result to hostname. @see hostname @param void @result string - The hostname on success or the IP address on failure. */ public function lookup_hostname() { $this->hostname = gethostbyaddr($this->ip); return $this->hostname; } /*! @function destroy @abstract Closes the socket. Thats pretty much it. @param void @result void */ public function destroy() { socket_close($this->socket); } function &__get($name) { return $this->{$name}; } function __isset($name) { return isset($this->{$name}); } }
class::SocketServer does all the functions necessary for a server. It binds to the IP address and starts listening to the port. Its easy to specify a maximum number of clients to allow, and the way its coded makes it easily modified.
Here is an example of a server (using this class) that listens for a user to send a string, and then echoes the reverse of that string back to the user.
< ?php // This is PHP5 Code, by the way. require_once("SocketServer.class.php"); // Include the Class File $server = new SocketServer(null,31337); // Create a Server binding to the default IP address (null) and listen to port 31337 for connections $server->max_clients = 10; // Allow no more than 10 people to connect at a time $server->hook("CONNECT","handle_connect"); // Run handle_connect everytime someone connects $server->hook("INPUT","handle_input"); // Run handle_input whenever text is sent to the server $server->infinite_loop(); // Run Server Code Until Process is terminated. /* * All hooked functions are sent the parameters $server (The server class), $client (the connection), and $input (anything sent, if anything was sent) * You should save the variables $server and $client using an ampersand (&) to make sure they are references to the objects and not duplications. */ function handle_connect(&$server,&$client,$input) { SocketServer::socket_write_smart($client->socket,"String? ",""); // Outputs 'String? ' without a Line Ending } function handle_input(&$server,&$client,$input) { $trim = trim($input); // Trim the input, Remove Line Endings and Extra Whitespace. if(strtolower($trim) == "quit") // User Wants to quit the server { SocketServer::socket_write_smart($client->socket,"Oh... Goodbye..."); // Give the user a sad goodbye message, meany! $server->disconnect($client->server_clients_index); // Disconnect this client. return; // Ends the function } $output = strrev($trim); // Reverse the String SocketServer::socket_write_smart($client->socket,$output); // Send the Client back the String SocketServer::socket_write_smart($client->socket,"String? ",""); // Request Another String }
In essence, this class allows you to handle sockets in PHP. Beautifully handle sockets in PHP, that is.
Magical Typesetting in PHP
So, well working for Route 50 I came up with a fantastic idea for “typesetting” that well exceeded the norm. Something we constantly have issues with is what type of string was sent to MySQL originally (some of us have different conventional ideas about where escaping HTML should be located.) as well as outputting that string in its correct format.
Me, with my fantastic idea, came up with a couple variables classes that I put in a file named class_typesetting.php. The version on gist.github is slightly modified from the original version on the server.
It creates three classes, GenericVariable, String, and Number. So far we haven’t used GenericVariable, but since the introduction of the classes I’ve taken it upon myself to introduce them to any new code I write. When we create Core v5 (which will Objectify everything) strings taken from SQL will automatically be re-stored as String class variables.
First, lets examine some useful functionality.
<?php $title = new String($_POST[“title”]); // <strong>Hello 'World'</strong> ?> HTML Output: <?= $title->html ?> (<strong>Hello 'World'</strong>) Text Output: <?= $title->text ?> (Hello 'World') SQL Output: <?= $title->sql ?> (<strong>Hello \'World\'</strong>) HTML Attribute Output: <?= $title->html_attr ?>(<strong>Hello 'World'</strong>)
This allows for quick and easy access to the variables without having to worry about escaping them.
I recommend you hit the download link (class_typesetting.php) and play around with it. Tell me about anything that’s not working correctly and if possible implement it in your future code. (This means I’m putting this code in the “Public Domain”).
Making Google Analytics Work in XHTML
I was moving my website, Google Voice for Outlook, over from HTML5 to XHTML5 today, and as soon as I did the basic content negotiation filters in PHP so that it would send the appropriate headers if the client supported XHTML as well as only outputting the <?xml if the client supported XHTML, I checked my developer tools to find a JavaScript error. It was Google Analytics, of course. document.write doesn’t exist in XHTML, after all.
The fix was simple, replace the current four line inclusion code with:
<script type="text/javascript"> var gaJsHost = (("https:" == document.location.protocol) ? "https://ssl." : "http://www."); var script = document.createElement("script"); script.src = gaJsHost + "google-analytics.com/ga.js"; script.type = "text/javascript"; document.getElementsByTagName("head")[0].appendChild(script); </script>
This will work fine, unless of course you have no head tag. In which case you should replace getElementsByTagName(“head”) with getElementsByTagName(“html”).
TwCLI
So, you think you’ve had a lot of fun with twitter on the web and all those twitter clients you’ve played around with? What if I told you that you haven’t seen anything yet? What if I told you that you could use Twitter in a TRUE Command Line Interface with specific commands for interacting with twitter.
Welcome to one of my latest and greatest creations, TwCLI.
TwCLI supports almost everything twitter has to offer, and will soon be expanding to support even more!
TwCLI includes a long list of commands, help information for each command, a theme-able interface (Specify a Pre-Determined theme, import from your twitter profile, or even specify an external CSS file!), Geo-Location, Retweets, and even Contributor Support!
Go ahead, give it a try and tell me what you think!
simpleTAPI is Broken
Apparently I’ve completely broken simpleTAPI somewhere between Build 27 and Build 30. I thought I had fixed it with Build 29, but it seems that I was mistaken.
In lieu of this, I am putting simpleTAPI on a temporary hiatus. I will be re-constructing it from scratch (though, probably looking back and using a good bit of the original code). The next version should have several configurable options, and will hopefully interact with the Twitter API much better than the previous versions.
Build 30 was supposed to return results as an array([“TAPI”] => data, [“result”] => data). But all I’m getting from it at the moment is “Unable to Authenticate User.”
Those wanting to use simpleTAPI should use Build 27, though you will have to deal with some minor quirks in the way results are returned. (the TAPI array is simply appended to the results array, making things slightly complicated if you don’t unset($result[“TAPI”]);
What will be simpleTAPI 0.4 should have better error handling, better return data, and better built-in caching. I’m also hoping to build in support for xAuth and Delegated OAuth, if at all possible. (Though probably not since simpleTAPI is built upon another OAuth library).
So, I’m asking for any and all feature requests. Is there something about simpleTAPI you don’t like or want to be improved? Please, post in the comments below!
A Quick Update to Simple Twitter
A lot of people use my Simple Twitter Feed written in JavaScript (for some reason). Well, today I pushed out a quick update that should fix all the woes users have given me in the past.
The code should now be valid XHTML strict, and I know longer use innerHTML for each list element. Instead, I’ve moved from adding the list elements via innerHTML to DOM Manipulation (appendChild). I’m not sure exactly what the benefits of this are, but I’m sure they exist.
As the previous HTML code seems to have been broken, this may cause some rendering errors for a few websites, but all in all it should work better than it has previously.
I’m not writing out a whole changelog, I’m just going ahead and saying that some changes were made – and hopefully Simple Twitter should work a lot easier for everyone using it.
Webkit JavaScript Notifications API
Something I learned about recently by following the updates being issued to Chrome, is that with today’s release they also pushed out the Webkit Notifications API to Chrome Stable (v4). Surprisingly, this is actually the first I’ve heard of it’s existence. I took a look and played around with it a bit, and it is qué cool.
Visit my Sandbox to see the code in action, or continue reading for some code excerpts.
Hunting Down the Bugs – TwCLI on Chrome for Linux Beta
This is the first post of a new series, looking at some of the odder bugs encountered while developing for the expanding Web, no matter how basic a bug it may be.
Thanks to twitter user @paperfairy, a bug was discovered on my Command Line Twitter Client, TwCLI.
For some reason, when submitting a command in Chrome for Linux, the page would simply refresh, and the command would never be sent. At first, I had no possible way to track down this bug. I didn’t have a linux box (with a GUI, anyway) so I simply told him that it was unfortunate, but it’d have to stay a bug. Until a recent post on lifehacker brought my attention to Portable Ubuntu. I immediately installed it, opened up the Chrome website in Firiefox, installed Chrome Beta, and headed over to TwCLI to see what was amiss.
Of course, it was a single line in a detection script to send Geo-Data to Twitter (as long as the user approved it, of course):
else if(google.gears) {
This single line was throwing an exception I hadn’t encountered in other browsers – Google wasn’t defined. Oddly, I thought it would handle that properly, since google wasn’t defined, it would just skip over it, but instead it threw an error and halted all further javascript code.
The fix was simpler than tracking down the bug, I simply had to add this to the start of the javascript code:
try{ if(!google) { google = 0; } } catch(err) { google = 0; }
And voila, I had both a check for google, and a catch if it decided to throw errors while checking for it.
Whether this is a Chrome bug or not, I don’t know – I simply don’t know enough about JavaScript in order to say so either way. But, are undefined variables supposed to throw errors, or are they simply supposed to return false?