Permalink
Browse files

Added MessagesService, a faye backend extension

  • Loading branch information...
1 parent 46e5396 commit ca3a59e76bafa7e166e090bf749e524adf5cb826 @aokon committed Nov 25, 2012
Showing with 36 additions and 6 deletions.
  1. +1 −1 Procfile
  2. +1 −0 README.textile
  3. +8 −4 app/assets/javascripts/chat.js.coffee
  4. +1 −1 app/views/messages/_form.html.erb
  5. +2 −0 faye.ru
  6. +23 −0 lib/faye/messages_service.rb
View
@@ -1,2 +1,2 @@
web: bundle exec rails s
-faye: thin start -R faye.ru -p 9292
+faye: thin start -R faye.ru -p 4000
View
@@ -9,3 +9,4 @@ h2. Run app
* run db migration @rake db:migrate@
* run web server and faye pub/sub server via foreman @foreman start@
+* you can run manually rails app and faye server see Procfile
@@ -13,7 +13,7 @@ Logger = {
}
$ ->
- fayeClient = new Faye.Client("http://localhost:9292/faye")
+ fayeClient = new Faye.Client("http://localhost:4000/faye")
fayeClient.addExtension Logger
subscription = fayeClient.subscribe "/messages/new", (data) ->
@@ -25,10 +25,14 @@ $ ->
subscription.errback (error)->
console.log error
- $("#new_message").on "ajax:complete", (xhr, data) ->
- message = JSON.parse(data.responseText)
- publication = fayeClient.publish '/messages/new', message.content
+ $('#new_message .btn').on 'click', (event) ->
+ event.preventDefault()
+ content = $('#new_message #message_content').val()
+
+ publication = fayeClient.publish '/messages/new', content
publication.callback -> console.log "message was received"
publication.errback (error) -> console.log error
+
+
@@ -1,4 +1,4 @@
-<%= form_for(Message.new, remote: true, html: { class: "form-horizontal" }) do |f| %>
+<%= form_for(Message.new, html: { class: "form-horizontal" }) do |f| %>
<div class="control-group">
<%= f.text_area :content %>
</div>
View
@@ -1,6 +1,8 @@
require 'faye'
+require './lib/faye/messages_service.rb'
Faye::WebSocket.load_adapter('thin')
faye_server = Faye::RackAdapter.new(:mount => '/faye', :timeout => 25)
+faye_server.add_extension MessagesService.new
run faye_server
@@ -0,0 +1,23 @@
+require 'logger'
+require 'active_record'
+require 'yaml'
+require File.expand_path('../../../app/models/message.rb', __FILE__)
+
+class MessagesService
+
+ def initialize
+ ActiveRecord::Base.logger = Logger.new(STDOUT)
+ ActiveRecord::Base.configurations = YAML::load(File.read(File.expand_path('../../../config/database.yml', __FILE__)))
+ ActiveRecord::Base.establish_connection( ENV['FAYE_ENV'] || 'development')
+ end
+
+ def incoming(message, callback)
+
+ if message['channel'] == '/messages/new'
+ params = { content: message['data'] }
+ Message.create! params
+ end
+ callback.call(message)
+ end
+
+end

0 comments on commit ca3a59e

Please sign in to comment.