Slide 1

Slide 1 text

PubSub in Rails @just_lest

Slide 2

Slide 2 text

Real-time features

Slide 3

Slide 3 text

No content

Slide 4

Slide 4 text

Real-time features → Collaboration → Asynchronous tasks → Keeping UI in sync

Slide 5

Slide 5 text

Action Cable Action Cable seamlessly integrates WebSockets with the rest of your Rails application. http://guides.rubyonrails.org/ action_cable_overview.html

Slide 6

Slide 6 text

Besides WebSocket protocol → Polling → Long-polling → EventSource

Slide 7

Slide 7 text

WebSocket protocol → Full-duplex → Low overhead

Slide 8

Slide 8 text

WebSocket protocol var socket = new WebSocket('ws://www.example.com/socketserver') socket.onopen = function () { websocket.send('Hello') } socket.onmessage = function (msg) { console.log(msg) }

Slide 9

Slide 9 text

Action Cable ApplicationCable::Connection is a WebSocket connection ApplicationCable::Channel is a unit of work inside a connection

Slide 10

Slide 10 text

Action Cable Action Cable uses Pub/Sub to communicate between the server and many clients. http://guides.rubyonrails.org/ action_cable_overview.html

Slide 11

Slide 11 text

What is PubSub?

Slide 12

Slide 12 text

What is PubSub? → Publishers → Subscribers

Slide 13

Slide 13 text

No content

Slide 14

Slide 14 text

Message reliability

Slide 15

Slide 15 text

Losing messages → Flaky internet connection → Swithing Wi-Fi networks → Closing your laptop's lid

Slide 16

Slide 16 text

Message reliability Messaging reliability is far more important than WebSockets. WebSockets are an implementation detail, not a feature. Sam Saffron https://samsaffron.com/archive/2015/12/29/ websockets-caution-required

Slide 17

Slide 17 text

Message reliability Twitter use HTTP/2 + polling, Facebook and Gmail use Long Polling. Sam Saffron https://samsaffron.com/archive/2015/12/29/ websockets-caution-required

Slide 18

Slide 18 text

HTTP/2

Slide 19

Slide 19 text

HTTP/2 There is no websockets for HTTP/2. By this, I mean that there’s no way to negotiate or upgrade a connection to websockets over HTTP/2 like there is for HTTP/1.1 as expressed by RFC 6455. Daniel Stenberg https://daniel.haxx.se/blog/2016/06/15/no- websockets-over-http2/

Slide 20

Slide 20 text

HTTP/2 Open 20 tabs with a WebSocket based application and you are risking 20 connections unless the client/server mitigates. Sam Saffron https://samsaffron.com/archive/2015/12/29/ websockets-caution-required

Slide 21

Slide 21 text

HTTP/2 HTTP comes with a lot of goodies, and by moving away from HTTP we'll lose it all. Caching, routing, multiplexing, gzipping and lot more. You could reimplement all of these things in Action Cable, but why? Nate Berkopec https://www.nateberkopec.com/2015/09/30/action- cable.html

Slide 22

Slide 22 text

Possible issues → Proxies when using unsecured HTTP → Load balancing

Slide 23

Slide 23 text

What to do then?

Slide 24

Slide 24 text

message_bus A reliable, robust messaging bus for Ruby processes and web clients. https://github.com/SamSaffron/message_bus

Slide 25

Slide 25 text

message_bus https://www.discourse.org

Slide 26

Slide 26 text

message_bus → Provides reliable queue → Supports both server-to-client and server-to- server communication

Slide 27

Slide 27 text

message_bus → Long-polling with chunked encoding (streaming) → Long-polling → Polling

Slide 28

Slide 28 text

But when to use WebSockets? → To utilize full-duplex connection → To minimize latency and overhead

Slide 29

Slide 29 text

Thank you!