I am writing a simple chat in real time, I connected the "faye" gem so that the messages are immediately displayed without reloading the page. faye is not detected when sending a message, so you need to reload the page to see messages sent by another person

messages_controller.rb

 def create respond_to do |format| if current_user @message = current_user.messages.build(message_params) if @message.save flash[:success] = 'Ура, отправилось! :)' else flash[: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_tree . //= require faye //= require messages 

messages.coffee

 window.client = new Faye.Client('/faye') jQuery -> $('#new_message').submit -> $(this).find("input[type='submit']").val('Отправляем...').prop('disabled', true) try client.unsubscribe('/messages') catch console?.log "Can't unsubscribe" client.subscribe '/messages', (payload) -> $('#messages').find('.media-list').prepend(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('Отправить').prop('disabled', false) }); publisher.errback(function() { alert('Ошибочка :('); }); 

https://github.com/AlexNikolaev94/chatclone.git source code

  • Comments are not intended for extended discussion; conversation moved to chat . - Nick Volynkin

1 answer 1

@ D-side, @cheops as it turned out, the problem was the same with Csrf Protection . for some reason, he blocked Faye, and this problem exists in both faye 2.0.0 and faye 2.0.1 . The implementation of csrf_protection was carried out according to current documentation , but the reason why faye could not subscribe to the channel due to csrf_protection was not found, so the discussion of the issue moved here . On the topic - removed csrf_protection, and earned.

  • " Example: Rails " <- well, never mind .-. - D-side
  • @D-side, I meant that everything was done according to the documentation, and therefore there should have been no problems - but they were. I re-read the documentation - everything was correct. but for some reason, it was precisely because of the csrf_protection faye could not subscribe to the channel. perhaps some library has changed in the gem itself: (I apologize for not being accurate - AlexNikolaev94
  • Hmm ... yes, I found an extension, last time I missed it. And to do there pry and see what happens, tried? - D-side
  • @ D-side, no, tried to downgrade to old versions, did not help ( - AlexNikolaev94
  • So you need to open the debugger CSRF-checker when the connection is established, and check if there is a token there. - D-side