Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Rails Action Cable

Kento Nagata
November 27, 2015

Rails Action Cable

Kento Nagata

November 27, 2015
Tweet

More Decks by Kento Nagata

Other Decks in Technology

Transcript

  1. $POOFDUJPO 4FSWFSTJEF # app/channels/application_cable/connection.rb module ApplicationCable class Connection < ActionCable::Connection::Base

    identified_by :current_user def connect self.current_user = find_verified_user end protected def find_verified_user if current_user = User.find(cookies.signed[:user_id]) current_user else reject_unauthorized_connection end end end end
  2. "QQFBSBODF$IBOOFM 4FSWFSTJEF # app/channels/appearance_channel.rb class AppearanceChannel < ApplicationCable::Channel def subscribed

    current_user.appear end def unsubscribed current_user.disappear end def appear(data) current_user.appear on: data['appearing_on'] end def away current_user.away end end
  3. 4VCTDSJQUJPO $MJFOUTJEF # app/assets/javascripts/cable/subscriptions/appearance.coffee App.appearance = App.cable.subscriptions.create "AppearanceChannel", connected: ->

    # Called once the subscription has been successfully completed rejected: -> # Called when the subscription is rejected by the server appear: -> @perform 'appear', appearing_on: @appearingOn() away: -> @perform 'away' appearingOn: -> $('main').data 'appearing-on' $(document).on 'page:change', -> App.appearance.appear() $(document).on 'click', '[data-behavior~=appear_away]', -> App.appearance.away() false
  4. 4VCTDSJQUJPO $MJFOUTJEF # app/assets/javascripts/cable/subscriptions/appearance.coffee App.appearance = App.cable.subscriptions.create "AppearanceChannel", connected: ->

    # Called once the subscription has been successfully completed rejected: -> # Called when the subscription is rejected by the server appear: -> @perform 'appear', appearing_on: @appearingOn() away: -> @perform 'away' appearingOn: -> $('main').data 'appearing-on' $(document).on 'page:change', -> App.appearance.appear() $(document).on 'click', '[data-behavior~=appear_away]', -> App.appearance.away() false