SlideShare a Scribd company logo
WebSocket for Web
Rubyists
@ryudoawaru at OEDO’05
はじめに
発表する機会をいただき
ありがとうございます
提 供提 供
五倍紅寶石五倍紅寶石
自己紹介
鄧 慕凡(テン ムファン)
a.k.a: 竜堂 終
両方どもある小説の登場人物
Github / Twitter: @ryudoawaru
http://ryudo.tw
台湾人であり
ルビーストRubeastであり
Rubyの肩書
RubyConf Taiwan
執行委員長
Rails Girls Taipei
主催人
仕事は
五倍の紅宝石
http://5xruby.tw/en
無限虫制師
Unlimited Bug WorksGenerator
弊社の業務
Ruby / Rails の 教育業者
コンサルタンシー / オフショア
海外の取引先は日本、シンガポールなどを持ちます
Rubyの広める
RailsGirls / RubyConf…etc
RWC初めの海外スポンサーになりま
した
ここから、英語で話す
Agenda
Introduce WebSocket / EM-Websocket.
Architectural Perception for Web
Rubyists.
WebSocket
RFC 6455
Websocket
W3C / HTML5 Standard
Standard Javascript API
Full-duplex
Pub-Sub between client and server
Search in
rubygems.org
WebSocket For Web Rubyists
Gem Category
EventMachine-Websocket
Faye-Websokcet
EM-Websocket
EM-Websocket
From 2009
Simple implementation
Easily handle c10k by 1 process
http://shokai.org/blog/archives/7149
Typical Server Side Code
EventMachine::WebSocket.start(:host =>
"0.0.0.0", :port => 8080) do |ws|
ws.onopen { ws.send "Hello Client!"}
ws.onmessage { |msg| ws.send "Pong: #{msg}" }
ws.onclose { puts "WebSocket closed" }
end
Typical Client Side Code
var ws = new WebSocket('ws://host/uri');
ws.onopen = function() {
show('websocket opened');
};
ws.onclose = function() {
show('websocket closed');
}
ws.onmessage = function(m) {
show('websocket message: ' + m.data);
};
ws.send('Hello Websocket!!');
How does it work
Client Server
Handshake Response
Websocket
Handshake Request
GET /chat HTTP 1.1
Host: server.host.com
Upgrade: Websocket
Connection: Upgrade
Origin: http://host.com
Sec-Websocket-Key: “WwV7thr/Uwrg3mA57risrQ=="
Sec-WebSocket-Version:"13"
Connection:”Upgrade"
Sec-WebSocket-Accept: F0VaFFGV/
JHx1hJWBlhuJAqdse8=
Upgrade:"websocket"
Events
onopen:When new connection is
established
onmessage:When new message comes
from another side
onclose:When connection is closed by
the other side
Methods of Handshake Object
send:Send message to client or server
side.
close:Close connection.
Message Flow
Server
Client
Client
Client
1. send msg
2. onmessage
3. Send to
all clients
4. onmessage
Basic EM-WebSocket Code Style
conns_in_channel = Set.new
EM::WebSocket.start(...) do |ws|
ws.onopen do |request|
conns_in_channel.add ws
end
ws.onmessage do |msg|
conns_in_channel.each do |conn|
EM.next_tick{conn.send(msg)}
end
end
ws.onclose do
conns_in_channel.delete ws
end
end
Store connection
Remove connection on quit
Set to store connection
Send msg to connections 1 by 1
WebSocket Protocol
is Simple.
But it’s never easy to
make a great product.
Architectural Issue
Application Stack Style
Authentication
Application Stack
Style
Standalone or in App?
Two Styles
Standalone
In-App
Standalone
Web Service
Stack
WebSocket
Service
Stack
Normal HTTP Request
WS Request
In App Style
Web Service
Logic
Normal HTTP
Logic
Normal HTTP Request
WS Request
Live with HTTP Service in the
Rack Stack
request.websocket?
Rack Stack
WebSocketOther
Rack
Stuffs
YES NO
Identify by Request Headers
# middlewares/chat_backend.rb
def call(env)
if Faye::WebSocket.websocket?(env)
# WebSockets logic goes here
ws.rack_response
else
@app.call(env)
end
end
https://devcenter.heroku.com/articles/
ruby-websockets
Identify by Request Headers
# config/routes.rb
Example::Application.routes.draw do
match "/websocket", :to => ActionCable.server,
via: [:get, :post]
end
https://github.com/rails/actioncable
Standalone or In
App?
Normal Web Services
Keeps Connection
for very short
period.
Process could be
terminated w/o
affecting any
client.
Client Server
open
close
open
close
WebSocket Service
Long-running
Connection
Stop process
means killing all
connections.
Server
Client
Client
Client
Concurrency Model
WebSocket Gems use one of following
concurrency models:
Reactor: EventMachine based Gems.
Thread: Tubesock.
Mixed: ActionCable
This may conflicts with model of your
normal Web Services.
Comparison of Connection
Features
Normal Web Works WebSocket
Connection Period short long
Concurrency Model
Depend on App
Server
Reactor or Thread
Process Long-
Running?
No Yes
Authenticate
Confirm identity in handshake
Authentication Issue
Client Server
Handshake Request
Handshake Response
Websocket
Confirm at this point!
Possible Methods to Confirm
Identity
Cookie
Headers
URL / Query Parameters
Identify by Cookie
Easy approach for web developers.
WebSocket may run at different host.
Some browsers don’t support cookie on
ws:// request.
Most mobile APP’s http client may not
support cookie/session by default.
Identify by Request Headers
Unable to add custom header in the
JavaScript WebSockets API.
Identify by URL
Client
GET http://host/chat
WebSocketWeb Service
Use ticket to handshake
Identity
confirmed
Generate URL: ws://host2/abc1234 as
“ticket”
Attention
Security issue:
The “ticket" should be removed from
database after connection established.
Should only be valid in a short time.
Showcase
Chatroom for Forum
iOS APPWEB
http://www.focus-sport.club.tw/
Serve all platforms by one stack.
Runs Permanently.
Handle more than 1k concurrent
connections very easily.
ご清聴ありがとうございました
Any Question?

More Related Content

PDF
Horizontally Scaling Node.js and WebSockets
PDF
Isomorphic JavaScript: #DevBeat Master Class
PDF
WebAssembly vs JavaScript: What is faster?
PDF
PDF
Avoiding Common Pitfalls in Ember.js
PDF
General Assembly Workshop: Advanced JavaScript
PDF
In Pursuit of the Holy Grail: Building Isomorphic JavaScript Apps
PDF
Building Isomorphic JavaScript Apps - NDC 2015
Horizontally Scaling Node.js and WebSockets
Isomorphic JavaScript: #DevBeat Master Class
WebAssembly vs JavaScript: What is faster?
Avoiding Common Pitfalls in Ember.js
General Assembly Workshop: Advanced JavaScript
In Pursuit of the Holy Grail: Building Isomorphic JavaScript Apps
Building Isomorphic JavaScript Apps - NDC 2015

What's hot (20)

PDF
Building Isomorphic Apps (JSConf.Asia 2014)
PDF
Isomorphic JavaScript with Nashorn
PPT
Web assembly overview by Mikhail Sorokovsky
PPT
Loading JavaScript: Even a caveman can do it
PDF
遇見 Ruby on Rails
PPTX
Automating Your Daily Tasks with Scripting - RubyConf 2015 Taiwan
PDF
Isomorphic React Applications: Performance And Scalability
PDF
JSConf US 2014: Building Isomorphic Apps
PDF
vert.x - life beyond jetty and apache
PDF
Great Tools Heavily Used In Japan, You Don't Know.
PDF
Ruby On Rails Basics
PDF
FITC - Here Be Dragons: Advanced JavaScript Debugging
PPT
RubyMotion #jbday
PDF
JavaScript MV* Framework - Making the Right Choice
PDF
RoR (Ruby on Rails)
PDF
React server side rendering performance
PPTX
PHP Indonesia - Nodejs Web Development
PDF
Writing Bullet-Proof Javascript: By Using CoffeeScript
PDF
Webアプリケーションとメモリ
PDF
Node.JS: Do you know the dependency of your dependencies dependency
Building Isomorphic Apps (JSConf.Asia 2014)
Isomorphic JavaScript with Nashorn
Web assembly overview by Mikhail Sorokovsky
Loading JavaScript: Even a caveman can do it
遇見 Ruby on Rails
Automating Your Daily Tasks with Scripting - RubyConf 2015 Taiwan
Isomorphic React Applications: Performance And Scalability
JSConf US 2014: Building Isomorphic Apps
vert.x - life beyond jetty and apache
Great Tools Heavily Used In Japan, You Don't Know.
Ruby On Rails Basics
FITC - Here Be Dragons: Advanced JavaScript Debugging
RubyMotion #jbday
JavaScript MV* Framework - Making the Right Choice
RoR (Ruby on Rails)
React server side rendering performance
PHP Indonesia - Nodejs Web Development
Writing Bullet-Proof Javascript: By Using CoffeeScript
Webアプリケーションとメモリ
Node.JS: Do you know the dependency of your dependencies dependency
Ad

Viewers also liked (20)

PDF
SQL 脳から見た Ruby
PDF
Ember コミュニティとわたし
PDF
Rubyで実はwritev(2) が使われているはなし
PDF
超絶技巧プログラミングと Ruby 3.0 (大江戸 Ruby 会議 05 コミッタ LT)
PDF
mruby で mackerel のプラグインを作るはなし
PDF
Exgettextの話
PDF
Oedo Ruby Conference 04: Ruby会議でSQLの話をするのは間違っているだろうか
PDF
Security Advisories Checker on Travis/Circle CI
PDF
BigQueryのちょっとした話 #phpblt
PDF
RailsエンジニアのためのSQLチューニング速習会
PDF
Composer並列化プラグイン #phpblt
PDF
Learning to forget continual prediction with lstm
PDF
パーフェクト"Elixir情報収集"
PDF
Developing the fastest HTTP/2 server
PDF
Cookpad TechConf 2016 - DWHに必要なこと
PDF
Tochigi07 cm
PDF
apachehereというPHPのBuiltin Serverっぽいやつをつくった
PPTX
Re: WebServer BenchMarking
PPTX
PSR-1 と PSR-2 を 5分でざっくり理解する
PPTX
Php blt-vol2
SQL 脳から見た Ruby
Ember コミュニティとわたし
Rubyで実はwritev(2) が使われているはなし
超絶技巧プログラミングと Ruby 3.0 (大江戸 Ruby 会議 05 コミッタ LT)
mruby で mackerel のプラグインを作るはなし
Exgettextの話
Oedo Ruby Conference 04: Ruby会議でSQLの話をするのは間違っているだろうか
Security Advisories Checker on Travis/Circle CI
BigQueryのちょっとした話 #phpblt
RailsエンジニアのためのSQLチューニング速習会
Composer並列化プラグイン #phpblt
Learning to forget continual prediction with lstm
パーフェクト"Elixir情報収集"
Developing the fastest HTTP/2 server
Cookpad TechConf 2016 - DWHに必要なこと
Tochigi07 cm
apachehereというPHPのBuiltin Serverっぽいやつをつくった
Re: WebServer BenchMarking
PSR-1 と PSR-2 を 5分でざっくり理解する
Php blt-vol2
Ad

Similar to WebSocket For Web Rubyists (20)

PDF
Timeless - Websocket on Rails
PPTX
Pune Ruby Meetup - November 2015
PDF
Websockets, Ruby y Pusher Webprendedor 2010
PDF
Websockets en Ruby en 5 Minutos
PDF
Websocket on Rails
PPTX
WebSockets in JEE 7
KEY
Websockets with ruby
PDF
Let's Get Real (time): Server-Sent Events, WebSockets and WebRTC for the soul
PPT
ruby + websocket + haproxy
PDF
Websockets talk at Rubyconf Uruguay 2010
PDF
WebSocket Push Fallback - Transcript.pdf
PDF
Real Time Web - What's that for?
PPTX
ClientServer Websocket.pptx
PDF
Building Next Generation Real-Time Web Applications using Websockets
PDF
Web Clients for Ruby and What they should be in the future
PPTX
WebSocket protocol
PDF
WebSockets with Spring 4
PDF
Asynchronous Ruby
PDF
Anchoring Trust: Rewriting DNS for the Semantic Network with Ruby and Rails
PPTX
Messaging for Real-time WebApps
Timeless - Websocket on Rails
Pune Ruby Meetup - November 2015
Websockets, Ruby y Pusher Webprendedor 2010
Websockets en Ruby en 5 Minutos
Websocket on Rails
WebSockets in JEE 7
Websockets with ruby
Let's Get Real (time): Server-Sent Events, WebSockets and WebRTC for the soul
ruby + websocket + haproxy
Websockets talk at Rubyconf Uruguay 2010
WebSocket Push Fallback - Transcript.pdf
Real Time Web - What's that for?
ClientServer Websocket.pptx
Building Next Generation Real-Time Web Applications using Websockets
Web Clients for Ruby and What they should be in the future
WebSocket protocol
WebSockets with Spring 4
Asynchronous Ruby
Anchoring Trust: Rewriting DNS for the Semantic Network with Ruby and Rails
Messaging for Real-time WebApps

More from Mu-Fan Teng (12)

PDF
My experience of Ruby Education in Taiwan
PDF
20150118 學個 Sinatra 好過年
PDF
Mopcon2014 - 使用 Sinatra 結合 Ruby on Rails 輕鬆打造完整 Full Stack 網站加 API Service服務
PDF
實踐大學教案20140329
PDF
Rails Girls Taiwan 2014 Intro
PDF
Eventmachine Websocket 實戰
PDF
Introduce Ruby Taiwan@Rubykaigi2013
PDF
Webconf2013-非典型貧窮網站維運經驗分享
PDF
Concurrency model for mysql data processing@rubyconf.tw 2012
PDF
Sinatra Tutorial@Rubyconf.TW2011
ODP
Ruby程式語言入門導覽
ODP
Ruby on discuz
My experience of Ruby Education in Taiwan
20150118 學個 Sinatra 好過年
Mopcon2014 - 使用 Sinatra 結合 Ruby on Rails 輕鬆打造完整 Full Stack 網站加 API Service服務
實踐大學教案20140329
Rails Girls Taiwan 2014 Intro
Eventmachine Websocket 實戰
Introduce Ruby Taiwan@Rubykaigi2013
Webconf2013-非典型貧窮網站維運經驗分享
Concurrency model for mysql data processing@rubyconf.tw 2012
Sinatra Tutorial@Rubyconf.TW2011
Ruby程式語言入門導覽
Ruby on discuz

Recently uploaded (20)

PDF
Machine learning based COVID-19 study performance prediction
PDF
Empathic Computing: Creating Shared Understanding
PPTX
20250228 LYD VKU AI Blended-Learning.pptx
PPTX
Cloud computing and distributed systems.
PDF
Network Security Unit 5.pdf for BCA BBA.
PDF
Electronic commerce courselecture one. Pdf
PPTX
Digital-Transformation-Roadmap-for-Companies.pptx
PDF
The Rise and Fall of 3GPP – Time for a Sabbatical?
PPTX
Detection-First SIEM: Rule Types, Dashboards, and Threat-Informed Strategy
PDF
Encapsulation theory and applications.pdf
PDF
Optimiser vos workloads AI/ML sur Amazon EC2 et AWS Graviton
PDF
Advanced methodologies resolving dimensionality complications for autism neur...
PPTX
Spectroscopy.pptx food analysis technology
PDF
Blue Purple Modern Animated Computer Science Presentation.pdf.pdf
PPT
“AI and Expert System Decision Support & Business Intelligence Systems”
PPTX
Understanding_Digital_Forensics_Presentation.pptx
PDF
Dropbox Q2 2025 Financial Results & Investor Presentation
PDF
KodekX | Application Modernization Development
PDF
Reach Out and Touch Someone: Haptics and Empathic Computing
PDF
7 ChatGPT Prompts to Help You Define Your Ideal Customer Profile.pdf
Machine learning based COVID-19 study performance prediction
Empathic Computing: Creating Shared Understanding
20250228 LYD VKU AI Blended-Learning.pptx
Cloud computing and distributed systems.
Network Security Unit 5.pdf for BCA BBA.
Electronic commerce courselecture one. Pdf
Digital-Transformation-Roadmap-for-Companies.pptx
The Rise and Fall of 3GPP – Time for a Sabbatical?
Detection-First SIEM: Rule Types, Dashboards, and Threat-Informed Strategy
Encapsulation theory and applications.pdf
Optimiser vos workloads AI/ML sur Amazon EC2 et AWS Graviton
Advanced methodologies resolving dimensionality complications for autism neur...
Spectroscopy.pptx food analysis technology
Blue Purple Modern Animated Computer Science Presentation.pdf.pdf
“AI and Expert System Decision Support & Business Intelligence Systems”
Understanding_Digital_Forensics_Presentation.pptx
Dropbox Q2 2025 Financial Results & Investor Presentation
KodekX | Application Modernization Development
Reach Out and Touch Someone: Haptics and Empathic Computing
7 ChatGPT Prompts to Help You Define Your Ideal Customer Profile.pdf

WebSocket For Web Rubyists