Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Loading…

Next-Gen: Node.JS Server, HTML5 WebSockets/Audio, Flash Fallback #43

Open
aitte opened this Issue · 3 comments

3 participants

@aitte

Update: I have written a tutorial in the 4th post listing every step required to make this a reality, getting rid of the complex Ruby, and the ancient Flash dependencies all in one go, and making the client/server much easier to code and extend in the future.

Old post:
It's true that the Ruby server works well, but if you are looking for a fun and quick-to-write project, there's Node.JS (http://nodejs.org/) which lets you write an ultra-high-performance server in very few lines of code.

It contains libraries for MySQL which makes implementation easy, and passing messages around between connected sockets is super easy too (there are built-in broadcast methods). Check some videos about it. It's fantastically fun and quick to develop for! I know that you could have a NodeJS based server in less than a day.

The language you use to write servers with it is Javascript, something you are already very familiar with considering that AJAXChat is mostly Javascript/AJAX.

Here's an introduction video from a year ago, with the author himself. He goes through everything including how to make a chat server in Node. http://www.youtube.com/watch?v=jo_B4LTHi3I

The question remains: Why write a NodeJS server?
1. Ultra-high performance (asynchronous threads)
2. Very quick and fun to write a chat server in
3. Exists for every platform and requires only a single .exe/bin file which contains everything (http://nodejs.org/download/)

The third point is especially great, because the Ruby server requires lots of setup for Ruby itself on Mac OS X and Windows (it's easier on Linux).

With NodeJS, they just have to download Node, and run one of:
./node server.js (mac, linux, unix)
node.exe server.js (windows)

@aitte

This is a good companion to #42 (HTML5 WebSockets).

If both were implemented, socket-based AJAXChat communications would finally be opened up to the world. All modern desktop and mobile browsers support WebSockets, and an accompanying NodeJS server is very easy to install and start on any computer.

End result: Finally switching from slow PHP/JSON back-end to Sockets for everyone!

@IngwiePhoenix
@aitte

Here is exactly what is needed. I am posting this for future reference. Would love to implement it myself, because it's a quick and fun job, but I am way too busy with work administering a network of Cloud servers right now and there's no way I can spare even a second to look at doing this myself.

Server

Core

MySQL driver

  • node-mysql
    • Site: https://github.com/felixge/node-mysql and https://npmjs.org/package/mysql
    • Platforms: All (Win/Mac/Linux/BSD)
    • Strengths/Weaknesses: Easy to install, mature and very stable (has financial and development support from corporate sponsors), built in Javascript using Node which makes it fast while still supporting every OS and requiring no extra libraries, lots of corporate applications use this one. This is the one to choose for the server, it's the least hassle to install for end-users and is fast and stable!

Client/Server Socket Communications

  • Socket.IO
    • Site: http://socket.io and https://npmjs.org/package/socket.io
    • Platforms: All (Win/Mac/Linux/BSD)
    • Strengths/Weaknesses: Provides a server/client socket abstraction that allows messages to pass back and forth effortlessly. Supports every browser (uses Flash automatically if the browser doesn't support WebSockets). You run it in both the client and the server. No weaknesses.

Client

Client/Server Socket Communications

  • Socket.IO
    • Site: http://socket.io and https://npmjs.org/package/socket.io
    • Platforms: All (Win/Mac/Linux/BSD)
    • Strengths/Weaknesses: Provides a server/client socket abstraction that allows messages to pass back and forth effortlessly. Supports every browser (uses Flash automatically if the browser doesn't support WebSockets). You run it in both the client and the server. No weaknesses.

Installing the Server (All Platforms)

  • Download and Install NodeJS for your platform using the INSTALL button at http://nodejs.org
  • Install the required server libraries
    • node-mysql: npm install mysql
    • socket.io: npm install socket.io
    • Note: On Windows, the npm tool is in C:\Program Files\nodejs\
  • Start the server
    • node server.js
  • That's it! Super easy for anyone to run a socket chat server!

Alright, so what to do to make the server and client talk to each other?

  • Replace the current, hacky Flash-socket code with Socket.io on both the client and server. The client's browser will automatically be able to talk to the server regardless of browser used, and will use either ultra-fast WebSockets, or Socket.io's Flash transport if no WebSockets are available.
  • To learn more about Socket.IO client/server and Node, view this short tutorial (it's very simple): http://java.dzone.com/articles/getting-started-socketio-and
  • These are the basic step by step components to making this a reality:
    • Write a Node.JS server that uses Socket.IO and make it talk back/forth with the client's Socket.IO
    • When that works, add the MySQL library to the server and make it write/read the database
    • That's it! No more need for Ruby or PHP. Server requires just Node and is easy to install on any platform.

Bonus: Getting rid of the Flash dependency for audio too!

Bonus: Getting rid of Web server dependency!

  • Use http://expressjs.com to build the web-server into the Node.JS server. That way, the Node server can reply on port 80 with the HTML/Javascript/Images that the client needs, and the client then connects to the chat-socket port after that.
  • That's it, no more Flash, no more PHP, no more Ruby, no more Apache/webserver. Just a single server script that works on every platform and hosts the entire chat application.
  • The only external dependency that remains is MySQL, which is fine and easy to install. Alternatively, one could switch the system over to Node+NoSQL (http://www.couchbase.com/why-nosql/nosql-database) which is even faster and lighter than MySQL.
@Frug Frug was assigned
@Frug Frug modified the milestone: 0.10, 0.9
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.