real-time web application with socket.io, node.js, and redis

40
Real-time Web Application with Socket.IO, Node.js, and Redis York Tsai, JSDC 2013

Upload: york-tsai

Post on 17-May-2015

18.503 views

Category:

Technology


5 download

DESCRIPTION

JSDC 2013

TRANSCRIPT

Page 1: Real-time Web Application with Socket.IO, Node.js, and Redis

Real-time Web Application with Socket.IO, Node.js, and Redis

York Tsai, JSDC 2013

Page 2: Real-time Web Application with Socket.IO, Node.js, and Redis

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

Page 3: Real-time Web Application with Socket.IO, Node.js, and Redis

Never Live Demo!

● http://jsdc.york.tw/● I hope this work on my (and yours) computer ...

Page 4: Real-time Web Application with Socket.IO, Node.js, and Redis
Page 5: Real-time Web Application with Socket.IO, Node.js, and Redis

You may Have Heard About

● Forever Iframe● XMLHttpRequest Long Polling● Cometd● Websocket● http://en.wikipedia.org/wiki/Comet_(programming

)● TL;DR

Page 6: Real-time Web Application with Socket.IO, Node.js, and Redis

Socket.IO

● Cross browser (including IE 6)● Real-time and bi-directional persistent

connection (WebSocket)● Very simple to use● Javascript!

Page 7: Real-time Web Application with Socket.IO, Node.js, and Redis

Start a Server

Page 8: Real-time Web Application with Socket.IO, Node.js, and Redis

Sending and Receiving Events

Page 9: Real-time Web Application with Socket.IO, Node.js, and Redis
Page 10: Real-time Web Application with Socket.IO, Node.js, and Redis

Select Receivers for Events

Page 11: Real-time Web Application with Socket.IO, Node.js, and Redis

Broadcasting

Page 12: Real-time Web Application with Socket.IO, Node.js, and Redis

Rooms

Page 13: Real-time Web Application with Socket.IO, Node.js, and Redis

Single Target

Page 14: Real-time Web Application with Socket.IO, Node.js, and Redis

Multiple Applications?

Page 15: Real-time Web Application with Socket.IO, Node.js, and Redis

Namespacing

Page 16: Real-time Web Application with Socket.IO, Node.js, and Redis

How About New Connections?

Page 17: Real-time Web Application with Socket.IO, Node.js, and Redis

Data Persistence

● Need a persistence layer● Get data from the persistence layer whenever a

new connection established

Page 18: Real-time Web Application with Socket.IO, Node.js, and Redis

IntegrationI have component X,Y,Z want to send/receive data

from user's browser

Page 19: Real-time Web Application with Socket.IO, Node.js, and Redis

Pub/Sub or Message Queue

Messaging system usually supports both

Page 20: Real-time Web Application with Socket.IO, Node.js, and Redis

Pub/Sub

Digram from Python's Website

Page 21: Real-time Web Application with Socket.IO, Node.js, and Redis

Redis

● Super-fast in-memory data structure server● Pub/Sub● Rich client libraries● Zero install and easy to operate

Page 22: Real-time Web Application with Socket.IO, Node.js, and Redis

3 Commands

● SUBSCRIBE channel● UNSUBSCRIBE channel● PUBLISH channel message

Page 23: Real-time Web Application with Socket.IO, Node.js, and Redis

In addition...

● Connect multiple node.js server to one redis channel

● Scalability!

Page 24: Real-time Web Application with Socket.IO, Node.js, and Redis

Two Connections per Server

Page 25: Real-time Web Application with Socket.IO, Node.js, and Redis

Redis-backed Pub/Sub

● Two clients required: one for pub, one for sub● Use one channel, add metadata to your

message

Page 26: Real-time Web Application with Socket.IO, Node.js, and Redis

Redis-backed Pub/Sub

Page 27: Real-time Web Application with Socket.IO, Node.js, and Redis

Frequently Asked Questions

Page 28: Real-time Web Application with Socket.IO, Node.js, and Redis

SSL?

Yes!

Page 29: Real-time Web Application with Socket.IO, Node.js, and Redis

SSL Server

Page 30: Real-time Web Application with Socket.IO, Node.js, and Redis

SSL Client

Page 31: Real-time Web Application with Socket.IO, Node.js, and Redis

Cross domain?

Yes! (If use WebSocket or JSONP)

Page 32: Real-time Web Application with Socket.IO, Node.js, and Redis

Authentication?

Page 33: Real-time Web Application with Socket.IO, Node.js, and Redis

Authentication

● Cookie & Session● Cookie does not cross domain, use SSL +

Access Token instead

Page 34: Real-time Web Application with Socket.IO, Node.js, and Redis

Load Balancing?

Yes! (If use Nginx >= 1.3.13)

Page 35: Real-time Web Application with Socket.IO, Node.js, and Redis

Sample Nginx Configuration

Page 36: Real-time Web Application with Socket.IO, Node.js, and Redis

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

Page 37: Real-time Web Application with Socket.IO, Node.js, and Redis

Scalability

● Single thread● 2500~3500 connections per process● As many processes as you want

Page 38: Real-time Web Application with Socket.IO, Node.js, and Redis

Configurations

● store (default: MemoryStore, single process only)

● transports (default: websocket, htmlfile, xhr-polling, jsonp-polling)

● authorization (default: false)

Page 39: Real-time Web Application with Socket.IO, Node.js, and Redis

Configurations - transports

Page 40: Real-time Web Application with Socket.IO, Node.js, and Redis

Thank You!The slide is also the live demo itself. Please check

the following repo!

https://github.com/yorktsai/jsdc2013