Building a Threaded Ruby Socket Server for Flash CS4

Post to Twitter

I haven’t seen many examples of connecting Adobe’s Flash to a Ruby socket server. I’ve seen several examples in Java, Python, Perl, etc. but not Ruby. Today I’m going to build a very simple threaded socket server that will work with Flash CS4 (it should also work with Flash CS3).

[ad name=”Google Adsense”]

For this project I used Ruby 1.8.7 and Flash CS4 (ActionScript 3.0). I ran the socket server on Ubuntu 10.04 (Beta 1) inside of VMWare Player 7 hosted on Windows Vista.

Here is the Ruby code:

require 'socket'

socketServer =

puts "Starting the socket server"

while true do |connection|

		puts "Accepting connection from: #{connection.peeraddr[2]}"

			while connection
			  	incomingData = connection.gets("\0")
			  	if incomingData != nil
					incomingData = incomingData.chomp

				puts "Incoming: #{incomingData}"

				if incomingData == "DISCONNECT\0"
					puts "Received: DISCONNECT, closed connection"
					connection.puts "You said: #{incomingData}"
		rescue Exception => e
		  puts "#{ e } (#{ e.class })"
		  puts "ensure: Closing"

Here is the ActionScript code to connect to the socket server on port 843:

var xmlSocket:XMLSocket = new XMLSocket();
// Adjust this to the IP address of your Linux machine/vm
xmlSocket.connect("", 843);

xmlSocket.addEventListener(DataEvent.DATA, onIncomingData);
send_btn.addEventListener(MouseEvent.CLICK, clickHandler);
disconnect_btn.addEventListener(MouseEvent.CLICK, disconnectHandler);

function clickHandler(event:MouseEvent):void
    input_txt.text = "";

function disconnectHandler(event:MouseEvent):void

	// You could use this if you wanted to but I
	// wanted the socket server the do the disconnect
    send_btn.enabled = false;

function onIncomingData(event:DataEvent):void
    trace("[" + event.type + "] " +;
    output_txt.text += + "\n";
    output_txt.verticalScrollPosition = output_txt.maxVerticalScrollPosition;

I named the interface components as such:
textarea – output_txt
textinput – input_txt
button (send) – send_btn
button (disconnect) – disconnect_btn

Keep in mind if you run this outside of the Flash IDE environment you will need to implement code to serve the Flash security policy file.

[ad name=”Google Adsense – large sqr rounded”]

Post to Twitter

This entry was posted in ActionScript, Adobe, Ruby. Bookmark the permalink.

4 Responses to Building a Threaded Ruby Socket Server for Flash CS4

  1. ManFromRussia says:

    It fastest than java server?
    Can you explain, how to make Multi Threaded? Thanks!

  2. Chad Lung says:


    This solution is not faster than a properly coded Java socket server.


  3. ozzi says:

    Hi Chad,

    Ive only just come across your blog and it seems to be something that I am trying to do.
    I need to create an XML Socket where I can allow Flash to send back an XML file.
    But I am a bit unsure on how to use your code to achieve this.
    Would it be possible to help me out?
    many thanks,

  4. Chad Lung says:


    If your looking to load an XML file then you would be better off using URLLoader and URLRequest. That way you can simply request the XML file and load it.

    You can see the URLLoader in action in this article of mine:

    Parsing the incoming XML is pretty easy, this article can get you started on that aspect:


Comments are closed.