Slide 1

Slide 1 text

HTML-over- WebSockets Vladimir Dementyev Evil Martians

Slide 2

Slide 2 text

palkan_tula palkan github.com/palkan 2

Slide 3

Slide 3 text

palkan_tula palkan Web applications 3

Slide 4

Slide 4 text

palkan_tula palkan Web applications 4 Data Representation

Slide 5

Slide 5 text

palkan_tula palkan Breakfast 🥐 5 Data Representation ?

Slide 6

Slide 6 text

palkan_tula palkan Breakfast 🥬 6 Data Representation ?

Slide 7

Slide 7 text

Canteen-style Ready-made Predefined set of dishes Wait in line Cheap & simple

Slide 8

Slide 8 text

Classic Rails Ready-made (HotW) Predefined set of responses Wait in line (queueing) Cheap & simple

Slide 9

Slide 9 text

DIY Raw food to cook Special equipment is required Expensive though tasty

Slide 10

Slide 10 text

SPA Raw data to render Frontenders are required Expensive though "cool" 😎

Slide 11

Slide 11 text

A la carte Waiters serve clients concurrently Order once, get fully-cooked food in batches Refills without requests The chef is a boss

Slide 12

Slide 12 text

HTML-over-WS Server serves clients concurrently Subscribe once, get HTML asynchronously Live updates w/o requests The server is a boss

Slide 13

Slide 13 text

palkan_tula palkan 13 Why does everyone try to build yet another Korean BBQ? Is HTML-over-WebSockets possible in 2021? The question

Slide 14

Slide 14 text

YES

Slide 15

Slide 15 text

palkan_tula palkan Frontendless Rails RailsConf 2021 15

Slide 16

Slide 16 text

palkan_tula palkan Frontendless Rails (live) 16

Slide 17

Slide 17 text

palkan_tula palkan HTML-over- WebSockets: The overview 17

Slide 18

Slide 18 text

palkan_tula palkan Phoenix LiveView 18

Slide 19

Slide 19 text

palkan_tula palkan Phoenix LiveView HTML elements «connects» to an Erlang process via WebSocket Process reacts on user interaction and internal server events, updates the state, re-renders the affected template parts and sends to the client Client uses morphdom to perform a fast DOM patching 19

Slide 20

Slide 20 text

palkan_tula palkan morphdom 20

Slide 21

Slide 21 text

palkan_tula palkan LiveView 21 https://my-live.app

Slide 22

Slide 22 text

palkan_tula palkan LiveView 21 https://my-live.app DOM element

Slide 23

Slide 23 text

palkan_tula palkan LiveView 21 https://my-live.app Erlang process DOM element

Slide 24

Slide 24 text

palkan_tula palkan LiveView 21 https://my-live.app Browser events Erlang process DOM element

Slide 25

Slide 25 text

palkan_tula palkan LiveView 21 https://my-live.app Browser events Partial HTML updates Erlang process DOM element

Slide 26

Slide 26 text

palkan_tula palkan LiveView 21 https://my-live.app Browser events Partial HTML updates Internal events Erlang process DOM element

Slide 27

Slide 27 text

palkan_tula palkan LiveView 21 https://my-live.app Browser events Partial HTML updates Internal events Erlang process DOM element

Slide 28

Slide 28 text

palkan_tula palkan Partial HTML updates 22
" id="<%= dom_id(item) %>"> >

<%= item.desc %>

Slide 29

Slide 29 text

palkan_tula palkan Partial HTML updates 23
" id="<%= dom_id(item) %>"> >

<%= item.desc %>

Static vs. Dynamic

Slide 30

Slide 30 text

palkan_tula palkan Partial HTML updates 23
" id="<%= dom_id(item) %>"> >

<%= item.desc %>

Static vs. Dynamic 0 1 2 3

Slide 31

Slide 31 text

palkan_tula palkan Partial HTML updates 23
" id="<%= dom_id(item) %>"> >

<%= item.desc %>

Static vs. Dynamic 0 1 2 3 { "0": "checked", "2": "checked" }

Slide 32

Slide 32 text

palkan_tula palkan Phoenix LiveView Component-driven architecture Erlang ecosystem (processes and message passing) Dedicated templating mechanism 24

Slide 33

Slide 33 text

palkan_tula palkan Back to Ruby 25

Slide 34

Slide 34 text

palkan_tula palkan “A new way to craft modern, reactive web interfaces with Ruby on Rails.” 26

Slide 35

Slide 35 text

palkan_tula palkan 27 Stimulus Reflex creator

Slide 36

Slide 36 text

palkan_tula palkan 27 Stimulus Reflex creator CableReady 🤔

Slide 37

Slide 37 text

palkan_tula palkan CableReady A library to broadcast DOM modification commands from server to browsers Uses Action Cable as a transport Uses morphdom to update HTML 28

Slide 38

Slide 38 text

palkan_tula palkan Example 29
... <%= button_to item_path(item), method: :delete, remote: true do %> ... <% end %>

Slide 39

Slide 39 text

palkan_tula palkan Example 30 # items_controller.rb def destroy item.destroy! stream = ListChannel.broadcasting_for(item.list) cable_ready[stream].remove(selector: dom_id(item)) head :no_content end

Slide 40

Slide 40 text

palkan_tula palkan # items_controller.rb def destroy item.destroy! stream = ListChannel.broadcasting_for(item.list) cable_ready[stream].remove(selector: dom_id(item)) head :no_content end Example 31 $(" ##{dom_id(item)}").remove()

Slide 41

Slide 41 text

palkan_tula palkan CableReady 32 cableready.stimulusreflex.com

Slide 42

Slide 42 text

palkan_tula palkan CableReady v5.0 Custom operations stream_from helper (like in Hotwire, see further) 33

Slide 43

Slide 43 text

palkan_tula palkan StimulusReflex Reflexes react on user actions and render HTML responses CableReady is use to send HTML to clients and to update DOM 34

Slide 44

Slide 44 text

No content

Slide 45

Slide 45 text

palkan_tula palkan StimulusReflex 36 https://my-reflex.app

Slide 46

Slide 46 text

palkan_tula palkan StimulusReflex 36 https://my-reflex.app DOM element

Slide 47

Slide 47 text

palkan_tula palkan StimulusReflex 36 https://my-reflex.app Browser events ➝ reflex class & action Reflex class DOM element

Slide 48

Slide 48 text

palkan_tula palkan StimulusReflex 36 https://my-reflex.app Browser events ➝ reflex class & action CableReady operation Reflex class DOM element Document

Slide 49

Slide 49 text

palkan_tula palkan StimulusReflex 36 https://my-reflex.app Browser events ➝ reflex class & action CableReady operation Action Cable broadcast from anywhere Reflex class DOM element Document

Slide 50

Slide 50 text

palkan_tula palkan StimulusReflex 36 https://my-reflex.app Browser events ➝ reflex class & action CableReady operation Action Cable broadcast from anywhere Reflex class DOM element Document

Slide 51

Slide 51 text

palkan_tula palkan Example 37

Slide 52

Slide 52 text

palkan_tula palkan Example 37

Slide 53

Slide 53 text

palkan_tula palkan Example 38
data-reflex="change ->List#toggle_item_completion" data-item-id="<%= item.id %> > ...

<%= item.desc %>

...

Slide 54

Slide 54 text

palkan_tula palkan Example 39 class ListReflex < ApplicationReflex def toggle_item_completion item = find_item item.toggle!(:completed) html = render_partial("items/item", {item}) selector = dom_id(item) cable_ready[ ListChannel.broadcasting_for(item.list) ].outer_html(selector:, html:) cable_ready.broadcast morph_flash :notice, "Item has been updated" end private def find_item Item.find element.dataset["item-id"] end end

Slide 55

Slide 55 text

palkan_tula palkan Example 40 class ListReflex < ApplicationReflex def toggle_item_completion item = find_item item.toggle!(:completed) html = render_partial("items/item", {item}) selector = dom_id(item) cable_ready[ ListChannel.broadcasting_for(item.list) ].outer_html(selector:, html:) cable_ready.broadcast morph_flash :notice, "Item has been updated" end private def find_item Item.find element.dataset["item-id"] end end Broadcast DOM updates to all connected clients Show flash-notification to the current user Object representing the current element data attributes

Slide 56

Slide 56 text

palkan_tula palkan Example 41 class ApplicationReflex < StimulusReflex ::Reflex private def morph_flash(type, message) morph "#flash", render_partial( "shared/alerts", {flash: {type => message}} ) end end

Slide 57

Slide 57 text

palkan_tula palkan StimulusReflex Introduces a new abstraction layer (reflexes) Unscoped DOM updates (you can even update the whole page) DOM manipulations could be customized to infinity 42

Slide 58

Slide 58 text

palkan_tula palkan StimulusReflex Stable & Mature (v3.4) Comprehensive documentation Active Discord community (>1k members) Works with AnyCable out-of-the-box 😉 43

Slide 59

Slide 59 text

palkan_tula palkan StimulusReflex v4.0 Transport-agnostic (cables, SSE, message_bus, AJAX) Reliable data flow 44

Slide 60

Slide 60 text

palkan_tula palkan NEW MAGIC hotwire.dev 45

Slide 61

Slide 61 text

palkan_tula palkan NEW MAGIC hotwire.dev 45

Slide 62

Slide 62 text

palkan_tula palkan evilmartians.com/blog evilmartians.com/chronicles/hotwire-reactive-rails-with-no-javascript 46

Slide 63

Slide 63 text

palkan_tula palkan Hotwire Demystified @jamie_gaskings at RailsConf 2021 47

Slide 64

Slide 64 text

palkan_tula palkan Turbo Drive Poor man's SPA Intercepts navigation / forms submission, performs AJAX requests, replaces HTML body contents Keeps track of visited pages (cache) to provide smooth experience 48 ex-Turbolinks

Slide 65

Slide 65 text

palkan_tula palkan Turbo Frames Turbolinks for page fragments (frames) Lazy loading of page parts (plays well with HTTP cache) 49

Slide 66

Slide 66 text

palkan_tula palkan Example 50 # app/controllers/items_controller.rb class ItemsController < ApplicationController def update item.update!(item_params) render partial: "item", locals: {item} end def destroy item.destroy! render partial: "item", locals: {item} end end

Slide 67

Slide 67 text

palkan_tula palkan Example 51 <% unless item.destroyed? %>
"> <%= form_for item do |f| %> <%= f.check_box :completed, class: item.completed? ? "hidden checked" : "hidden", onchange: "this.form.requestSubmit();" %> <% end %>

<%= item.desc %>

<%= button_to item_path(item), method: :delete do %> ... <% end %>
<% end %>

Slide 68

Slide 68 text

palkan_tula palkan Turbo Streams Minimalistic CableReady (only 5 actions) Transport-agnostic Zero JavaScript (uses custom HTML elements to trigger updates) 52

Slide 69

Slide 69 text

palkan_tula palkan Turbo Streams 53 https://my-hot.app

Slide 70

Slide 70 text

palkan_tula palkan Turbo Streams 53 https://my-hot.app

Slide 71

Slide 71 text

palkan_tula palkan Turbo Streams 53 https://my-hot.app Action Cable subscribe

Slide 72

Slide 72 text

palkan_tula palkan Turbo Streams 53 https://my-hot.app GET/POST/... Rails controller Action Cable subscribe

Slide 73

Slide 73 text

palkan_tula palkan Turbo Streams 53 https://my-hot.app GET/POST/... HTML Rails controller Action Cable subscribe

Slide 74

Slide 74 text

palkan_tula palkan Turbo Streams 53 https://my-hot.app GET/POST/... HTML Action Cable broadcast from anywhere Rails controller Action Cable subscribe

Slide 75

Slide 75 text

palkan_tula palkan DOM update Turbo Streams 53 https://my-hot.app GET/POST/... HTML Action Cable broadcast from anywhere Rails controller Action Cable subscribe

Slide 76

Slide 76 text

palkan_tula palkan Turbo Streams 54
<%= turbo_stream_from workspace %>

<%= workspace.name %>


Slide 77

Slide 77 text

palkan_tula palkan Turbo Streams 55 # app/controllers/chat/messages_controller.rb class MessagesController < ApplicationController def create Turbo ::StreamsChannel.broadcast_append_to( workspace, target: ActionView ::RecordIdentifier.dom_id(workspace, :chat_messages), partial: "chats/message", locals: {message: params[:message], name: current_user.name} ) head :ok end end

Slide 78

Slide 78 text

palkan_tula palkan More HTML-over-WS Motion Live 56

Slide 79

Slide 79 text

palkan_tula palkan Motion 57 github.com/unabridged/motion

Slide 80

Slide 80 text

palkan_tula palkan Live 58 github.com/socketry/live

Slide 81

Slide 81 text

palkan_tula palkan Live 59 class ClickCounter < Live ::View def initialize(id, **data) super @data[:count] ||= 0 end def handle(event, details) @data[:count] = Integer(@data[:count]) + 1 replace! end def render(builder) builder.tag :button, onclick: forward do builder.text("Add an image. ( #{@data[:count]} images so far).") end builder.tag :div do Integer(@data[:count]).times do builder.tag :img, src: "https: //picsum.photos/200/300" end end end end

Slide 82

Slide 82 text

palkan_tula palkan Live 60 class ClickCounter < Live ::View def initialize(id, **data) super @data[:count] ||= 0 end def handle(event, details) @data[:count] = Integer(@data[:count]) + 1 replace! end def render(builder) builder.tag :button, onclick: forward do builder.text("Add an image. ( #{@data[:count]} images so far).") end builder.tag :div do Integer(@data[:count]).times do builder.tag :img, src: "https: //picsum.photos/200/300" end end end end

Slide 83

Slide 83 text

palkan_tula palkan Motion / Live Conceptually closer to LiveView (stateful components) Does keeping view components state scale in Ruby? 🤔 61

Slide 84

Slide 84 text

palkan_tula palkan HTML-over-WebSockets There are plenty of implementations already StimulusReflex and CableReady are rock solid! Hotwire is gaining popularity (and stability) Motion and Live are promising 62

Slide 85

Slide 85 text

palkan_tula palkan HTML-over-WebSockets Gives full-stack development a second chance Worth considering if: You don't want to hire the whole new team Your app is based on user-server interactions You want to vitalize a classic Rails app 63

Slide 86

Slide 86 text

THANKS! @palkan @palkan_tula evilmartians.com @evilmartians https://discord.gg/stimulus-reflex