what's new in rails 5
TRANSCRIPT
![Page 1: What's New in Rails 5](https://reader034.vdocument.in/reader034/viewer/2022052514/58f32f511a28ab99388b45a7/html5/thumbnails/1.jpg)
What’s new in Rails 5
![Page 2: What's New in Rails 5](https://reader034.vdocument.in/reader034/viewer/2022052514/58f32f511a28ab99388b45a7/html5/thumbnails/2.jpg)
Turbolinks
ActionCable
![Page 3: What's New in Rails 5](https://reader034.vdocument.in/reader034/viewer/2022052514/58f32f511a28ab99388b45a7/html5/thumbnails/3.jpg)
Turbolinksnavigating web application faster
![Page 4: What's New in Rails 5](https://reader034.vdocument.in/reader034/viewer/2022052514/58f32f511a28ab99388b45a7/html5/thumbnails/4.jpg)
Turbolinks: Features
• Optimizes navigation automatically
• No server-side cooperation necessary
• Respects the web
• Supports mobile apps
![Page 5: What's New in Rails 5](https://reader034.vdocument.in/reader034/viewer/2022052514/58f32f511a28ab99388b45a7/html5/thumbnails/5.jpg)
Turbolinks: How it works
1. intercepts all clicks to links to the same domain
2. changes the browser’s URL using the History API
3. requests the new page using XHR
4. replaces the current <body> element and merges the contents of the <head> element
![Page 6: What's New in Rails 5](https://reader034.vdocument.in/reader034/viewer/2022052514/58f32f511a28ab99388b45a7/html5/thumbnails/6.jpg)
Turbolinks Example
![Page 7: What's New in Rails 5](https://reader034.vdocument.in/reader034/viewer/2022052514/58f32f511a28ab99388b45a7/html5/thumbnails/7.jpg)
ActionCablefull-stack, real-time, publish-subscribe framework for
Rails 5
![Page 8: What's New in Rails 5](https://reader034.vdocument.in/reader034/viewer/2022052514/58f32f511a28ab99388b45a7/html5/thumbnails/8.jpg)
Usage• Chat
• Multiplayer Games
• Updates
• Feeds
• Collaborative Editing and Coding
• etc.
![Page 9: What's New in Rails 5](https://reader034.vdocument.in/reader034/viewer/2022052514/58f32f511a28ab99388b45a7/html5/thumbnails/9.jpg)
WebSocket• TCP-based protocol
• Persistent (long-lived)
• Bi-directional (full-duplex)
• Designed to be implemented in web browsers and web servers
• Supports encrypted connection
• Introduces URI schemes: ws, wss
![Page 10: What's New in Rails 5](https://reader034.vdocument.in/reader034/viewer/2022052514/58f32f511a28ab99388b45a7/html5/thumbnails/10.jpg)
WebSocket: Standards
IETF: RFC 6455
The WebSocket Protocol
W3C Web IDL
The WebSocket API
Standardised in 2011
![Page 11: What's New in Rails 5](https://reader034.vdocument.in/reader034/viewer/2022052514/58f32f511a28ab99388b45a7/html5/thumbnails/11.jpg)
can-i-use-itYes!
![Page 12: What's New in Rails 5](https://reader034.vdocument.in/reader034/viewer/2022052514/58f32f511a28ab99388b45a7/html5/thumbnails/12.jpg)
WebSocket: Handshakeprotocol switch from HTTP to WebSocket
![Page 13: What's New in Rails 5](https://reader034.vdocument.in/reader034/viewer/2022052514/58f32f511a28ab99388b45a7/html5/thumbnails/13.jpg)
WebSocket: Handshake ExampleCLIENT REQUEST
GET /cable HTTP/1.1Host: zeroeleven.rs:3000Cookie: session=N3Yyc0Z0cVdmUpgrade: websocketConnection: UpgradeSec-WebSocket-Key: dGhlIHNhbXBsZSBub25jZQ==Sec-WebSocket-Version: 13
SERVER RESPONSE
HTTP/1.1 101 Switching ProtocolsUpgrade: websocket
Connection: UpgradeSec-WebSocket-Accept:
s3pPLMBiTxaQ9kYGzzhZRbK+xOo=
![Page 14: What's New in Rails 5](https://reader034.vdocument.in/reader034/viewer/2022052514/58f32f511a28ab99388b45a7/html5/thumbnails/14.jpg)
WebSocketVisual Representation
![Page 15: What's New in Rails 5](https://reader034.vdocument.in/reader034/viewer/2022052514/58f32f511a28ab99388b45a7/html5/thumbnails/15.jpg)
WebSocket: API
var ws = new WebSocket("ws://zeroeleven.rs");
ws.onopen = function(evt) { alert("Connection open..."); };ws.onmessage = function(evt) { alert("Received Message: " + evt.data); };ws.onclose = function(evt) { alert("Connection closed."); };
ws.send("Hello from Start-it!");ws.close();
![Page 16: What's New in Rails 5](https://reader034.vdocument.in/reader034/viewer/2022052514/58f32f511a28ab99388b45a7/html5/thumbnails/16.jpg)
TerminologyConnection
Consumer
Channel
Subscriber
Subscription
Pub/Sub
Broadcasting
![Page 17: What's New in Rails 5](https://reader034.vdocument.in/reader034/viewer/2022052514/58f32f511a28ab99388b45a7/html5/thumbnails/17.jpg)
Pub/Sub Adapters
async
Redis
PostgreSQL
![Page 18: What's New in Rails 5](https://reader034.vdocument.in/reader034/viewer/2022052514/58f32f511a28ab99388b45a7/html5/thumbnails/18.jpg)
Running the ActionCable Server
In-App
Standalone
![Page 19: What's New in Rails 5](https://reader034.vdocument.in/reader034/viewer/2022052514/58f32f511a28ab99388b45a7/html5/thumbnails/19.jpg)
Visualisation of ActionCable in Production
![Page 20: What's New in Rails 5](https://reader034.vdocument.in/reader034/viewer/2022052514/58f32f511a28ab99388b45a7/html5/thumbnails/20.jpg)
$ rails new start-it
![Page 21: What's New in Rails 5](https://reader034.vdocument.in/reader034/viewer/2022052514/58f32f511a28ab99388b45a7/html5/thumbnails/21.jpg)
app/ channels/ application_cable/ channel.rb connection.rb
app/ assets/ javascripts/ cable.js
![Page 22: What's New in Rails 5](https://reader034.vdocument.in/reader034/viewer/2022052514/58f32f511a28ab99388b45a7/html5/thumbnails/22.jpg)
# 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
def find_verified_user if verified_user = User.find_by(id: cookies.signed[:user_id]) verified_user else reject_unauthorized_connection end end endend
![Page 23: What's New in Rails 5](https://reader034.vdocument.in/reader034/viewer/2022052514/58f32f511a28ab99388b45a7/html5/thumbnails/23.jpg)
// app/assets/javascripts/cable.js
//= require action_cable//= require_self//= require_tree ./channels
(function() { this.App || (this.App = {});
App.cable = ActionCable.createConsumer();
}).call(this);
![Page 24: What's New in Rails 5](https://reader034.vdocument.in/reader034/viewer/2022052514/58f32f511a28ab99388b45a7/html5/thumbnails/24.jpg)
$ rails generate channel room speak
![Page 25: What's New in Rails 5](https://reader034.vdocument.in/reader034/viewer/2022052514/58f32f511a28ab99388b45a7/html5/thumbnails/25.jpg)
app/ channels/ room_channel.rb
app/ assets/ javascripts/ channels/ room.coffee
![Page 26: What's New in Rails 5](https://reader034.vdocument.in/reader034/viewer/2022052514/58f32f511a28ab99388b45a7/html5/thumbnails/26.jpg)
# app/channels/room_channel.rb
class RoomChannel < ApplicationCable::Channel def subscribed stream_from "room_channel" end
def unsubscribed end
def speak(data) ActionCable.server.broadcast 'room_channel', message: data['message'] endend
![Page 27: What's New in Rails 5](https://reader034.vdocument.in/reader034/viewer/2022052514/58f32f511a28ab99388b45a7/html5/thumbnails/27.jpg)
# app/assets/javascripts/channels/room_channel.js
App.room = App.cable.subscriptions.create "RoomChannel", connected: ->
disconnected: ->
received: (data) -> $("#messages").append data["message"];
speak: (message) -> @perform "speak", message: message
App.room.speak "SIR, YES SIR!"
![Page 28: What's New in Rails 5](https://reader034.vdocument.in/reader034/viewer/2022052514/58f32f511a28ab99388b45a7/html5/thumbnails/28.jpg)
ActionCable Example