Tutorial: Socket server with Flash and Node.js

Programming with sockets seems to be an intimidating venture for many developers. However, with AS 3 and Node.js, it really isn’t too difficult. In this tutorial you will set up a simple socket server using Node.js and connect to it with a Flash-based client. When the project is complete, you’ll be able to see data being transmitted from Flash to the server and back to Flash again - Booyah!

Getting Started

To get started, you’ll first need to download Node.js. If you’re on a Windows box, you can get the download right here. If not, check out this wiki page for instructions on how to get it on your system. I wrote this tutorial on my Windows 7 machine, so the instructions will target that platform, though you can still follow along and use the appropriate paths on your own machine regardless of which platform you’re using.

Once you have downloaded the package, create a new directory named nodeJS on your C drive. You can extract the contents of the node.js package to this directory. If you want to test out the installation, go into the Node.js bin directory and create a new JavaScript file. Bring up any text editor (notepad is fine) and enter the text below; save the file as helloworld.js.

console.log("Hello, World!");

The actual Node program is a command-line based tool that will execute your javascript files. To run it, bring up the Windows command prompt.

1) hit the Windows key + r to bring up the Run box
2) type cmd in the box and hit enter

With the command window open, we need to navigate to the Node.js bin directory; that is where we saved our helloworld.js file. If you created a nodeJS directory on your C drive as I have done above then type the following in the command window:

cd C:\nodeJS\bin

If you created your directory in a different place, you’ll need to adjust the path accordingly. Now that we’re in the right place, we need to tell the Node program to execute our script. We can do this by issuing the command like so:

node helloworld.js

With any luck, you’ll see the words “Hello, World” printed out in the console window as shown below.

If everything worked, then you’re ready to move on. If not, try repeating the steps and make sure you’ve installed Node.js in the right place.

Pages: 1 2 3

Bookmark and Share

24 Responses to “Tutorial: Socket server with Flash and Node.js”

  1. EvilPaul says:

    Awesome stuff! Simple and helpful!

  2. James says:

    Awesome post! Thanks for taking the time to write this tutorial.

  3. Mayank says:

    Hi.

    thanks for such an awesome post!! was really helpful!
    it would be great if you can also post the modified C# file for sending kinect information!
    looking forward for a quick reply.

  4. Rhuno says:

    Hi, Mayank.

    I did do a couple of tutorial on sending kinect information to Flash via node.js earlier. They may be outdated now as they were done with the original sdk and getting properly set up was somewhat of an involved process. However, if you’re interested you can check out the articles below.

    http://rhuno.com/flashblog/2011/07/02/tutorial-kinect-to-flash/

    http://rhuno.com/flashblog/2011/09/18/full-body-tracking-with-kinect-and-flash/

  5. Mayank says:

    Hi,

    Thanks for the tutorials!! they are awesome!!! :) :) :) :)

  6. Jeff Marshall says:

    Thank you for the great instructions. I am liking node and can’t wait to try some more things with it.

  7. Marwan says:

    Great post. Could you explain how to set up a flashpolicy.xml for this demo? I keep getting the following in my server log: “From Flash = “. The example works when you compile through flash IDE but now when you run the swf independently.

    thanks

  8. Rhuno says:

    Hi Marwan,

    Depending on where you are running the content from, you may not need a cross doman policy file. If you are running the swf from your machine, you might need to add the location to the list of Flash’s trusted locations. You can do so using the online global security panel here:

    http://www.macromedia.com/support/documentation/en/flashplayer/help/settings_manager04.html

    If you do need to set up a cross domain file, this article may help you out.

    http://active.tutsplus.com/tutorials/tools-tips/quick-tip-a-guide-to-cross-domain-policy-files/

    I hope that helps.

  9. ^_^ says:

    thankyou! =*^_^*=

  10. Vipul says:

    Thanks for this post. It is very helpful for me. Please let me know when user close flash player then how will user disconnect from server at that time.

  11. Rhuno says:

    You can call socket.close when the player exits the program, but sockets should close on their own when the flash player is closed.

  12. Vipul says:

    Thanks for the same.

  13. Satya says:

    Hi,

    How do I run the swf file from a web browser

    Sorry I am new here

    Thank you
    Satya

  14. benz.muircroft says:

    right cool tutorial… a little question, you are the only page on the net that sends and receives with just net node module , how is this done? can i build some thing similar for html/javascript on the client?

    wow, very strange..

  15. Rhuno says:

    All you need is the server and the socket connection; this is the same way its done on the node homepage example. If you were doing this html/js, the node code would work the same, but you’d need to use websockets or something on the client.

  16. Nigel Douglas says:

    Hey there! Any ideas how best to approach Flash with MongoDB. Should I continue on this path with MongoJS, or should I go an alternative route. Also, any idea if there is any good tutorials on the topic?

  17. Rhuno says:

    I can’t really offer any advice as I’ve never worked with MongoDB. I haven’t looked for other tutorials on this, but I’m sure they are out there.

  18. Najm says:

    Thanks bro. I was thinking if socket.io cannot support high traffic and was stuck studying websocket proxy using nginx which is a complete proper server. I figured out that using node.js cluster it will not be a problem. Please share if you know good example or tips for load balancing or still nginx will be better. I am PHP developer therefore prefering nginx.

  19. Kevin Gibson says:

    I was doing great until I came to publish and nothing happened… no error message or anything. I am using CS5.5. I even downloaded project files and still the same. any advice?

  20. Rhuno says:

    Hey Kevin,

    You do have to click on the flash app to make it connect. After clicking on it, you should see it trace “Connected” in the output window. Also note, you should run it directly from the Flash IDE, if you run it externally you’ll need to give it permission to use sockets.

    I guess the only other thing is to make sure the node server is running.

  21. Nate says:

    3 years after this post was written…. and i still feel the need to comment, just because of how impressed i am of this tutorial.

    I’ve been looking into technologies to convert my turn-based polling applications into real time apps, and decided node.js was the best solution. Only problem was that there werent any simple to understand tutorials on the internet to setup flash + node.js

    This tutorial taught me lots and has allowed me to understand the correct approach to go forward with node.js

    Thank you so much..

  22. Rhuno says:

    Thanks, Nate. Always good to hear.

  23. 8power8 says:

    Hi there,

    Thanks for this nice example, I know I am late (we’re in 2015!) but it I can’t get any other event than ‘data’ fired on the node.js side. Any idea on this ? Thanks.

  24. YWern says:

    Hi there! I followed every steps but when I publish my server does not show ‘connected to flash’. When I press on my keys, the key data is still able to send from flash to server. I am confused if the connection is properly connected in this situation =) THank you!

Leave a Reply

Subscribe to RSS feed FGS5 Badge