Last active
October 27, 2016 16:38
-
-
Save llaine/4d42a75a5deda3fdef280638bb59cd7d to your computer and use it in GitHub Desktop.
ActionCable
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Message < ApplicationRecord | |
belongs_to :user | |
belongs_to :channel | |
validates :body, presence: true, length: {minimum: 2, maximum: 1000} | |
after_create_commit { | |
MessageBroadcastWorker.perform_async(self.id) | |
} | |
def timestamp | |
created_at.strftime('%H:%M:%S %d %B %Y') | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class MessageBroadcastWorker | |
include Sidekiq::Worker | |
def perform(message_id) | |
message = Message.where(id: message_id).first | |
ActionCable.server.broadcast "room_#{message.channel_id}_channel:", message: render_message(message) | |
end | |
private | |
def render_message(message) | |
ApplicationController.renderer.render(partial: 'messages/message', locals: { message: message }) | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
jQuery(document).on 'turbolinks:load', -> | |
messages = $('#messages') | |
if messages.length > 0 | |
messages_to_bottom = -> messages.scrollTop(messages.prop('scrollHeight')) | |
messages_to_bottom() | |
App.room = App.cable.subscriptions.create { | |
channel:"RoomChannel", | |
channel_id: messages.data('channel-room-id') | |
}, | |
connected: -> | |
console.log('connected') | |
console.log(App.room) | |
# Called when the subscription is ready for use on the server | |
disconnected: -> | |
# Called when the subscription has been terminated by the server | |
received: (data) -> | |
console.log(data) | |
messages.append data['message'] | |
messages_to_bottom() | |
speak: (message, channel_id) -> | |
@perform 'speak', message: message, channel_id: channel_id | |
$('#new_message').submit (e) -> | |
$this = $(this) | |
textarea = $this.find('#message_body') | |
if $.trim(textarea.val()).length > 1 | |
App.room.speak textarea.val(), messages.data('channel-room-id') | |
textarea.val('') | |
e.preventDefault() | |
return false |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Be sure to restart your server when you modify this file. Action Cable runs in a loop that does not support auto reloading. | |
class RoomChannel < ApplicationCable::Channel | |
def subscribed | |
# stream_from "some_channel | |
stream_from "room_#{params['channel_id']}_channel" | |
end | |
def unsubscribed | |
# Any cleanup needed when channel is unsubscribed | |
end | |
def speak(data) | |
current_user.messages.create!(body: data['message'], channel_id: data['channel_id']) | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment