Websockets and Ruby EventMachine1. WebSockets & Ruby EventMachine
HTML5 Web sockets
Polling
Long-polling
Web socket
websockets.org/about.html
Ismael Celis new bamboo
2. WebSockets & Ruby EventMachine
Use cases
• Chat (lame)
• Stocks (lame)
• Games
• Presence
• Collaboration
• Real-time notifications
Ismael Celis new bamboo
4. WebSockets & Ruby EventMachine
WebSockets handshake
Request
Response
Profit!
Ismael Celis new bamboo
5. WebSockets & Ruby EventMachine
Messages
"x00Hello Worldxff"
Ismael Celis new bamboo
7. WebSockets & Ruby EventMachine
(Ruby) WebSockets server
• Speak WebSockets (handshake)
• Keep connections alive
• Performant!
• Scalable
• Threads? Evented?
Ismael Celis new bamboo
8. WebSockets & Ruby EventMachine
EventMachine rubyeventmachine.com
Ismael Celis new bamboo
9. WebSockets & Ruby EventMachine
EM-WebSocket
github.com/igrigorik/em-websocket
Ismael Celis new bamboo
10. WebSockets & Ruby EventMachine
EM-WebSocket
github.com/igrigorik/em-websocket
Ismael Celis new bamboo
12. WebSockets & Ruby EventMachine
Pick your format
STOMP CONNECT
login: <username>
passcode: <passcode>
<message
XMPP from=”john@server.com/ruby”
to=”jane@server.com/ruby”>
<body>Hey Jane!</body>
</message>
Your own (JSON?)
Ismael Celis new bamboo
13. WebSockets & Ruby EventMachine
Custom JSON format
[
“user_connected”, Event name
{
“name” : “Ismael”,
“message” : “hello!”, Custom data
“total_users” : 10
}
]
Ismael Celis new bamboo
15. WebSockets & Ruby EventMachine
Javascript wrapper
[ “user_message”,
{
“name” : “Ismael”,
“message” : “hello!”
}
]
Ismael Celis new bamboo
16. WebSockets & Ruby EventMachine
Implementation gist.github.com/299789
Ismael Celis new bamboo
17. WebSockets & Ruby EventMachine
Implementation gist.github.com/299789
Ismael Celis new bamboo
18. WebSockets & Ruby EventMachine
Implementation gist.github.com/299789
WebSocket
Ismael Celis new bamboo
19. WebSockets & Ruby EventMachine
Implementation gist.github.com/299789
Ismael Celis new bamboo
Editor's Notes - WebSockets is a HTML5 standard for server-browser persistent connections
- Bidirectional
- Less overhead than polling and long-polling - callbacks - Browser sends &#x201C;Upgrade&#x201D; request.
- Server &#x201C;upgrades&#x201D; connection to persistent TCP socket.
- No more headers sent. - &#x201C;messages&#x201D; delimited by these hex characters (null and termination)
- incoming message triggers &#x201C;onmessage&#x201D;.
- &#x201C;send&#x201D; method wraps messages internally - listent
- send - Server requirements
- Threads: memory bloat - EventMachine: evented, non-blocking Network connections
- predefined callbacks: post_init, receive_data
- implement servers (HTTP, WebSockets, others) - EM-WebSocket extension by Ilya Grigorik
- Implements WS server, handshake
- WS API callbacks open, message, close - Send messages back to clients - store new connections
- send message to each connection - you can structure you messages however you want.
- pick the right protocol for your domain
- or design your own - custom JSON format: [event, custom_data]
- simple standard for client-side events - structured JSON messages allow neat JS client.
- Browser binds to custom events and manipulates DOM
- Treat server as any DOM element - Send form to sockets server
- Familar Javascript events-centric semantics
- Wrapper sends JSON event convention - Simplified. Trivial
- wraps WebSocket object
- callbacks chain
- JSON encoding - decoding - callback chain
- add one or more handlers to each event name - hook in WebSocket.onmessage()
- JSON-decode
- execute callbacks for each event name Trigger and send event to server
- JSON-encode message to format convention
- [event_name, data]