|
1. Preparation
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
end11. 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. |
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?
corbinhoenes check out shooting_star on rubyforge, it's a similar project: http://rails.office.drecom.jp/takiuchi/archive/181 http://shooting-star.rubyforge.org/wiki/wiki.pl?Making_A_Chat_System_Within_5_Minutes
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.
The ajaxmessaging-example app doesn't work with IE 6.0.
Same issue, requires 3 args, providing 2.
when i start mongrel, i got uninitizlized constant: AjaxMessaging?::AjaxMessagingHelper?, what's wrong??? thks
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!
test
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..