Upload
framgia-vietnam
View
363
Download
0
Tags:
Embed Size (px)
Citation preview
Ruby on Rails & WebSocket
Le Hoai PhuongNguyen Huy Hung Hoang Van QuanCao Duy Khanh Vu Van Chuyen
Timeless
● WebSocket Overview● Benefit● How does WS work?● Pros & Cons● WebSocket on Rails● Summary
Content
WebSocket Overview● Full-duplex channels over a single TCP
connection● Was standardized in 2011● Use an HTTP Upgrade request for
handshaking
Upgrade RequestGET /chat HTTP/1.1Host: server.example.comUpgrade: websocketConnection: UpgradeSec-WebSocket-Key: x3JJHMbDL1EzLkh9GBhXDw==Sec-WebSocket-Protocol: chat, superchatSec-WebSocket-Version: 13Origin: http://example.com
HTTP/1.1 101 Switching ProtocolsUpgrade: websocketConnection: UpgradeSec-WebSocket-Accept: HSmrc0sMlYUkAGmm5OPpG2HaGWk=Sec-WebSocket-Protocol: chat
Client Request
Server Response
WebSocket Benefit● Overhead of HTTP
○ 1 HTTP request/response Header: 871 bytes○ 1 WS frame: 2 bytes
● Low Latency
How does it work?
Pros & Cons● Pros
○ Realtime○ Lightweight○ Low latency○ Full duplex
● Cons○ Still in working progress○ Not suitable for streaming large data (HD video)○ Hard to control multi-channels
WebSocket on Rails● Web server
○ Event Based○ or Client WS simulation using JavaScript
● Many gems: websocket-rails, em-websocket, evma_httpserver
Reactor pattern
RequestEventMachine(demultiplexer - dispatcher)
Thin(Web Server)
Rack Rails adapter
Rack Thin handlerRails
A demo coding with gem websocket-rails
Summary● Realtime● New● Lightweight● Full duplex