My favorites | Sign in
Project Home Downloads Wiki Issues Source
Search
for
ChatRoomIn5Mins  
Using AjaxMessaging to create a chatroom in 5 mins
Featured, Tutorial
Updated Feb 4, 2010 by siu.y...@gmail.com

1. Preparation

  • Download and install Ruby on Rails
  • Download and install Apache ActiveMQ
  • Install Mongrel ( sudo gem install mongrel )
  • Install json (sudo gem install json)
  • Install stomp (sudo gem install stomp)

2. Generate chat application

rails chat

3. Install AjaxMessaging plugin

cd chat
script/plugin install http://ajaxmessaging.googlecode.com/svn/trunk/plugins/ajaxmessaging

4. Configure

a. Configure Apache ActiveMQ (we setup the topic 'chat') ACTIVEMQ_HOME/conf/activemq.xml

<beans>

  <!-- Allows us to use system properties as variables in this configuration file -->
  <bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"/>
  
  <broker brokerName="localhost" useJmx="true" xmlns="http://activemq.org/config/1.0">
    <!-- In ActiveMQ 4, you can setup destination policies -->  
    <destinationPolicy>
      <policyMap><policyEntries>        
          <policyEntry topic="chat>">
            <dispatchPolicy>
              <strictOrderDispatchPolicy />
            </dispatchPolicy>
            <subscriptionRecoveryPolicy>
              <lastImageSubscriptionRecoveryPolicy/>
            </subscriptionRecoveryPolicy>
          </policyEntry>
      </policyEntries></policyMap>
    </destinationPolicy>

    <persistenceAdapter>
        <journaledJDBC journalLogFiles="5" dataDirectory="${activemq.base}/activemq-data"/>
    </persistenceAdapter>

    <transportConnectors>
       <transportConnector name="stomp"   uri="stomp://localhost:61613"/>
    </transportConnectors>
    
    <networkConnectors>
      <!-- by default just auto discover the other brokers -->
      <networkConnector name="default-nc" uri="multicast://default"/>
    </networkConnectors>    
  </broker>
</beans>

b. Configure AjaxMessaging (we use defaults config) config/a11g.yml

USERNAME: 
PASSWORD: 
HOST: localhost 
PORT: 61613 
RELIABLE: false 
DEFAULT_CHANNELS: default 
CHANNEL_ROOT: /topic/ 
BASE_URL: /amq 
POLL_DELAY: 0.0 
TIMEOUT: 30

c. Configure database (we use sqlite3 in this example) config/database.yml

development: 
  adapter: sqlite3 
  database: db/dev 
  encoding: utf8 
  timeout: 5000 
 
test: 
  adapter: sqlite3 
  database: db/test 
  encoding: utf8 
  timeout: 5000 
 
production: 
  adapter: sqlite3 
  database: db/prod 
  encoding: utf8 
  timeout: 5000

d. Configure Mongrel config/mongrel_conf.yml

:config_script: lib/ajax_messaging_handler.rb

5. Prepare Data Model and Scaffold

./script/generate scaffold_resource chat name:string message:string
rake db:migrate

6. Implement Chat Application app/view/layouts/chats.rhtml

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 
       "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
 
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"> 
<head> 
  <meta http-equiv="content-type" content="text/html;charset=UTF-8" /> 
  <title>Chats: <%= controller.action_name %></title> 
  <%= stylesheet_link_tag 'scaffold' %> 
  <%= javascript_include_tag :defaults %> 
  <%= yield :script %> 
</head> 
<body> 
 
<p style="color: green"><%= flash[:notice] %></p> 
 
<%= yield  %> 
 
</body> 
</html>

7. Edit index view app/view/chats/index.rhtml

<% content_for("script") do %> 
    <%= a11g_listen_to('chat') %> 
<% end %> 
 
<!-- form to input message --> 
<%= render_component :action => 'new' %> 
 
<!-- message list --> 
<ul id="chat-list"> 
<% for chat in @chats %> 
  <%= render_component :action => 'show', :id => chat.id %> 
<% end %> 
</ul>

8. Edit view to add a message app/views/chats/new.rhtml

<% form_remote_for(:chat, :url => chats_path) do |f| %> 
  <%= f.text_field :name %> 
  <%= f.text_field :message %> 
  <%= submit_tag "Talk" %> 
<% end %>

9. Edit view to chat entry app/views/chats/show.rhtml

<li> 
  <%=h @chat.name %> 
  <%=h @chat.message %> 
</li>

10. Edit chat controller

class ChatsController < ApplicationController 
  layout 'chats', :only => 'index' 
 
  def index 
    @chats = Chat.find(:all).reverse 
  end 
  def show 
    @chat = Chat.find(params[:id]) 
  end 
  def create 
    @chat = Chat.create(params[:chat]) 
 
    # create HTML string to show a message 
    content = render_component_as_string :action => 'show', :id => @chat.id 
 
    # create JS string to add a message on list 
    javascript = render_to_string :update do |page| 
      page.insert_html :top, 'chat-list', content 
    end 
 
    AjaxMessaging.send_data 'chat', javascript 
     
    # nothing is rendered 
    render :nothing => true 
  end 
end

11. Start Servers a. Start ActiveMQ server:

cd apache-activemq
./bin/activemq

b. Start Mongrel

cd chat
mongrel_rails start –C ./config/mongrel_conf.yml

c. Start Chatting Open browser and browse to http://localhost:3000/chats to chat, your messages get distributed to others in realtime. Reminder: Some browsers limit the number of connections to a server (e.g. Firefox), if you want to chat with yourself on same computer, use two different browsers.

Credit: This tutorial is based on tutorial of shooting_star. If you want a powerful and stable comet server, you probably want shooting_star instead.

Comment by corbinho...@gmail.com, Aug 1, 2007

What the?! Why do you put "you probably want shooting star instead" at the bottom of your tutorial. What is the difference between shooting star and ajaxmessaging?

Comment by patc...@gmail.com, Aug 19, 2007
Comment by nao...@gmail.com, Nov 4, 2007

AjaxMessaging?.send_data 'chat', javascript

The above code in the ChatsController#create? method causes an error. AjaxMessaging?.send_data should have 3 args. I cannot figure out what value the second arg should be.

please help.

Comment by iamt...@gmail.com, Jan 17, 2008

The ajaxmessaging-example app doesn't work with IE 6.0.

Comment by aurelien...@gmail.com, May 6, 2008

Same issue, requires 3 args, providing 2.

Comment by myta...@gmail.com, Feb 22, 2009

when i start mongrel, i got uninitizlized constant: AjaxMessaging?::AjaxMessagingHelper?, what's wrong??? thks

Comment by krisssr...@gmail.com, Apr 9, 2009

dosent work, and if you change the activemq file , makes it persistent across different versions of the activemq, you have to restart the computer to get it right again. and there are bugs in the code. also where can i find proper documentaion for this? its killing me!

Comment by dwsm...@gmail.com, Feb 18, 2010

test

Comment by KamP...@gmail.com, Apr 25, 2010

doesnt work. dont waste your time. i have tried for hours and hours, eventuall i gave up. Why did people post the code that does not work.. wasting ppl time..


Sign in to add a comment
Powered by Google Project Hosting