Building a socket server with Node.js and Flash CS4

Post to Twitter

Node.js is very easy to use and very well designed. Today I’m going to build a simple socket server using the sample provided by the Node.js documentation and create a socket client in Flash CS4.

Updated July 30, 2010 – I’ve changed the Node.js Server code below to reflect the latest changes in the Node.js API changes as of version: 0.1.102. I also tested this in Flash CS5 and it worked.

[ad name=”Google Adsense”]

I used the following to build this project:
1. Ubuntu 9.10 hosted in VirtualBox on Windows Vista
2. Flash CS4
3. Node.js

I also turned off the firewall in Ubuntu (not recommended but since I’m hosting it in VirtualBox and is already behind the router firewall I didn’t want to deal with any additional firewall issues – obviously in a production environment you need to think this through before disabling it)

I disabled the firewall in Ubuntu using this command:

ufw disable

Do so at your on risk!

Lets create the Flash CS4 project first. Create a new “Flash File (ActionScript 3.0)” and save it as “SocketExample”. My user interface look like this:

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

On the first frame of the timeline I added this ActionScript code:

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

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
    send_btn.enabled = false;

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

If you haven’t worked through my Node.js tutorial do that first because I’m going to assume you have Node.js up and running.

Here is the Node.js code (taken mostly from the Node.js documentation):

var net = require("net"), sys = require('sys');
var server = net.createServer(function (stream) {
  stream.on("connect", function () {
    sys.puts(sys.inspect(stream, false));
  stream.on("data", function (data) {
    stream.write(data + "\0"); 
  stream.on("end", function () {
server.listen(7000, "");

Make sure to adjust the IP address as needed. Save the file as “tcp_server.js”.

Go ahead and run the script:

node tcp_server.js

Run the Flash SWF now and try it out.

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

Post to Twitter

This entry was posted in ActionScript, Adobe, JavaScript, Node.js, Open Source, Socket Server, Ubuntu. Bookmark the permalink.

5 Responses to Building a socket server with Node.js and Flash CS4

  1. Pingback: Messing around with Node.js and HTTP « Giant Flying Saucer

  2. Nate says:

    Can’t wait to try this!!!!

  3. Olivier says:

    Heys, thanks for this, i needed some startpoint for a project involving as3 and Node.js, looks like i found it 🙂 thanks again

  4. andreas says:

    hey, thanks. how can i run this in my browser on localhost:7000?
    thanks again for your help.

  5. Chad Lung says:


    Just change the following to your machine’s IP address:

    server.listen(7000, “”);
    server.listen(7000, “localhost”);
    server.listen(7000, “MY-IP-ADDRESS”);


Comments are closed.