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 full-size slide

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

    View full-size 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 full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size 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 full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size 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 full-size slide

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

    View full-size 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 full-size 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 full-size slide

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

    View full-size slide

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

    View full-size 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 full-size 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 full-size 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 full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size 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 full-size slide

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

    View full-size slide

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

    View full-size 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 full-size 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 full-size slide

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

    View full-size slide

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

    View full-size 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 full-size 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 full-size 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 full-size slide

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

    View full-size 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 full-size slide

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

    View full-size 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 full-size slide

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

    View full-size 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 full-size 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 full-size 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 full-size 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 full-size 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 full-size slide

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

    View full-size 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 full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size 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 full-size slide

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

    View full-size 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 full-size 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 full-size slide

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

    View full-size 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 full-size 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 full-size 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 full-size slide

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

    View full-size 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 full-size slide

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

    View full-size 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 full-size slide

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

    View full-size 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 full-size 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 full-size 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 full-size 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 full-size 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 full-size 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 full-size 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 full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size 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 full-size slide

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

    View full-size 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 full-size 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 full-size 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 full-size 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 full-size 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 full-size 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 full-size 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 full-size 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 full-size slide

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

    View full-size 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 full-size 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 full-size 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 full-size 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 full-size 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 full-size 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 full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size 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 full-size 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 full-size 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 full-size slide

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

    View full-size slide

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

    100
    ǹ

    View full-size slide

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

    View full-size 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 full-size 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 full-size slide

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

    View full-size 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 full-size 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 full-size slide

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

    View full-size slide

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

    View full-size 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 full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size 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 full-size 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 full-size 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 full-size slide

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

    View full-size 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 full-size 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 full-size 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 full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide