Well, i'm trying to create a chat app with rails and websocket but don't work good.
This is my events.rb:
WebsocketRails::EventMap.describe do
namespace :chat do
subscribe :new_message, :to => ChatController, :with_method => :incoming_message
end
end
This is my JS:
$(document).ready(function() {
var dispatcher = new WebSocketRails(window.location.host + "/websocket");
dispatcher.bind('chat.new_message', bind_new_message);
$('#send_message').on('click', function(event) {
var message = {
text: $('#new_message').val()
}
dispatcher.trigger('chat.new_message', message);
});
});
function bind_new_message(message) {
$('#chat_history').append('<div class="message"><span class="user">' + message.user + ':</span> ' + message.text + '</div>');
}
And this is my chatcontroller.rb:
class ChatController < WebsocketRails::BaseController
def incoming_message
logger.info "====>>>> Llega al Controlador --->>> #{message.inspect}"
new_comment = {:user => current_user.screen_name, :text => message[:text]}
broadcast_message :new_message, new_comment, namespace: 'chat'
# send_message :new_message, new_comment, namespace: 'chat'
end
end
And the problem is that this don't work, for example, in websocket gem doc puts that when Broadcast Events to all Clients you can use method broadcast_message
but don't work, however, if i change this method for send_message
it works (but only broadcast to user that trigger the event)
This is my test app