Upgrade to Pro — share decks privately, control downloads, hide ads and more …

socket.io v1.0 introduction

socket.io v1.0 introduction

Tokyo Node Gakuen #tng13

Yosuke Furukawa

June 23, 2014
Tweet

More Decks by Yosuke Furukawa

Other Decks in Programming

Transcript

  1. Binary Support • sound • video • image • etc…

    ͜Ε·ͰemitͰ͖ͳ͔ͬͨ΋ͷ͕Ͱ͖ΔΑ͏ʹ ͳͬͨɻ
  2. ࣮ݱํ๏ // client side var img = canvas.toDataURL('image/webp'); var binary

    = window.atob(img.split(‘,')[1]); // binaryΛͦͷ·· emit socket.emit(‘image’, binary); ! // server side io.on('connection', function(socket){ // ࠓ·ͰͱมΘΒͳ͍ɺimage ΠϕϯτΛड͚औΔ socket.on('image', function(data){ // ड͚औͬͨbinaryͷσʔλΛbroadcast͢Δ socket.broadcast.volatile.emit('image', { id: socket.id, blob: data }); }); });
  3. ࣮ݱํ๏ // client side var img = canvas.toDataURL('image/webp'); var binary

    = window.atob(img.split(‘,')[1]); // binaryΛͦͷ·· emit socket.emit(‘image’, binary); ! // server side io.on('connection', function(socket){ // ࠓ·ͰͱมΘΒͳ͍ɺimage ΠϕϯτΛड͚औΔ socket.on('image', function(data){ // ड͚औͬͨbinaryͷσʔλΛbroadcast͢Δ socket.broadcast.volatile.emit('image', { id: socket.id, blob: data }); }); }); DBOWBT͔Βը૾ΛCJOBSZ Ͱऔಘ͠ɺFNJU FNJU͞ΕౕͨΛͦͷ·· CSPBEDBTU
  4. ࣮ݱํ๏(nginx) # in nginx.conf # ip_hashΛجʹ3001-3004ͷportͰಈ͍͍ͯΔserverʹ઀ଓ upstream io_nodes { ip_hash;

    server 127.0.0.1:3001; server 127.0.0.1:3002; server 127.0.0.1:3003; server 127.0.0.1:3004; } # ຊମ͸3000൪portͰड͚෇͚Δ server { listen 3000; server_name <io.yourhost.com>; location / { proxy_set_header Upgrade $http_upgrade; proxy_set_header Connection "upgrade"; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_set_header Host $host; proxy_http_version 1.1; # ͜͜Ͱproxy_pass ʹ io_nodesΛࢦఆ͢Δ proxy_pass http://io_nodes; } }
  5. ࣮ݱํ๏(nginx) # in nginx.conf # ip_hashΛجʹ3001-3004ͷportͰಈ͍͍ͯΔserverʹ઀ଓ upstream io_nodes { ip_hash;

    server 127.0.0.1:3001; server 127.0.0.1:3002; server 127.0.0.1:3003; server 127.0.0.1:3004; } # ຊମ͸3000൪portͰड͚෇͚Δ server { listen 3000; server_name <io.yourhost.com>; location / { proxy_set_header Upgrade $http_upgrade; proxy_set_header Connection "upgrade"; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_set_header Host $host; proxy_http_version 1.1; # ͜͜Ͱproxy_pass ʹ io_nodesΛࢦఆ͢Δ proxy_pass http://io_nodes; } } OHJOYͰJQΛجʹόοΫΤϯυ ͷαʔόʔʹৼΓ෼͚Δ ݟͯͷ௨ΓɺDMVTUFS͸࢖ͬͯͳ͍ɻ DMVTUFSͩͱৼΓ෼͚ઌ͕Ұҙʹ ఆ·Βͳ͍ڪΕɺ DMVTUFS࢖͏৔߹͸TUJDLZTFTTJPO NPEVMFΛ࢖͏ɻ
  6. ࣮ݱํ๏(redis) // socket.io-redisΛ࢖͏ // ͪͳΈʹadapter͸୔ࢁ͋ΔͷͰɺredis͡Όͳ͍ํ๏΋͋Δ(zero-mq etc) var sioredis = require('socket.io-redis');

    ! // io.adapterͰredisαʔόΛࢦఆ͢Δɻ io.adapter(sioredis({ host: 'localhost', port: 6379, })); ! ! // جຊ͜Ε͚ͩͰϓϩηεؒͷ΍ΓͱΓ͸࣮ݱՄೳɻ
  7. ࣮ݱํ๏(socket.io-go-emitter) // From golang code emitter := SocketIO.NewEmitter(&SocketIO.EmitterOpts{ // need

    Redis connect option Host:"localhost", Port:6379, }) emitter.Emit("message", “Hello World!”) ! // جຊ͜ΕΛ࣮ߦ͢Δ͚ͩɺEmitͬͯ΍Ε͹ͦΕΛon͍ͯ͠Δsocket.ioΫϥΠΞϯ τʹಧ͘ɻ ! // ݟͨײ͡ɺPHP, Python, Golang, RubyͰ͸࣮૷͞ΕͯΔ // (Perl͞Μ…)
  8. Other Tips • Debug͠΍͘͢ͳͬͨ # खલʹDEBUG=*ͱ͔͚ͭΔͱstdoutʹϩά͕දࣔ͞ΕΔ $ DEBUG=* node app.js

    ! visionmedia࡞ͷ debug module ࢖͍ͬͯΔɻ ! # ΫϥΠΞϯταΠυͷ৔߹΋localStorage.debug=“*”ͱ͔΍ΔͱϩΪϯά͞ΕΔ > localStorage.debug=“*” !
  9. Other Tips • ΫϥΠΞϯτ͕CDN ͔Β΋഑৴͞ ΕΔΑ͏ʹͳͬͨ # ࠓ·Ͱ <script src="/socket.io/socket.io.js"></script>

    ! # CDN͔Β΋഑৴͞ΕΔɻ <script src="https://cdn.socket.io/socket.io-1.0.6.js"></script>
  10. ·ͱΊ • New Engine • Binary Support • Scalability •

    Integration with others • Other Tips