| 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 })
| 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)
| 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)
| 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
| 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
| 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
| 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