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

ActionCable - For Not Another Chat App

ActionCable - For Not Another Chat App

RailsConf 2016

Jesse Wolgamott

May 05, 2016
Tweet

More Decks by Jesse Wolgamott

Other Decks in Programming

Transcript

  1. ActionCable - Not Another Chat App - by Jesse Wolgamott | RailsConf 2016
    ActionCable: No Chat
    1

    View Slide

  2. ActionCable - Not Another Chat App - by Jesse Wolgamott | RailsConf 2016
    ActionCable
    Not Another Chat App
    2

    View Slide

  3. ActionCable - Not Another Chat App - by Jesse Wolgamott | RailsConf 2016 3
    What are we going to do?
    The Agenda

    History
    ƽ
    What is ActionCable? Building Blocks Patterns
    ǹ

    View Slide

  4. ActionCable - Not Another Chat App - by Jesse Wolgamott | RailsConf 2016
    Jesse Wolgamott
    @jwo
    [email protected]
    [email protected]
    htttp://jessewolgamott.com
    4

    View Slide

  5. ƽ
    HISTORY
    Web Sockets
    https://unsplash.com/photos/j_Ch0mwBNds

    View Slide

  6. ActionCable - Not Another Chat App - by Jesse Wolgamott | RailsConf 2016
    ƽ F5 Refresh
    Or, Hotmail
    6

    View Slide

  7. ActionCable - Not Another Chat App - by Jesse Wolgamott | RailsConf 2016
    F5 Refresh
    Or, Hotmail
    7

    View Slide

  8. ActionCable - Not Another Chat App - by Jesse Wolgamott | RailsConf 2016
    ƽ MetaTag Refresh
    Or, the Drudge Report page-load-increasinator-2000
    8

    View Slide

  9. ActionCable - Not Another Chat App - by Jesse Wolgamott | RailsConf 2016
    MetaTag Refresh
    Or, the Drudge Report page-load-increasinator-2000
    9
    1

    View Slide

  10. ActionCable - Not Another Chat App - by Jesse Wolgamott | RailsConf 2016
    ƽ Polling
    Good Enough for Campfire and Basecamp for a Decade
    10

    View Slide

  11. ActionCable - Not Another Chat App - by Jesse Wolgamott | RailsConf 2016
    Polling
    Good Enough for Campfire and Basecamp for a Decade
    11

    View Slide

  12. ActionCable - Not Another Chat App - by Jesse Wolgamott | RailsConf 2016
    Polling
    Good Enough for Campfire and Basecamp for a Decade
    12

    View Slide

  13. ActionCable - Not Another Chat App - by Jesse Wolgamott | RailsConf 2016
    Polling
    Good Enough for Campfire and Basecamp for a Decade
    13
    1 $(window).ready(function(){
    2 setInterval(function(){
    3 $.ajax("/api/messages")
    4 }, 3000)
    5 })

    View Slide

  14. ActionCable - Not Another Chat App - by Jesse Wolgamott | RailsConf 2016
    ƽ Web Sockets
    So Hot Right Now
    14

    View Slide

  15. ActionCable - Not Another Chat App - by Jesse Wolgamott | RailsConf 2016
    Web Sockets
    So Hot Right Now
    15
    The WebSocket Protocol enables two-way
    communication between a client running
    untrusted code in a controlled environment to a
    remote host that has opted-in to communications
    from that code.
    RFC 6455 (2011)

    View Slide

  16. ActionCable - Not Another Chat App - by Jesse Wolgamott | RailsConf 2016
    Web Sockets
    So Hot Right Now
    16
    The WebSocket Protocol enables two-way
    communication between a client running
    untrusted code in a controlled environment to a
    remote host that has opted-in to communications
    from that code.
    RFC 6455 (2011)

    View Slide

  17. ActionCable - Not Another Chat App - by Jesse Wolgamott | RailsConf 2016
    Web Sockets
    So Hot Right Now
    17
    socket.io

    View Slide

  18. ActionCable - Not Another Chat App - by Jesse Wolgamott | RailsConf 2016
    Web Sockets
    So Hot Right Now
    18
    starfighter.io

    View Slide

  19. ActionCable - Not Another Chat App - by Jesse Wolgamott | RailsConf 2016
    Web Sockets
    So Hot Right Now
    19
    elixir / phoenix - 2 million web socket connections

    View Slide

  20. ActionCable - Not Another Chat App - by Jesse Wolgamott | RailsConf 2016
    Web Sockets
    So Hot Right Now
    20
    Is the future Real Time?
    Probably Not.

    View Slide

  21. ActionCable - Not Another Chat App - by Jesse Wolgamott | RailsConf 2016
    Web Sockets
    So Hot Right Now
    21
    But for certain apps, that require / like very low
    latency, WebSockets is TreatSoYelf good.

    View Slide

  22. ActionCable - Not Another Chat App - by Jesse Wolgamott | RailsConf 2016
    Web Sockets
    So Hot Right Now
    22

    View Slide

  23. WHAT
    IT IS
    (& NOT)
    https://unsplash.com/photos/CoD2Q92UaEg

    View Slide

  24. ActionCable - Not Another Chat App - by Jesse Wolgamott | RailsConf 2016
    What ActionCable is NOT
    (silver bullet)
    24

    View Slide

  25. ActionCable - Not Another Chat App - by Jesse Wolgamott | RailsConf 2016
    What ActionCable is NOT
    (silver bullet)
    25
    For non rails Apps

    View Slide

  26. ActionCable - Not Another Chat App - by Jesse Wolgamott | RailsConf 2016
    What ActionCable is NOT
    (silver bullet)
    26
    Step forward for Rails
    into more JS
    integration

    View Slide

  27. ActionCable - Not Another Chat App - by Jesse Wolgamott | RailsConf 2016
    What ActionCable is NOT
    (silver bullet)
    27

    View Slide

  28. ActionCable - Not Another Chat App - by Jesse Wolgamott | RailsConf 2016
    What ActionCable IS
    Solid. Productive. Fun.
    28

    View Slide

  29. ActionCable - Not Another Chat App - by Jesse Wolgamott | RailsConf 2016
    What ActionCable IS
    Solid. Productive. Fun.
    29
    Solid Feature for Rails
    Monoliths

    View Slide

  30. ActionCable - Not Another Chat App - by Jesse Wolgamott | RailsConf 2016
    What ActionCable IS
    Solid. Productive. Fun.
    30
    Like Turbolinks and JS
    responses, a way to
    snapify rails

    View Slide

  31. ActionCable - Not Another Chat App - by Jesse Wolgamott | RailsConf 2016
    What ActionCable IS
    Solid. Productive. Fun.
    31
    Easy and Fun

    View Slide

  32. ActionCable - Not Another Chat App - by Jesse Wolgamott | RailsConf 2016
    Why “No Chat Apps”
    Hello World!
    32

    View Slide

  33. ActionCable - Not Another Chat App - by Jesse Wolgamott | RailsConf 2016
    Why “No Chat Apps”
    Hello World
    33
    Chat Apps are the
    hello world of
    websockets

    View Slide

  34. ActionCable - Not Another Chat App - by Jesse Wolgamott | RailsConf 2016
    Why “No Chat Apps”
    Hello World
    34
    Most apps do not
    need chat between
    two users

    View Slide

  35. ActionCable - Not Another Chat App - by Jesse Wolgamott | RailsConf 2016
    Why “No Chat Apps”
    Hello World
    35
    Some apps do need
    communication
    between server and
    client tho

    View Slide

  36. ActionCable - Not Another Chat App - by Jesse Wolgamott | RailsConf 2016
    Why “No Chat Apps”
    Hello World
    36
    Use Case 1:
    Collaboration

    View Slide

  37. ActionCable - Not Another Chat App - by Jesse Wolgamott | RailsConf 2016
    Why “No Chat Apps”
    Hello World
    37
    Use Case 2:
    Asynchronous tasks

    View Slide

  38. ActionCable - Not Another Chat App - by Jesse Wolgamott | RailsConf 2016
    First ActionCable Beta
    a short, short history
    38

    View Slide

  39. ActionCable - Not Another Chat App - by Jesse Wolgamott | RailsConf 2016
    First ActionCable Beta
    a short, short history
    39
    ActionCable
    announced as
    vaporware at
    RailsConf 2015

    View Slide

  40. ActionCable - Not Another Chat App - by Jesse Wolgamott | RailsConf 2016
    First ActionCable Beta
    a short, short history
    40

    View Slide

  41. ActionCable - Not Another Chat App - by Jesse Wolgamott | RailsConf 2016
    First ActionCable Beta
    a short, short history
    41
    Released as preview
    beta in July 2015

    View Slide

  42. ActionCable - Not Another Chat App - by Jesse Wolgamott | RailsConf 2016
    First ActionCable Beta
    a short, short history
    42
    Was a Non-Rails
    Experience

    View Slide

  43. ActionCable - Not Another Chat App - by Jesse Wolgamott | RailsConf 2016
    First ActionCable Beta
    a short, short history
    43
    Was a Non-Rails
    Experience
    No Generators

    View Slide

  44. ActionCable - Not Another Chat App - by Jesse Wolgamott | RailsConf 2016
    First ActionCable Beta
    a short, short history
    44
    Was a Non-Rails
    Experience
    No Deployment Story

    View Slide

  45. ActionCable - Not Another Chat App - by Jesse Wolgamott | RailsConf 2016
    First ActionCable Beta
    a short, short history
    45
    Was a Non-Rails
    Experience
    No Standalone

    View Slide

  46. ActionCable - Not Another Chat App - by Jesse Wolgamott | RailsConf 2016
    Current Beta Version
    a short, short history
    46

    View Slide

  47. ActionCable - Not Another Chat App - by Jesse Wolgamott | RailsConf 2016
    First ActionCable Beta
    a short, short history
    47
    5.0.0.beta3 released
    Feb 2016

    View Slide

  48. ActionCable - Not Another Chat App - by Jesse Wolgamott | RailsConf 2016
    First ActionCable Beta
    a short, short history
    48

    View Slide

  49. BUILDING
    BLOCKS
    https://unsplash.com/photos/osSryggkso4

    View Slide

  50. ActionCable - Not Another Chat App - by Jesse Wolgamott | RailsConf 2016
    The Modules
    Quick Overview
    50

    View Slide

  51. ActionCable - Not Another Chat App - by Jesse Wolgamott | RailsConf 2016 51
    The Modules
    Quick Overview
    Ɇ
    Channel
    Streaming channels clients can subscribe to. Channels
    have a name, which can be dynamic `cart_#{user_id}`
    Ʊ
    Cable
    The upgraded connection between browser and
    server. There’s one connection, many channels.
    Subscription
    JS Side, you subscribe to a channel, and handle when
    you `received` data and broadcast back to server.
    Broadcast
    From Rails, you broadcast messages to a channel
    ActionCable

    View Slide

  52. ActionCable - Not Another Chat App - by Jesse Wolgamott | RailsConf 2016
    Ʊ Cable
    Connection from Client to Rails
    52

    View Slide

  53. ActionCable - Not Another Chat App - by Jesse Wolgamott | RailsConf 2016
    Cable
    Connection from Client to Rails
    53
    1 //= require action_cable
    2 //= require_self
    3 //= require_tree ./channels
    4
    5 this.App || (this.App = {});
    6
    7 App.cable = ActionCable.createConsumer();
    8

    View Slide

  54. ActionCable - Not Another Chat App - by Jesse Wolgamott | RailsConf 2016
    Cable
    Connection from Client to Rails
    54
    1 Rails.application.routes.draw do
    2
    3 # Serve websocket cable requests in-process
    4 mount ActionCable.server => '/cable'
    5 end

    View Slide

  55. ActionCable - Not Another Chat App - by Jesse Wolgamott | RailsConf 2016
    Ɇ Channel
    Rooms. Many Many Rooms.
    55

    View Slide

  56. ActionCable - Not Another Chat App - by Jesse Wolgamott | RailsConf 2016
    Channel
    Rooms. Many Many Rooms.
    56
    1 #app/channels/product_channel.rb
    2 class ProductChannel < ApplicationCable::Channel
    3 def subscribed
    4 stream_from "products"
    5 end
    6
    7 def unsubscribed
    8 # Any cleanup needed when channel is unsubscribed
    9 end
    10
    11 end

    View Slide

  57. ActionCable - Not Another Chat App - by Jesse Wolgamott | RailsConf 2016
    Channel
    Rooms. Many Many Rooms.
    57
    1 # app/channels/application_cable/connection.rb
    2 module ApplicationCable
    3 class Connection < ActionCable::Connection::Base
    4
    5 identified_by :current_user
    6
    7 def connect
    8 self.current_user = User.find_by(id: cookies.signed[:user_id])
    9 # reject_unauthorized_connection unless self.current_user
    10 end
    11
    12 end
    13 end

    View Slide

  58. ActionCable - Not Another Chat App - by Jesse Wolgamott | RailsConf 2016
    Channel
    Rooms. Many Many Rooms.
    58
    1 # app/controllers/application_controller.rb
    2 class ApplicationController < ActionController::Base
    3 # ...
    4 before_action do
    5 @current_user = User.find_by id: cookies.signed[:user_id]
    6 end
    7
    8 def sign_in(user)
    9 cookies.signed[:user_id] = user.id
    10 end
    11 # ...
    12 end

    View Slide

  59. ActionCable - Not Another Chat App - by Jesse Wolgamott | RailsConf 2016
    Broadcast
    From Rails, you broadcast messages to a channel
    59

    View Slide

  60. ActionCable - Not Another Chat App - by Jesse Wolgamott | RailsConf 2016
    Broadcast
    Connection from Client to Rails
    60
    1 # app/jobs/inventory_syncer_job.rb
    2 class InventorySyncerJob < ApplicationJob
    3 queue_as :default
    4
    5 def perform(product)
    6 ActionCable.server.broadcast "products", {
    7 product: product,
    8 template: draw(product)
    9 }
    10
    11 end
    12
    13 def draw(product)
    14 ApplicationController.render(partial: 'products/product',
    15 locals: { product: product })
    16 end
    17 end

    View Slide

  61. ActionCable - Not Another Chat App - by Jesse Wolgamott | RailsConf 2016
    Subscription
    That which your JS can subscribe to
    61

    View Slide

  62. ActionCable - Not Another Chat App - by Jesse Wolgamott | RailsConf 2016
    Subscription
    That which your JS can subscribe to
    62
    1 // app/assets/javascripts/channels/product.js
    2 App.product = App.cable.subscriptions.create("ProductChannel", {
    3 connected: function() {},
    4 disconnected: function() {},
    5 received: function(data) {
    6 var id = data.product.id;
    7 $(".product[data-product-id=" + id + "]")
    8 .replaceWith(data.template);
    9 }
    10 });

    View Slide

  63. ActionCable - Not Another Chat App - by Jesse Wolgamott | RailsConf 2016
    Flow
    How the Magic Magics
    63

    View Slide

  64. ActionCable - Not Another Chat App - by Jesse Wolgamott | RailsConf 2016
    Cable
    Connection from Client to Rails
    64
    By requiring
    actioncable, the cable
    cables to the rails
    cable

    View Slide

  65. ActionCable - Not Another Chat App - by Jesse Wolgamott | RailsConf 2016
    Cable
    Connection from Client to Rails
    65
    The browser
    upgrades the “/cable”
    connection to a web
    sockets connection.

    View Slide

  66. ActionCable - Not Another Chat App - by Jesse Wolgamott | RailsConf 2016
    Cable
    Connection from Client to Rails
    66
    When a Subscription
    Subscribed, A
    Connection Connects

    View Slide

  67. ActionCable - Not Another Chat App - by Jesse Wolgamott | RailsConf 2016
    Cable
    Connection from Client to Rails
    67
    To start listening for
    updates, you create a
    subscription with a
    received function

    View Slide

  68. ActionCable - Not Another Chat App - by Jesse Wolgamott | RailsConf 2016
    Cable
    Connection from Client to Rails
    68
    You can have many
    subscriptions in many
    ways in many places

    View Slide

  69. ActionCable - Not Another Chat App - by Jesse Wolgamott | RailsConf 2016
    Cable
    Connection from Client to Rails
    69
    Access the
    `App.cable.channel`
    inside JS: page loads,
    React, etc

    View Slide

  70. ActionCable - Not Another Chat App - by Jesse Wolgamott | RailsConf 2016
    Cable
    Connection from Client to Rails
    70
    The subscription calls
    `received(data)`
    when subscription
    receives data

    View Slide

  71. ActionCable - Not Another Chat App - by Jesse Wolgamott | RailsConf 2016
    Cable
    Connection from Client to Rails
    71

    View Slide

  72. ǹ
    PATTERNS
    https://unsplash.com/photos/bk22E-Sp65k

    View Slide

  73. ActionCable - Not Another Chat App - by Jesse Wolgamott | RailsConf 2016
    ǹ Data Updates
    Data is the Data
    73

    View Slide

  74. ActionCable - Not Another Chat App - by Jesse Wolgamott | RailsConf 2016
    Data Updates
    Data is the Data
    74
    Keep Data In Sync
    Across Tabs and
    Users
    ǹ

    View Slide

  75. ActionCable - Not Another Chat App - by Jesse Wolgamott | RailsConf 2016
    Data Updates
    Data is the Data
    75
    In-Sync
    ǹ

    View Slide

  76. ActionCable - Not Another Chat App - by Jesse Wolgamott | RailsConf 2016
    Data Updates
    Data is the Data
    76
    ǹ
    Add To Cart
    Tab Two
    WHAT ABOUT TAB TWO
    Cart on Tab One
    Updates

    View Slide

  77. ActionCable - Not Another Chat App - by Jesse Wolgamott | RailsConf 2016
    Data Updates
    Data is the Data
    77
    After Update,
    Controller (or Job)
    Broadcasts Message
    ǹ

    View Slide

  78. ActionCable - Not Another Chat App - by Jesse Wolgamott | RailsConf 2016
    Data Updates
    Data is the Data
    78
    Message can either
    be data
    ǹ

    View Slide

  79. ActionCable - Not Another Chat App - by Jesse Wolgamott | RailsConf 2016
    Data Updates
    Data is the Data
    79
    or a Rails rendered
    template partial for
    you to update with
    ǹ

    View Slide

  80. ActionCable - Not Another Chat App - by Jesse Wolgamott | RailsConf 2016
    Data Updates
    Data is the Data
    80
    ǹ
    1 ApplicationController.render(partial: 'products/product',
    2 locals: { product: @product })

    View Slide

  81. ActionCable - Not Another Chat App - by Jesse Wolgamott | RailsConf 2016
    Data Updates
    Data is the Data
    81
    ǹ
    1 ActionCable.server.broadcast "cart_#{current_user.id}", {
    2 template: ApplicationController.render(partial: 'carts/cart',
    3 locals: { cart: @cart })
    4 }

    View Slide

  82. ActionCable - Not Another Chat App - by Jesse Wolgamott | RailsConf 2016
    Data Updates
    Data is the Data
    82
    ǹ
    1 # app/channels/cart_channel.rb
    2 class CartChannel < ApplicationCable::Channel
    3 def subscribed
    4 stream_from "cart_#{current_user.id}"
    5 end
    6 end

    View Slide

  83. ActionCable - Not Another Chat App - by Jesse Wolgamott | RailsConf 2016
    Data Updates
    Data is the Data
    83
    ǹ
    1 // app/assets/javascripts/channels/cart.js
    2 App.cart = App.cable.subscriptions.create(“cart_", + current_user.id, {
    3 received: function(data) {
    4 $("#cart").replaceWith(data.template);
    5 }
    6 });
    7

    View Slide

  84. ActionCable - Not Another Chat App - by Jesse Wolgamott | RailsConf 2016
    ǹ JavaScript Hooks
    JS to the rescue
    84

    View Slide

  85. ActionCable - Not Another Chat App - by Jesse Wolgamott | RailsConf 2016
    JavaScript Hooks
    JS to the rescue
    85
    Sending Messages to
    Server, and out to
    other clients
    ǹ

    View Slide

  86. ActionCable - Not Another Chat App - by Jesse Wolgamott | RailsConf 2016
    JavaScript Hooks
    JS to the rescue
    86
    Example: HTML5
    Slides Presenter
    Mode
    ǹ

    View Slide

  87. ActionCable - Not Another Chat App - by Jesse Wolgamott | RailsConf 2016
    JavaScript Hooks
    You + Me = We
    87
    ǹ
    1 Reveal.addEventListener( 'slidechanged', function( event ) {
    2
    3 var slide = {
    4 "indexh": event.indexh,
    5 "indexv": event.indexv,
    6 "indexf": event.indexf
    7 }
    8 App.slidesNotification.advanceSlide(slide);
    9 } );
    JS Object
    Becomes Ruby Hash
    Channel Method
    Calls method on Rails Channel
    Channel Name
    Name of Rails Channel

    View Slide

  88. ActionCable - Not Another Chat App - by Jesse Wolgamott | RailsConf 2016
    JavaScript Hooks
    You + Me = We
    88
    ǹ
    1 App.slidesNotification = App.cable.subscriptions.create("SlidesChannel",
    2 {
    3
    4 advanceSlide: function(data) {
    5 // tell action-cable rails to execute something
    6 return this.perform('slides', data);
    7 },
    8
    9 received: function(data) {
    10 return Reveal.slide(data.indexh, data.indexv, data.indexf);
    11 }
    12 });

    View Slide

  89. ActionCable - Not Another Chat App - by Jesse Wolgamott | RailsConf 2016
    JavaScript Hooks
    You + Me = We
    89
    ǹ
    1 class SlidesChannel < ApplicationCable::Channel
    2 def subscribed
    3 stream_from "slides"
    4 end
    5
    6 def slides(data)
    7 # We only want to let the admin (current_user) advance slides
    8 if current_user.admin?
    9 ActionCable.server.broadcast "slides", data.slice("indexh",
    10 "indexv",
    11 "indexf")
    12 end
    13 end
    14 end

    View Slide

  90. ActionCable - Not Another Chat App - by Jesse Wolgamott | RailsConf 2016
    JavaScript Hooks
    JS to the rescue
    90
    Messages go from
    Client to Server, not
    P2P
    ǹ

    View Slide

  91. ActionCable - Not Another Chat App - by Jesse Wolgamott | RailsConf 2016
    ǹ Collaboration
    You + Me = We
    91

    View Slide

  92. ActionCable - Not Another Chat App - by Jesse Wolgamott | RailsConf 2016
    Collaboration
    You + Me = We
    92
    ǹ
    Trello

    View Slide

  93. ActionCable - Not Another Chat App - by Jesse Wolgamott | RailsConf 2016
    Collaboration
    You + Me = We
    93
    ǹ
    Pivotal Tracker

    View Slide

  94. ActionCable - Not Another Chat App - by Jesse Wolgamott | RailsConf 2016
    Collaboration
    You + Me = We
    94
    ǹ
    GitHub Updates on CI
    Testing

    View Slide

  95. ActionCable - Not Another Chat App - by Jesse Wolgamott | RailsConf 2016
    ǹ Asynchronous Tasks
    Tell Me Later
    95

    View Slide

  96. ActionCable - Not Another Chat App - by Jesse Wolgamott | RailsConf 2016
    Asynchronous Tasks
    Tell me later
    96
    ǹ
    1 class MechanizeChannel < ApplicationCable::Channel
    2 def subscribed
    3 stream_from "search_#{params[:uuid]}"
    4 end
    5
    6 end

    View Slide

  97. ActionCable - Not Another Chat App - by Jesse Wolgamott | RailsConf 2016
    Asynchronous Tasks
    Tell me later
    97
    ǹ
    1 var uuid = guid();
    2
    3 $.ajax({
    4 url: '/api/search',
    5 method: 'POST',
    6 data: {
    7 search: term,
    8 uuid: uuid
    9 }
    10 })

    View Slide

  98. ActionCable - Not Another Chat App - by Jesse Wolgamott | RailsConf 2016
    Asynchronous Tasks
    Tell me later
    98
    ǹ
    1 class SiteSearchJob < ApplicationJob
    2
    3 def perform(search, uuid)
    4 stream = "search_#{search_uuid}"
    5
    6 ActionCable.server.broadcast(stream, status: START)
    7
    8 # ...
    9
    10 ActionCable.server.broadcast(
    11 stream,
    12 status: COMPLETE,
    13 template: action_template
    14 )
    15
    16 end
    17 end

    View Slide

  99. ActionCable - Not Another Chat App - by Jesse Wolgamott | RailsConf 2016
    ǹ And Yes, Chatting
    (…)
    99

    View Slide

  100. ActionCable - Not Another Chat App - by Jesse Wolgamott | RailsConf 2016
    And Yes, Chatting

    100
    ǹ

    View Slide

  101. ActionCable - Not Another Chat App - by Jesse Wolgamott | RailsConf 2016
    ǹ Deployment - In-App
    (…)
    101

    View Slide

  102. ActionCable - Not Another Chat App - by Jesse Wolgamott | RailsConf 2016
    Deployment - Standalone
    > 1,000 concurrent
    102
    ǹ
    Can be done with
    only 1 server and no
    resque / sidekiq

    View Slide

  103. ActionCable - Not Another Chat App - by Jesse Wolgamott | RailsConf 2016
    Deployment - Standalone
    > 1,000 concurrent
    103
    ǹ
    1 # config/environments/production.rb
    2 config.active_job.queue_adapter = :async
    3
    4 # config/cable.yml
    5 production:
    6 adapter: async
    7 development:
    8 adapter: async
    9 test:
    10 adapter: async
    11

    View Slide

  104. ActionCable - Not Another Chat App - by Jesse Wolgamott | RailsConf 2016
    Deployment - Standalone
    > 1,000 concurrent
    104
    ǹ
    Everything is done
    async (threaded)

    View Slide

  105. ActionCable - Not Another Chat App - by Jesse Wolgamott | RailsConf 2016
    Deployment - Standalone
    > 1,000 concurrent
    105
    ǹ
    ActionCable connects
    to `/cable` on Rails
    Server

    View Slide

  106. ActionCable - Not Another Chat App - by Jesse Wolgamott | RailsConf 2016
    Deployment - Standalone
    > 1,000 concurrent
    106
    ǹ
    If more than 1 server/
    dyno, use Redis

    View Slide

  107. ActionCable - Not Another Chat App - by Jesse Wolgamott | RailsConf 2016
    ǹ Deployment - Standalone
    (…)
    107

    View Slide

  108. ActionCable - Not Another Chat App - by Jesse Wolgamott | RailsConf 2016
    Deployment - Standalone
    > 1,000 concurrent
    108
    ǹ
    Slightly more involved

    View Slide

  109. ActionCable - Not Another Chat App - by Jesse Wolgamott | RailsConf 2016
    Deployment - Standalone
    > 1,000 concurrent
    109
    ǹ
    (same scale as
    moving from async to
    Sidekiq)

    View Slide

  110. ActionCable - Not Another Chat App - by Jesse Wolgamott | RailsConf 2016
    Deployment - Standalone
    > 1,000 concurrent
    110
    ǹ
    Connect via Redis

    View Slide

  111. ActionCable - Not Another Chat App - by Jesse Wolgamott | RailsConf 2016
    Deployment - Standalone
    > 1,000 concurrent
    111
    ǹ
    sockets.domain.io

    View Slide

  112. ActionCable - Not Another Chat App - by Jesse Wolgamott | RailsConf 2016
    Deployment - Standalone
    > 1,000 concurrent
    112
    ǹ
    Configure
    config.action_cable.url

    View Slide

  113. ActionCable - Not Another Chat App - by Jesse Wolgamott | RailsConf 2016
    Deployment - Standalone
    > 1,000 concurrent
    113
    ǹ

    View Slide

  114. ActionCable - Not Another Chat App - by Jesse Wolgamott | RailsConf 2016
    Deployment - Standalone
    > 1,000 concurrent
    114
    ǹ

    View Slide

  115. ActionCable - Not Another Chat App - by Jesse Wolgamott | RailsConf 2016
    Deployment - Standalone
    > 1,000 concurrent
    115
    ǹ

    View Slide

  116. ActionCable - Not Another Chat App - by Jesse Wolgamott | RailsConf 2016
    Deployment - Standalone
    > 1,000 concurrent
    116
    ǹ

    View Slide

  117. ActionCable - Not Another Chat App - by Jesse Wolgamott | RailsConf 2016
    ǹ Gotchas
    WATCH OUT
    117

    View Slide

  118. ActionCable - Not Another Chat App - by Jesse Wolgamott | RailsConf 2016
    Gotchas
    WATCH OUT
    118
    Missed Messages
    ǹ

    View Slide

  119. ActionCable - Not Another Chat App - by Jesse Wolgamott | RailsConf 2016
    Gotchas
    WATCH OUT
    119
    Best to always use
    ActionJob to
    broadcast
    ǹ

    View Slide

  120. ActionCable - Not Another Chat App - by Jesse Wolgamott | RailsConf 2016
    Gotchas
    WATCH OUT
    120
    No Session
    Only Cookies
    ǹ

    View Slide

  121. ActionCable - Not Another Chat App - by Jesse Wolgamott | RailsConf 2016
    Gotchas
    WATCH OUT
    121
    Devise
    ǹ
    1 # app/config/initializers/warden_hooks.rb
    2 Warden::Manager.after_set_user do |resource,auth,opts|
    3 scope = opts[:scope]
    4 auth.cookies.signed[“#{scope}_id"] = resource.id
    5 end
    http://www.rubytutorial.io/actioncable-devise-authentication/

    View Slide

  122. ActionCable - Not Another Chat App - by Jesse Wolgamott | RailsConf 2016
    Gotchas
    WATCH OUT
    122
    Request Origins
    ǹ
    1 # config/environments/production.rb
    2 config.action_cable.allowed_request_origins = [
    3 'http://example.com', # DON'T TRAIL a '/'
    4 ]
    5

    View Slide

  123. ActionCable - Not Another Chat App - by Jesse Wolgamott | RailsConf 2016
    Gotchas
    WATCH OUT
    123
    Production
    ǹ
    1 # config/cable.yml
    2 production:
    3 adapter: redis
    4 url: redis://localhost:6379/1
    5
    6 development:
    7 adapter: async
    8
    9 test:
    10 adapter: async

    View Slide

  124. ActionCable - Not Another Chat App - by Jesse Wolgamott | RailsConf 2016
    Where to go from here?
    Now What?
    124

    View Slide

  125. ActionCable - Not Another Chat App - by Jesse Wolgamott | RailsConf 2016
    Where to go from here?
    Now What?
    125
    Simple Example:
    https://github.com/
    jwo/inventory-cable
    ǹ

    View Slide

  126. ActionCable - Not Another Chat App - by Jesse Wolgamott | RailsConf 2016
    Where to go from here?
    Now What?
    126
    https://github.com/
    rails/actioncable-
    examples
    ǹ

    View Slide

  127. ActionCable - Not Another Chat App - by Jesse Wolgamott | RailsConf 2016
    Where to go from here?
    Now What?
    127
    Documentation:
    https://github.com/
    rails/rails/tree/
    master/actioncable
    ǹ

    View Slide

  128. ActionCable - Not Another Chat App - by Jesse Wolgamott | RailsConf 2016
    ! Many Thanks
    !!!!!
    128

    View Slide

  129. ActionCable - Not Another Chat App - by Jesse Wolgamott | RailsConf 2016
    Many Thanks
    !!!!!!
    129
    DHH

    View Slide

  130. ActionCable - Not Another Chat App - by Jesse Wolgamott | RailsConf 2016
    Many Thanks
    !!!!!!
    130
    42+ Contributors

    View Slide

  131. ActionCable - Not Another Chat App - by Jesse Wolgamott | RailsConf 2016
    Many Thanks
    !!!!!!
    131
    Hiring Educators

    View Slide

  132. ActionCable - Not Another Chat App - by Jesse Wolgamott | RailsConf 2016
    Many Thanks
    !!!!!!
    132
    I’m @JWo
    Thanks!!!!11!!!!

    View Slide