I'm writing a simple real-time chat, so I decided to use web-sockets. I used gem 'faye-rails'
. Web console shows that it works POST http://khersonchat.herokuapp.com/faye [HTTP/1.1 200 OK 170ms]
but when I send a message there's an error: While loading the page connection ws://khersonchat.herokuapp.com/faye was broken
(translated from Russian). So when I send a message, the whole page still reloads, and I need to reload a page to see other people's messages sent.
messages_controller.rb
:
def create
respond_to do |format|
if current_user
@message = current_user.messages.build(message_params)
if @message.save
flash[:success] = 'Message sent'
else
flash[:error] = 'Oops, an error :('
end
format.html {redirect_to root_path}
format.js
else
format.html {redirect_to root_path}
format.js {render nothing: true}
end
end
end
application.js
:
//= require jquery
//= require jquery_ujs
//= require faye
//= require messages
//= require_self
//= require turbolinks
messages.coffee
:
window.client = new Faye.Client('/faye')
jQuery ->
$('#new_message').submit ->
$(this).find("input[type='submit']").val('Sending...').prop('disabled', true)
try
client.unsubscribe('/messages')
catch
console?.log "Can't unsubscribe"
client.subscribe '/messages', (payload) ->
$('#messages').find('.media-list').append(payload.message) if payload.message
create.js.erb
publisher = client.publish('/messages', {
message: '<%= j render @message %>'
});
publisher.callback(function() {
$("#message_body").val('');
$("#new_message").find("input[type='submit']").val('Send').prop('disabled', false)
});
publisher.errback(function() {
alert('Oops, an error');
});
https://github.com/AlexNikolaev94/chatclone.git - up-to-date source code
important! the chat authenticates via omniauth
, using a social network Vkontakte(vk.com), so the version stored in git has the access to localhost
The problem was with Csrf Protection, so the topic continues here