ruby on rails - How to use jQuery to pass value into coffescript ActionCable -
i trying create dynamic channels users chat in, unable pass room_id
app.cable.subscriptions.create
call in room.coffee
file. in clarifying why cannot use jquery value extremely helpful.
this receive in console:
roomchannel transmitting subscription confirmation roomchannel streaming room__channel
roomscontroller
class roomscontroller < applicationcontroller before_action :authenticate_user! def show other_user = user.find(params[:id]) title = [other_user.id.to_s, current_user.id.to_s].sort!.join("-") @room = room.where(title: title).first if @room.nil? @room = room.create! title: title else @messages = message.where(room_id: @room[:id]) end end end
show.html.erb
<h1>chat room</h1> <div id="messages"> <% if @messages.blank? %> <div id="no_messages_yet"> <p>no messages yet...</p> </div> <% else %> <%= render @messages %> <% end %> </div> <form> <label>say something:</label><br> <input type="text" data-behavior="room_speaker"> <input type="hidden" value="<%= @room[:id] %>" id="room_id"> <input type="hidden" value="<%= current_user[:id] %>" id="user_id"> </form>
views/messages/_message.html.erb
<div class="message"> <p><%= message.content %></p> </div>
room.coffee
$(document).ready -> room_id = $("#room_id").val() console.log(room_id) // => 1 // when try , pass 'room_id' value room_id:, error stating variable not recognized. app.room = app.cable.subscriptions.create {channel: "roomchannel", room_id: $('#room_id').val()}, connected: -> # called when subscription ready use on server disconnected: -> # called when subscription has been terminated server received: (data) -> $('#messages').append data['message'] # called when there's incoming data on websocket channel speak: (message, room_id, user_id) -> @perform 'speak', message: message, room_id: room_id, user_id: user_id $(document).on 'keypress', '[data-behavior~=room_speaker]', (event) -> if event.keycode 13 # return = send app.room.speak event.target.value, $('#room_id').val(), $('#user_id').val() event.target.value = "" event.preventdefault()
roomchannel
class roomchannel < applicationcable::channel def subscribed stream_from "room_#{params[:room_id]}_channel" end def unsubscribed # cleanup needed when channel unsubscribed end def speak(data) m = message.new m.user_id = data["user_id"] m.room_id = data["room_id"] m.content = data["message"] m.save! end end
message.rb
class message < applicationrecord after_create_commit { messagebroadcastjob.perform_later self } end
jobs/message_broadcast_job.rb
class messagebroadcastjob < applicationjob queue_as :default def perform(message) actioncable.server.broadcast "room_#{message.room_id}_channel", message: render_message(message) end private def render_message(message) applicationcontroller.renderer.render(partial: 'messages/message', locals: { message: message }) end end
place functionality inside $(document).ready
call. remember coffeescript touchy when comes indentation , whitespace.
$(document).ready -> room_id = $('#room_id').val() app.room = app.cable.subscriptions.create {channel: "roomchannel", room_id: room_id}, connected: -> # called when subscription ready use on server disconnected: -> # called when subscription has been terminated server received: (data) -> $('#messages').append data['message'] # called when there's incoming data on websocket channel speak: (message, room_id, user_id) -> @perform 'speak', message: message, room_id: room_id, user_id: user_id $(document).on 'keypress', '[data-behavior~=room_speaker]', (event) -> if event.keycode 13 # return = send app.room.speak event.target.value, $('#room_id').val(), $('#user_id').val() event.target.value = "" event.preventdefault()
Comments
Post a Comment