Connect Coldfusion and Ruby via Socket

This week a read a lot about ZeroMQ(zeromq.org). 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.

drawing

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
    end
    return data
end

def init()
    server = TCPServer.open(2000)
    loop {
        client = server.accept
        puts incommingMessage(client)
        client.puts "Hi from ruby, cya!"
        client.close
    }
end

init()

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: http://www.getrailo.org/index.cfm/download/.

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

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

    if ( socket.isConnected() ) {
        outputStream = socket.getOutputStream();
        output = createObject("java", "java.io.PrintWriter").init( outputStream );
        inputStream = socket.getInputStream();
        inputStreamReader = createObject("java", "java.io.InputStreamReader").init(inputStream);
        input = createObject("java", "java.io.BufferedReader").init(inputStreamReader);
        output.println(arguments.message);
        output.println();
        output.flush();
        result = input.readLine();
        socket.close();
    } else {
        throw message="Not connected to host #arguments.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:
ruby
Within the browser:
cf

view on github: https://github.com/Macagare/Ruby-CF-Socket

Advertisements