This document introduces WebSocket and how it can be used for real-time communication between a client and server. It discusses:
1. The basics of WebSocket including how the handshake process works and how events and messages are exchanged.
2. Two approaches for integrating WebSocket into a Ruby/Rails application - as a standalone service or within the application stack. Using it standalone allows the service to run independently while integrating it brings challenges in handling long-running connections.
3. Key considerations for WebSocket including authentication, concurrency models, and whether the server process needs to run permanently to maintain open connections.
29. 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"
30. Events
onopen:When new connection is
established
onmessage:When new message comes
from another side
onclose:When connection is closed by
the other side
31. Methods of Handshake Object
send:Send message to client or server
side.
close:Close connection.
33. 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
40. In App Style
Web Service
Logic
Normal HTTP
Logic
Normal HTTP Request
WS Request
41. Live with HTTP Service in the
Rack Stack
request.websocket?
Rack Stack
WebSocketOther
Rack
Stuffs
YES NO
42. 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
43. 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
45. Normal Web Services
Keeps Connection
for very short
period.
Process could be
terminated w/o
affecting any
client.
Client Server
open
close
open
close
47. 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.
48. 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
52. 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.
53. Identify by Request Headers
Unable to add custom header in the
JavaScript WebSockets API.
54. Identify by URL
Client
GET http://host/chat
WebSocketWeb Service
Use ticket to handshake
Identity
confirmed
Generate URL: ws://host2/abc1234 as
“ticket”