Assembla home | Assembla project page
 

Chat Setup

Our chat has four components:

  • The juggernaut plugin. This includes methods to send chat messages to the push_server, so that they can be received by the clients.
  • The juggernaut push_server. This is a stand-alone script that sends new chat messages to all of the clients.
  • the flash XML Socket control, which is packaged into a flash component called socket_server.swf. This makes a connection to the push_server. When it receives a message, it runs the javascript method to update the page.
  • The chat page, start_chat_session.rhtml, and the related Ajax actions and views, and related javascript (chat_tool.js)

When you send a message, you submit a normal request to our Rails server. The juggernaut plugin helps us send it to the push_server, and the push_server sends it to the clients. The clients show an updated page.

Message Architecture

The Chat Tool took advantage of push messaging architecture provided by juggernaut, initially the tool was implemented using polling, wherein the browser have to poll the server at a predetermined amount of time for new messages. Though the solution works, it consumes a lot of bandwidth and server resources because even if there are no new messages, the client still sends request to the server for updates. Push messaging has a nice advantage that the client no longer polls the server but just wait for the server to push them with new updates. This way it minimizes the server resources and at the same time reduces bandwidth usage.

For this architecture, juggernaut uses macromedia flash and a push server created using ruby. The push server is responsible for maintaining the list of open channels and the list of clients subscribing to it. If a client needs to send message to other client, it will send it first to the push server, and push server will broadcast it to the rest of the clients listening to the same channel. The client here is the web browser with macromedia flash installed.

Basically what will happen, when a new user visits the chat tool page, the application will assign him to a channel, get the latest posted messages from the database and broadcast to the rest of group listening to the same channel, of the user's presence. This is manifested, with the user's name added in the who's here list on the right hand side of the chat tool area. When a user sends a message, the client (in this case the browser) opens a socket connection to the push server and sends the message to the push server, the push server will then broadcast it. If the user leaves the chat tool page, the browser before unload, will send a message to the push server of the user intention to leave, and the push server will broadcast it again to the rest of the clients and it will be manifested, with the user's name removed from the who's here list.

Installation

If you want to test the Chat Tool, below are the requirements.

  • Rails 1.1
  • json,daemons, event_machine gems
    $ gem install json daemons event_machine
    

If you encounter some errors during the installation of json, go to your json's gem folder (on my computer, it is located in C:\InstantRails?\ruby\lib\ruby\gems\1.8\gems\json-0.4.2), run the "install.rb" script and everything should be OK after that.

Juggernaut Specific Configuration

Modify the juggernaut_config file found in "config/juggernaut_config.yml". The most important parameters that you need to set in order to run the Chat tool are the following:

* PUSH_PORT -> This is the port that the push server will be on. The port number should be greater than 1024. (Here, I used 1025 for my port number.)

* PUSH_HOST -> This is the host that the push server will be on. (Here, I used my machine's IP address)

* PUSH_HELPER_HOST -> This should be the url as seen from the browser. Flash's xmlsocket can only connect to addresses in the same domain or subdomain. Omit the port number and the 'http://'. Example: stage.assembla.com.

* CROSSDOMAIN -> http://host/crossdomain.xml, ie: http://stage.assembla.com/crossdomain.xml

After everythings set, run the push server with the following *very complicated* command!

$ ./script/push_server_control.rb start

Or run push_server directly to start the push server.

Documentation

Juggernaut Project Page: http://juggernaut.rubyforge.org/

  • You need to install the gems (json and eventmachine) on your local machine (not the plugin, as it is already installed).
  • You need to configure juggernaut_config.yml (check this page: http://ncavig.com/blog/?page_id=11 and mail sromano@gmail.com if you have any doubt)

XML Socket Api (an important object use by the socket_server.swf): http://www.adobe.com/support/flash/action_scripts/actionscript_dictionary/actionscript_dictionary860.html

Crossdomain.xml : http://moock.org/asdg/technotes/crossDomainPolicyFiles/