Real-time Web Application with
Socket.IO, Node.js, and Redis
York Tsai, JSDC 2013
Slide 2
Slide 2 text
Who is York
●
http://www.linkedin.com/pub/york-tsai/13/bb/8a8
●
https://twitter.com/yorktsai
●
https://www.facebook.com/yorktsai
●
VP Engineering @ EZTABLE
Slide 3
Slide 3 text
Never Live Demo!
●
http://jsdc.york.tw/
●
I hope this work on my (and yours) computer ...
Slide 4
Slide 4 text
No content
Slide 5
Slide 5 text
You may Have Heard About
●
Forever Iframe
●
XMLHttpRequest Long Polling
●
Cometd
●
Websocket
●
http://en.wikipedia.org/wiki/Comet_(programming
)
●
TL;DR
Slide 6
Slide 6 text
Socket.IO
●
Cross browser (including IE 6)
●
Real-time and bi-directional persistent
connection (WebSocket)
●
Very simple to use
●
Javascript!
Slide 7
Slide 7 text
Start a Server
Slide 8
Slide 8 text
Sending and
Receiving Events
Slide 9
Slide 9 text
No content
Slide 10
Slide 10 text
Select Receivers for
Events
Slide 11
Slide 11 text
Broadcasting
Slide 12
Slide 12 text
Rooms
Slide 13
Slide 13 text
Single Target
Slide 14
Slide 14 text
Multiple Applications?
Slide 15
Slide 15 text
Namespacing
Slide 16
Slide 16 text
How About New
Connections?
Slide 17
Slide 17 text
Data Persistence
●
Need a persistence layer
●
Get data from the persistence layer whenever a
new connection established
Slide 18
Slide 18 text
Integration
I have component X,Y,Z want to send/receive data
from user's browser
Slide 19
Slide 19 text
Pub/Sub or Message
Queue
Messaging system usually supports both
Slide 20
Slide 20 text
Pub/Sub
Digram from Python's Website
Slide 21
Slide 21 text
Redis
●
Super-fast in-memory data structure server
●
Pub/Sub
●
Rich client libraries
●
Zero install and easy to operate
In addition...
●
Connect multiple node.js server to one redis
channel
●
Scalability!
Slide 24
Slide 24 text
Two Connections per Server
Slide 25
Slide 25 text
Redis-backed Pub/Sub
●
Two clients required: one for pub, one for sub
●
Use one channel, add metadata to your
message
Slide 26
Slide 26 text
Redis-backed Pub/Sub
Slide 27
Slide 27 text
Frequently Asked
Questions
Slide 28
Slide 28 text
SSL?
Yes!
Slide 29
Slide 29 text
SSL Server
Slide 30
Slide 30 text
SSL Client
Slide 31
Slide 31 text
Cross domain?
Yes! (If use WebSocket or
JSONP)
Slide 32
Slide 32 text
Authentication?
Slide 33
Slide 33 text
Authentication
●
Cookie & Session
●
Cookie does not cross domain, use SSL +
Access Token instead
Slide 34
Slide 34 text
Load Balancing?
Yes! (If use Nginx >= 1.3.13)
Slide 35
Slide 35 text
Sample Nginx Configuration
Slide 36
Slide 36 text
Load Balancing using ELB
●
ELB (Elastic Load Balancer)'s HTTP(S) proxy
does not understand websocket requests
●
Use TCP/SSL, instead of HTTP/HTTPS
●
No session stickyness
●
No X-Forwarded-For header
Slide 37
Slide 37 text
Scalability
●
Single thread
●
2500~3500 connections per process
●
As many processes as you want
Slide 38
Slide 38 text
Configurations
●
store (default: MemoryStore, single process
only)
●
transports (default: websocket, htmlfile, xhr-
polling, jsonp-polling)
●
authorization (default: false)
Slide 39
Slide 39 text
Configurations - transports
Slide 40
Slide 40 text
Thank You!
The slide is also the live demo itself. Please check
the following repo!
https://github.com/yorktsai/jsdc2013