building realtimewebslide

54
Makoto Inoue (@makoto_inoue) Building Real Time Web リアルタイムウェブができるまで Friday, 27 August 2010

Upload: inouemak

Post on 07-Nov-2014

3.683 views

Category:

Technology


0 download

DESCRIPTION

The real time web is not about adding chat on your website. It's not really about having stock tickers. The most interesting aspect of the realtime web is to be found in augmenting the web that we already know. We can add realtime functionally to our applications as another layer of fidelity. This is our story (New Bamboo, London, UK) of experimenting various real time web technologies and coming up with our own solutions (http://www.pusherapp.com). This is not about explaining how to use our solutions, but about sharing exciting opportunities and new technical challenges HTML5 WebSocket brings when you build real time web.

TRANSCRIPT

Page 1: Building realtimewebslide

Makoto Inoue (@makoto_inoue)

Building Real Time Webリアルタイムウェブができるまで

Friday, 27 August 2010

Page 2: Building realtimewebslide

About - 自己紹介 -

http://truestoryapp.com/ http://pandastream.com/ http://pusherapp.com/

Friday, 27 August 2010

Page 3: Building realtimewebslide

HTML5 WebSocket

Cloud Service

Powered by Ruby

Pusher [púsh・er]【名】【C】

Friday, 27 August 2010

Page 4: Building realtimewebslide

押す人.(A person who pushes stuff)

押すもの[道具].(A tool to push)

《俗》 麻薬密売人.(A drug dealer)

Pusher [púsh・er]【名】【C】

http://ejje.weblio.jp/content/pusher

Friday, 27 August 2010

Page 5: Building realtimewebslide

A:@pusherapp のWebSocket試してみました (I tried Pusher’s WebSocket)

B:麻薬買いました (I bought cocaine)

“I got hooked by Pusher”

Friday, 27 August 2010

Page 6: Building realtimewebslide

“I got hooked by Pusher”

Friday, 27 August 2010

Page 7: Building realtimewebslide

node.js & WebSocket (node.js と WebSocket)

The Ruby’s Counter Attack (逆襲のRuby)

The Birth of Pusher (Pusher誕生)

Growing with Pusher (Pusherを通して学んだ事)

Today’s Topic今日のお題

Friday, 27 August 2010

Page 8: Building realtimewebslide

      & evented worldnode.jsとイベント駆動な世界(Nov 2009)

http://www.slideshare.net/simon/evented-io-based-web-servers-explained-using-bunnies

rows = database.fetch(category = 'news')template = read_file('homepage.html')json = fetc_url('http://.../')

database.fetch(category = 'news', callback)read_file('homepage.html', callback)fetc_url('http://.../', callback)

Blocking - Synchronous

Non Blocking - Asynchronous

Friday, 27 August 2010

Page 9: Building realtimewebslide

http://blog.chromium.org/2009/12/web-sockets-now-available-in-google.html

Web Socket In Google Chrome Google のWebSocket サポート(Dec 2009)

Friday, 27 August 2010

Page 10: Building realtimewebslide

WebSocket Protocol

1. Handshake Request 2. Handshake Response

3. Frame Transportation

Benefits1. Less overhead1. Not releasing connection = good to share state within a session

Friday, 27 August 2010

Page 11: Building realtimewebslide

Benefit (利点)

Less Overhead (少ないオーバーヘッド)

Easy to manage state per session(クッキーいらず)

Server Push (サーバからクライアントへのデータ通知)

Friday, 27 August 2010

Page 12: Building realtimewebslide

Ajax(Polling)

Comet

WebSocket

Ajax vs Comet vs WebSocket

(Long Polling)

Friday, 27 August 2010

Page 13: Building realtimewebslide

Web Sockets Client Side Example クライアントサイドの例

http://blog.chromium.org/2009/12/web-sockets-now-available-in-google.html

var ws = new WebSocket("ws://example.com/service"); ws.onopen = function() { ws.send("message to send"); }; ws.onmessage = function (evt) { var received_msg = evt.data; }; ws.onclose = function() {};

Friday, 27 August 2010

Page 14: Building realtimewebslide

Web Sockets Server Side Example サーバサイドの例(node-websocket-server)

http://github.com/miksago/node-websocket-server/blob/master/examples/echo-server.js

var httpServer = http.createServer(serveFile);var server = ws.createServer({}, httpServer);

server.addListener("connection", function(conn){ conn.send(conn.id, "Connected as: "+conn.id);

conn.addListener("message", function(message){ conn.broadcast(message); });});

server.addListener("close", function(conn){ conn.broadcast("<"+conn.id+"> disconnected");});

server.listen(8000);

node-websocket-server = Micheil Smith

Friday, 27 August 2010

Page 15: Building realtimewebslide

TPS(Tweets Per Second)Example秒速ツイート例

http://blog.new-bamboo.co.uk/2009/12/14/pushing-the-boundary-of-real-time-web-with-twitter-and-xfactor

Friday, 27 August 2010

Page 16: Building realtimewebslide

Current State現在の状況

Chrome,Safari, Firefox(Beta)

Different Draft (75, 76)

Flash fallback for non supported browsers

http://github.com/gimite/web-socket-js

Not for iPhone/iPad ;-(

No Binary(yet)

By Hiroshi Ichikawa

Remy Sharp just made iPhone PhoneGap version for 75(remy)

Friday, 27 August 2010

Page 17: Building realtimewebslide

モビルスーツの性能の違いが、戦力の決定的差でないということを教えてやる!!(ガンダムとの2回戦、シャアの自信に溢れたセリフ。)

http://khaos-gate.blog.so-net.ne.jp/2006-07-20

The Ruby’s Counter Attack逆襲のRuby (Dec 2009)

Matz and Ryan Dahli

Friday, 27 August 2010

Page 18: Building realtimewebslide

Rubyをキメると気持ちイイ。説明できていない!!(RubyKaigi2008)

The Ruby’s Counter Attack逆襲のRuby (Dec 2009)

Matz and Ryan Dahli

Friday, 27 August 2010

Page 19: Building realtimewebslide

Event Machine + WebSocket= em-websocket

http://www.igvita.com/2009/12/22/ruby-websockets-tcp-for-the-browser/

require 'em-websocket' 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

em-websocket = Illya Grigorik

Friday, 27 August 2010

Page 20: Building realtimewebslide

Other Ruby related librariesその他の赤い彗星達

http://github.com/frsyuki/rev-websocket

http://github.com/lifo/cramp

http://rainbows.rubyforge.org/sunshowers/Sunshowers/WebSocket.html

http://github.com/mcolyer/em-websocket-proxy

http://github.com/gimite/web-socket-ruby

rev-websocket = sadayuki furuhashicramp = Pratik Naikrainbows - sunshowers = Eric Wongem-websocket-proxy = Matt Colyerweb-socket-ruby = Hiroshi Ichikawa

Friday, 27 August 2010

Page 21: Building realtimewebslide

How to pick the right one?どれを選べばいいの?

Up to date with the latest draft? (最新ドラフトサポート)

Library or framework? (ライブラリかフレームワークか)

Performance (パフォーマンス)

(Alex MacCaw’s fork has 76 support for Cramp)

Friday, 27 August 2010

Page 22: Building realtimewebslide

Performance

http://github.com/makoto/wsbench

http://bit.ly/9tKdeK

This is example of using 2 small size ec2 instance, making 5000 ~ 20000 number of connections , send 1 message to distribute to all connections, and majored the average of how long it took to be delivered to each connection (DRAFT 75 ONLY)

Friday, 27 August 2010

Page 23: Building realtimewebslide

The Birth of Pusher(Pusher 誕生)

Friday, 27 August 2010

Page 24: Building realtimewebslide

TrueStory

Friday, 27 August 2010

Page 25: Building realtimewebslide

Problem 問題

Data gets stale (データが古くなる)

Conflicts (更新データの衝突)

Discourages collaboration (コラボレーションの障害)

Friday, 27 August 2010

Page 26: Building realtimewebslide

Requirement要求

No rewrite (一からの書き直しは避けたい)

Reusable (再利用可能)

Simple (シンプル)

Friday, 27 August 2010

Page 27: Building realtimewebslide

Our solution解決方法

http://eiga.com/movie/44107/goods/t142030853/

Friday, 27 August 2010

Page 28: Building realtimewebslide

Separate

Friday, 27 August 2010

Page 29: Building realtimewebslide

Serve file

Friday, 27 August 2010

Page 30: Building realtimewebslide

Bind

Friday, 27 August 2010

Page 31: Building realtimewebslide

AJAX

Friday, 27 August 2010

Page 32: Building realtimewebslide

REST

IMPORTANT : You can synchronise data from client to server by saving before hitting Pusher

Friday, 27 August 2010

Page 33: Building realtimewebslide

Push

Friday, 27 August 2010

Page 34: Building realtimewebslide

Friday, 27 August 2010

Page 35: Building realtimewebslide

Retrospectiveapp

http://screenr.com/NJ0

Friday, 27 August 2010

Page 36: Building realtimewebslide

Retrospectiveapp

Friday, 27 August 2010

Page 37: Building realtimewebslide

Channel & Event

server = new Pusher(Pusher.key, Pusher.channel);

server.bind('note-create', function(note) { generateNote(note);});

post '/notes.json' do content_type 'text/json', :charset => 'utf-8' n = Note.new(params[:note]) n.save Pusher[CHANNEL].trigger('note-create', n.to_json)end

Server side (Ruby)

Client side (Javascript)

We enhanced WebSocket by adding channel and event

Friday, 27 August 2010

Page 38: Building realtimewebslide

Growing With PusherPusher を通して学んだ事

http://www.flickr.com/photos/amy_ng/1692424006/sizes/m/

Friday, 27 August 2010

Page 39: Building realtimewebslide

Growing With PusherPusher を通して学んだ事

Usage Pattern (サーバからクライアントへのデータ通知)

Making Robust & Scalable (スケーラブルにする)

Friday, 27 August 2010

Page 40: Building realtimewebslide

Usage Pattern

3 patterns

Introducing new pattern (新しいパターン)

Friday, 27 August 2010

Page 41: Building realtimewebslide

3 patterns

Friday, 27 August 2010

Page 42: Building realtimewebslide

1: Collaboration

Friday, 27 August 2010

Page 43: Building realtimewebslide

2: Streaming

Friday, 27 August 2010

Page 44: Building realtimewebslide

3:One Day Event

Friday, 27 August 2010

Page 45: Building realtimewebslide

Introducing new pattern: Presence

http://screenr.com/B5O

Friday, 27 August 2010

Page 46: Building realtimewebslide

Introducing new pattern: Presence

Friday, 27 August 2010

Page 47: Building realtimewebslide

Introducing new pattern: Presence

http://github.com/makoto/rubykaigi/tree/presence

class PusherController < ApplicationController def auth response = Pusher[params[:channel_name]].authenticate(params[:socket_id], { :user_id => current_user.id, :user_info => { :name => current_user.username } }) render :json => response else render :text => "Not authorized", :status => '403' end endend

Friday, 27 August 2010

Page 48: Building realtimewebslide

Introducing new pattern: Presence

http://github.com/makoto/rubykaigi/tree/presence

jQuery(function(){ var server = new Pusher('#{Pusher.key}'); var myPresenceChannel = server.subscribe('presence#{pusher_presense_channel}')

myPresenceChannel.bind('pusher:subscription_succeeded', function(member_list){ updatePresence(member_list); })

myPresenceChannel.bind('pusher:member_added', function(member){ updatePresence(myPresenceChannel.members()); })

myPresenceChannel.bind('pusher:member_removed', function(member){ updatePresence(myPresenceChannel.members()); })});

Friday, 27 August 2010

Page 49: Building realtimewebslide

Making Robust & Scalable

Beyond one process (プロセス間通信)

Beyond proxies (プロキシの壁)

Friday, 27 August 2010

Page 50: Building realtimewebslide

Beyond One Processプロセス間通信

@connections = {} # How do you scale beyond one process?EM.run { EM::WebSocket.start(:host => "0.0.0.0",:port =>3001)do|ws| ws.onopen { puts "WebSocket connection open" @connections[ws.id] = ws } ws.onclose { @connections.delete(ws.id)} ws.onmessage { |data| @connections.each do |k, v| v.send(data) unless v.id == ws.id end } end}

Friday, 27 August 2010

Page 51: Building realtimewebslide

Beyond One Processプロセス間通信

API

API

API

WSC CC

WSC CC

WSC CC

Friday, 27 August 2010

Page 52: Building realtimewebslide

Beyond Proxiesプロキシの壁

http://www.infoq.com/articles/Web-Sockets-Proxy-Servers

Friday, 27 August 2010

Page 53: Building realtimewebslide

More Infohttp://blog.pusherapp.com

http://blog.new-bamboo.co.uk

http://video2010.scottishrubyconference.com/

http://www.atmarkit.co.jp (日本語記事書きました)

“We're rubyists living abroad. Any questions?”(08-29 13:30~15出ます)

Friday, 27 August 2010

Page 54: Building realtimewebslide

Thanksご清聴ありがとうございました

http://naun.blog118.fc2.com/

@pusherapp

@makoto_inoue

Friday, 27 August 2010