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:


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s