Recently, as I was working on a Rails application, I found myself needing to generate a random 5 digit numeric code in order to verify a user entered phone number. Essentially I needed to generate a code that I could have them text to my app from their entered phone number in order to ensure it was theirs. Additionally, I needed to mark the number as “not verified” and generate a new code automatically in the event that they changed it. To handle this, I elected to use ActiveRecord hooks in my model file. It works perfectly, and handles everything automatically in just a few lines of code.
While I had long known about the presence of these hooks, I had not known about the magic of the _was modifier. I could try to explain it, but instead I’ll provide you with a code sample that uses it.
So now, when a new row in our model is created, we automatically create our 5 digit, zero padded verification code. When the row is edited, we check to see if the phone number has changed using the _was syntax and if so, we generate a new code and indicate that their phone is no longer verified. Simple right?
I haven’t had a ton of time to work on my node.js project lately so I figured I’d just put it out there in its current form as a learning tool for others. In this blog post I’ll walk you through everything I did to get it working and, by the end, you should be able to write your own basic webapps using socket.io.
First, let’s start with an overview of the functionality we’d like to have. We want a semi-real time news feed that can display messages passed in from one or more client applications. In order to tell which application passed in which message, we’d like to have some color coding as well. Since we’re using socket.io we also want to avoid AJAX callbacks in favor of server pushes. Finally - it would be cool to see a count of the number of other people also viewing the news feed.
For this project I’m going to use the Jade templating language, and the Express node.js framework, which provides Sinatra style routing for node applications. Links to each can be found in the project readme.
Lets start off by creating our app and setting it up to use Jade, socket.io, and Express.
We need to import the Express library and call the createServer() method on it to get a server object. We’ll store it in app. Then we import the socket.io library and attatch it to listen on our newly created server. Finally we import the Jade library, set our app to use it as the view engine and configure some basic settings.
link(rel="stylesheet", type="text/css", href="/main.css")
h1 News Feed
| connected clients
If we were to run the node server now, it would work, but it wouldn’t do anything - it would just render out our basically empty view. Not particularly useful. Now that we have the basic structure in place however, the fun can begin. Let’s get socket.io hooked up and talking to the clients. We’ll start by implementing a real-time count of connected clients. On the server side it will look something like this:
We will bind an anonymous function to the socket ‘connection’ event that will execute every time a new client connects. When this happens we will increment the current count of connected clients, and then send out a broadcast containing the count. The socket.broadcast method sends a message to all connected clients. In this case we’re passing some JSON that contains our count. We then use the same syntax to bind a function to execute when the client disconnects. This function simply decrements the count and broadcasts the count back to the remaining clients.
This code will need to go in the feed.js file we linked from the main view. We’re using JQuery’s document ready function to set up our client side socket and cache the selector for the client count on our page. Node.js runs on port 3000 by default so that’s what we’ll enter here. We’ll then bind a function to execute on the socket’s ‘message’ event. This seems like overkill now as the function is only one line, we could just as easily declare it inline anonymously, but we’ll be building on it in the next step. For now we’ll just update the client count.
At this point we have a working app - you can start up node.js, navigate to localhost:3000 and see: 1 connected client. If you open the page in another tab, you’ll see the counter update to 2 on both pages. Likewise, it will decrement as you close tabs. So far we’ve seen how to set up a very simple node.js app, import the Jade, Express, and socket.io libraries, and pass messages from the server to the clients using “comet” style message pushes. Now that we have the basics down, we’ll work on our news feed in the next post.
Hope you found this helpful - if you want to skip ahead and see the ‘finished’ product you can take a look at the github repo, otherwise we’ll cover the rest in the next post.
While doing the excellent Rails for Zombies screencasts I noticed how nice the color scheme they were using for the embedded code editor was. A little digging through their page source informed me that they were using Mozilla’s Bespin. From there I googled for an existing vim colorscheme that mimicked Bespin. While I wasn’t able to find one - I did find this post where the author had created an emacs theme from a Textmate version of the theme. I ran the Textmate theme file through the excellent coloration gem and ended up with a pretty decent vim theme. I thought I’d share it with anyone else that may want it.
As of last night my blog is completely powered by Jekyll. I made the switch for two reasons: performance and ease of use.
Going with Jekyll has allowed me to shut down MySQL as well as all of the PHP-FPM processes that were previously required to run Wordpress. This saves a substantial amount of RAM and allows more overhead for spikes in traffic. This would have been especially useful last week when one of my posts hit position 3 on hackernews and the frontpage of /r/programming on reddit. Fortunately, I was able to avoid any real downtime - but with barely any RAM to spare. With this setup I should be able to sustain that amount of traffic again easily - all on a small linode instance. All that is required to run the blog now is nginx, which simply serves up static files. Add in some intelligent compression and caching settings and you have a recipie for lightning fast performance with minimal overhead.
For my purposes, Jekyll is also much easier to use. I set up a Rakefile with tasks to publish and create new posts. I can create a post, write and edit it in vim, and then publish all from the command line. This allows me to be super efficient in my workflow and use tools that I’m familiar with. Versions are tracked in git, posts are edited in vim, and publishes happen over scp.
So far the transition has worked quite well, the permalink structure changed slightly which, unfortunately, means I lost the disqus threads for my old posts - but that’s been the only negative so far.
My node.js project is coming along well and I am in the process of writing up a couple of detailed blog posts about it, but in the meantime I’ll leave you with this rant about SOAP.
While at work today I was writing some code to interface with Microsoft’s AdCenter API which, if you didn’t know, utilizes SOAP. My end goal was to POST some data to AdCenter, I had approximately 100 entries, each consisting of three primitive data fields (two 64 bit ints and a string). With any modern, usable, API this would be a trivial task completable in 15 minutes or less - but oh no. Not with SOAP.
For starters you have to point Visual Studio (sigh) to the service reference, at which point nine thousand (yes, that’s nine-zero-zero-zero) lines of code are generated. Next you have to write code to construct your objects and initialize your SOAP client. Only then are you able to actually use the API.
Anyways, all of this went more or less according to plan until I actually tried to run the code. I got an error. Not a descriptive error, or an HTTP status code, or anything that could be used to track down the problem but a “SOAP Error.” That was it. The whole text of the error from the Microsoft API was “SOAP Error.” Well, “no problem” I thought - surely I could step into the method I was calling using my debugger and see what was happening. Unfortunately I was wrong. All I really wanted was the ability to see the XML envelopes that were being generated so I could find out what was happening but alas, Visual Studio abstracts this away from you entirely. I asked a co worker and he helpfully pointed me to some web resources on how to inspect SOAP messages. The absurdity of the solution blew my mind. Keep in mind all I wanted to do is SEE what messages are being passed to the API - a simple enough request.
Long story short, in order to inspect SOAP messages you have to write a class that implements the ICLientMessageInspector interface. This class must implement the AfterReceiveReply and BeforeSendRequest methods. As you’ll see in my example code, I simply output the message to the console and write it to a file so I can look at it later.
Okay, simple enough, but how do we hook it up? Well, for that we need to create another class. This one must implement the IEndpointBehavior interface. There are four methods we must implement in order to use this interface - but we only care about one of them: ApplyClientBehavior. We need to add our newly created message inspector to the list of client message inspectors.
Alright, now we’ve done that - but our code still doesn’t actually do anything. What we need to do next is enable our newly created behavior. You can do this one of two ways - in your application configuration or programmatically. I chose the latter. To do this you add your new behavior to your instance of the SOAP client like so:
Now, finally, the SOAP XML envelopes that our client sends and receives will be written to a file and to the output window for inspection. As you can see, the code is dead simple, but its incredibly frustrating to be forced to jump through hoops in order to do something as basic as seeing what messages your application is sending and receiving. As a proponent of RESTful interfaces its even more frustrating because I’m used to simply typing curl "resourcename" > file to accomplish the exact same functionality.
In my mind SOAP’s biggest failure is what some people consider to be its biggest success - it abstracts away all notion of the underlying HTTP model and replaces it with crutches like code generation.
As an aside - I later ditched the code I’ve shown here and just went with Wireshark. I was able to capture and reassemble the XML messages with it, and it provided a much nicer interface for searching and sorting them.