Connect Coldfusion and Ruby via Socket

This week a read a lot about ZeroMQ( It’s a very complex and elegant way to connect applications via a message queue service. What I like most is the possible connection on a single host or over multiple hosts. The reason why I was interested in this topic was the search of an easy way to connect Coldfusion to ruby.

We have lot of code running on Coldfusion and we want to migrate a lot of complex parts from it to a more performant environment. On the other hand we want to keep the easy HTML-templating system Coldfusion is providing. This means to create a ruby library to be the base of the new system and having Coldfusion on top of it.

Part 1: Connecting Coldfusion and ruby

While reading the zeroMQ documentation I got the idea of trying the same approche of using the TCP socket in a simpler way. So I was looking for different Socket examples for Coldfusion. In contrast to the most available articles, I want to use Coldfusion as a Socket Client and ruby as a Socket Server.


Creating the ruby Socket Server

At first I started with the ruby server, because later I want that server to receive and return the data. For now I’m only fine with the transfer of text based data.

require 'socket'

def incommingMessage(client)
    data = ""
    incomming_length = 56
    while( tmp = client.recv(incomming_length) )
        data += tmp
        break if tmp.length < incomming_length
    return data

def init()
    server =
    loop {
        client = server.accept
        puts incommingMessage(client)
        client.puts "Hi from ruby, cya!"


Start the server: ruby socket.rb

Creating the Coldfusion client

After having the ruby Socket server running I was concentrating on the Coldfusion client part. For that I downloaded and startet the Express Version of Railo 4.2, which can be found here:

private string function callSocket2( required string host, required numeric port, required string message ) {
    var result = "";
    var socket = createObject("java", "");
    var outputStream = "";
    var inputStream = "";
    var output = "";
    var input = "";
    var inputStreamReader = "";

    try {
        socket.init(, arguments.port);
    } catch( error) {
        throw message="#error.Message#: Could not connected to host on port #arguments.port#";

    if ( socket.isConnected() ) {
        outputStream = socket.getOutputStream();
        output = createObject("java", "").init( outputStream );
        inputStream = socket.getInputStream();
        inputStreamReader = createObject("java", "").init(inputStream);
        input = createObject("java", "").init(inputStreamReader);
        result = input.readLine();
    } else {
        throw message="Not connected to host via port #arguments.port#";
    return result;

#callSocket2('localhost','2000',"Hello there, it's CF")#

When having both scripts executed, the output should look like this.

Within Terminal:
Within the browser:

view on github:


Getting Startet with Coldfusion and mxUnit

Unit-Testing is a often used wording in the current fields of coding. Many people hesitate to use the given tools. I also didn’t use it very often.

Here a simple “getting into” for mx unit

  1. go to and get the latest project zip
  2. extract the mxunit zip into your project /yourproject/mxunit
  3. setup done

Now let’s create a simple test as warm-up.

Somewhere within your project one creates a single [name]Test.cfm with this default content.

When you have done everything right it should look like this:

Screenshot 2014-05-23 09.47.33

That’s all. We’re done.

Have fun playing around more.

getting String filesize in Coldfusion

Sometimes one needs to get the filesize of a string. For example when one wants to write this string into an Amazon SQS where there is a maximum limit of 256KB.

private numeric function getStringByteSize(required string text) {
 return ArrayLen( arguments.text.getBytes("UTF-8") );