Engine.IO ããã¿ã Socket.IO ã®ä»å¾
intro
ãã®è¨äºã¯ 東京Node学園祭2012 アドベントカレンダー : ATND ã® 24 æ¥ç®ã®è¨äºã§ãã
Socket.IO ã® 1.0 ããåºãåºãã¨ãã£ã¦å
¨ç¶åºãªãã®ã§ã
ãããããã¦ããæ¹ãå¤ãã¨æãã¾ãã
ãããããã®è£ã§ã¯ Engin.IO ã¨ãããå²ãã¨è¯ãæãã®
ãã¡ããªã¼ããã¸ã§ã¯ããã§ãã¦ãã¦ã
ã¡ããã©å
æ¥ RealtimeConf ã§ããã®è©±ãããã¾ããã
ãã㯠Socket.IO ã«ãç¹ããã¯ãªããªã®ã§ã
ä»æ¥ã¯ãã® Engine.IO ã®è©±ããã¾ãã
åèã¯ãã®ã¸ãã
Engine.IO 㨠Socket.IO (㨠WebSocket.IO)
Socket.IO ã¯ã 1.0 ãè¦éã«å
¥ãããããã§ã
é¢é£ããã¸ã§ã¯ãã¨ãã¦ã Engine.IO 㨠WebSocket.IO ã®
äºã¤ã®ãããã¯ããæ°ãã«ç«ã¡ä¸ãã¾ããã
(ãã®è¾ºã¯ Web+DB Press ã§ã触ãã¾ããã)
ãã®äºã¤ã¯ãéçºãé²ãã§å¤ãã®æ©è½ãåä¸ã®ãããã¯ãã«å
å
ãã¦ãã Socket.IO ãã
ããå°ããªã¢ã¸ã¥ã¼ã«ã«å¥ãã¦ãä¿å®æ§ã»åå©ç¨æ§ãé«ããã¨ãã£ããã¨ãç®çã¨ãã¦ãã¾ãã
æçµçã«ã¯ä»¥ä¸ã®ãããªä¾åé¢ä¿ã«ãªãã¾ãã
Socket.IO | Engine.IO | WebSocket.IO | (ws)
å
·ä½çãªå½¹å²ã¯æ¬¡ã«èª¬æãã¾ãã
WebSocket.IO
éå»ã«ãã£ã WebSocket ãããã³ã«ã®ä»æ§å¤æ´ã¨ããããããããããã©ã¦ã¶ã®å®è£
å·®ç°ãå¸åãããã¨ãç®çã¨ããã
WebSocket ã®ã©ããã¢ã¸ã¥ã¼ã«ã§ãã(å®éã¯ãã®ã»ã¨ãã©ã ws ã¨ããã¢ã¸ã¥ã¼ã«ããã£ã¦ãã¾ãã)
ããã§ç«ã¦ã WebSocket ãµã¼ãã¯ãã©ããªãã¼ã¸ã§ã³ã® WebSocket ãå®è£
ãã¦ãã¦ãã
åãã¤ã³ã¿ãã§ã¼ã¹ã§ããåãã§ãã¾ãã
対å¿ãããã³ã«ãªã©ã詳細ã¯ä»¥ä¸ãåç §ãã ããã
Engine.IO
WebSocket ãå®è£
ããªããã©ã¦ã¶ã«å¯¾ãã¦ã Flash Socket ã Long Pooling ã¸ã®ãã©ã¼ã«ããã¯ãæä¾ãã¾ãã
WebSocket ã®éä¿¡ã«ã¯ WebSocket.IO ã使ã£ã¦ãã¾ãã
ã ã£ããã§ãããä»ã¯ãããå°ãå¤ãã£ã¦ãã¾ãããã®è©±ã¯å¾ã»ã©ã
詳細ã¯ä»¥ä¸ã
Socket.IO
Engine.IO ããã¼ã¹ã«ãããã®ä¸ã« namespace ã authentication, custom events ãªã©ã®é«ã¬ãã« API ãæä¾ãã¾ãã
ãããããã®æ§æ㯠[email protected] ããã§ã 0.9 ã§ã¯ã¾ã Socket.IO 㯠Engine.IO ã«ä¾åãã¦ãããã
å¾æ¥éãèªåã§ãã©ã³ã¹ãã¼ããå®è£
ãã¦ãã¾ãã
æè¿ãæ´æ°ããã¾ããªãã®ã¯ã Socket.IO ã 1.0 ã«ããããã«ã
Engine.IO 㨠WebSocket.IO ã®å®å®ãæ¬ ãããªãããã§ãã
WebSocket.IO ã¯å²ãã¨è½ã¡çãã¦ãç¾å¨ã¯ Engine.IO ã®å®è£
ãé²ãã¦ãã¾ãã
ãããã Engine.IO ã«ã [email protected] ã¿ã°ãã¤ãã issue ãã¡ãã»ãæ®ã£ã¦ããã®ã§ã
ã¾ã ã¡ãã£ã¨ãããããªã¨ã¯æã£ã¦ãã¾ãã
ãã©ã¼ã«ããã¯ã¨ã¢ããã°ã¬ã¼ã
(ä½åãæ¸ããæ°ããã¾ãã)
Socket.IO ã§ã¯ãããã¾ã§æ¥ç¶ã®ç¢ºç«ã« WebSocket ãã¾ã試ãã
ãã©ã¦ã¶ãä¸éãµã¼ããªã©ã®å½±é¿ã§ãã¡ã ã£ãå ´åã®ã¿ã
XHR ã Flash ã«ãã©ã¼ã«ããã¯ããæ¹å¼ãã¨ã£ã¦ãã¾ããã
ãããããã®æ¹æ³ã§ã¯ãã©ã¼ã«ããã¯ãéãªã£ã¦ãã¾ã£ãæã
å®éã«éä¿¡å¯è½ãªæ¥ç¶ã確ç«ããã¾ã§ãçµæ§ãªæéãããã£ãã¾ãå ´åãããã¾ããã
åé¡ã¯ããã©ã¦ã¶ã®å®è£
ã¨ãããã㯠Intermediaries (Proxy, Personal Wirewall etc)ãå¤ããã¨ãããã£ã¦ãã¾ãã
Personal Firewall ã«é¢ãã Socket.IO ã®ãã¼ã ã®èª¿æ»ã¯ã wiki ã«ã¾ã¨ã¾ã£ã¦ãã¾ãã
ãã㧠Engine.IO ã§ã¯ãã¾ãæåã«æãå®å®ãã¦æ¥ç¶ã確ç«ã§ãã XHR LongPooling ã«ãã
æ¥ç¶ã確ç«ãããã®å¾ã«ããããéä¿¡æ¹æ³(FlashSocket, WebSocket)ã試ãã¦ãå¯è½ã ã£ãããã¡ãã«
ã¢ããã°ã¬ã¼ãããã¨ããæ¹æ³ãã¨ããã¨ã«ãªãã¾ããã
(ã ããããã®ã³ã³ããã¹ãã® Upgrade 㯠HTTP1.1 ã® Upgrade ã¨ã¯æå³ãéãã®ã§æ³¨æ)
Socket.IO ã Engine.IO ã«ä¾åãããã¨ã«ãªãã°ã Socket.IO ã®æåããããªããã¨ã«ãªãã¾ãã
ããã§ã¤ãã·ã£ã«ã®æ¥ç¶é度ã®åä¸ãè¦è¾¼ã¾ãã¾ãã
MetaEvent
ããã¤ãã®ã¤ãã³ãã API ã¨ãã¦å
¬éããããã¨ã«ãªãã¾ããã
ç¾æç¹ã§ã¯ä»¥ä¸ã®äºã¤ãäºå®ããã¦ãã¾ãã
(ã¨ãã£ã¦ããçºè¡¨èãã¦èªåãå®è£
ãããã§ããã)
socket.on('packet', callback); // ãã¹ã¦ã® packet (ping, message, close etc) ãåä¿¡ socket.on('packetCreate', callback); // ãã¹ã¦ã®éä¿¡
ããã§ãããç´°ããªå
é¨ç¶æ
ãåå¾ã§ãã¾ãã
ãã®ã³ã°ãå¯è¦åãããã¯ãªã©ããããããªãã§ãããã
Visualization
Guillermo éã¯ãªã¢ã«ã¿ã¤ã Web ã®éçºå ´é¢ã«ããã¦ãå§åçã«è¶³ãã¦ãªãã®ãã
ãå
é¨ã®ç¶æ
ã®å¯è¦å(Visualization)ãã ã¨èãã¦ãã¾ãã
- ã³ãã¯ã·ã§ã³ã®æ°
- ãã®ä¸ã® WebSocket æ¥ç¶ã®æ°
- çºçããéåä¿¡ã®æ°
- ã¬ã¤ãã·
- ã©ã®ãã©ã³ã¹ãã¼ãã使ã£ã¦ãã
- etc
ããã§å½¼ãã¯ãããã解決ããããã« engine.io-monitor ã¨ããããã¸ã§ã¯ãã®ç«ã¡ä¸ãã
èãã¦ããããã§ãã
ä»ã¯ã¬ã¤ãã£ãè¦ããµã³ãã«ã engine.io ã® example ã«å
¥ã£ã¦ãã¾ãã
engine.io/examples/latency at master · LearnBoost/engine.io · GitHub
ä¸ã§ä½ãèµ·ãã£ã¦ããã®ãããã¡ãã¨ææ¡ãããã¨ã¯ã¨ã¦ã大äºã§ããã
Engine.IO ã®ã¬ã¤ã¤ã§è¦ãããã°ã Socket.IO ã®éçºã§ã使ããã¯ããªã®ã§ã
ãã®è¾ºã¯ãæå¾
ãããã¨ããã§ããããã»ãã¨ã¾ã ãµã³ãã«ããç¡ãã®ã§ãã©ããªãã®ããªããã
debug
ã¡ãã£ã¨ããã¾ãããSocket.IO ãã¡ããªã¼ã§ã¯ TJ ã® debug ã¨ããã¢ã¸ã¥ã¼ã«ãæ¡ç¨ãã¦ãã¾ãã
https://github.com/visionmedia/debug
ããã¯ãNode.js の起動オプション、環境変数、npm start の話 - Block Rockin’ Codes ã§ãç´¹ä»ãã
ç°å¢å¤æ°ã使ã£ããããã°åºåã®å¿ç¨ã§ãã
Node æ¬ä½ã«ã¯ NODE_DEBUG ã§æå®ããã¨è¡¨ç¤ºããããããã°åºåãä»è¾¼ã¾ãã¦ãã¾ãã
$ NODE_DEBUG=http,net index.js
ããã¯åºæ¬æ¨æºã¢ã¸ã¥ã¼ã«ã®ãã®ã§ããããã®æ¹æ³ã«åã£ã¦èªåã®ã¢ã¸ã¥ã¼ã«ã«
ãããã°ãä»è¾¼ãã¢ã¸ã¥ã¼ã«ã debug ã§ãã
var debug = require('debug')('fuga') , http = require('http') debug('start'); http.createServer(function(req, res){ debug(req.method + ' ' + req.url); res.end('hello\n'); }).listen(3000, function(){ debug('listening'); });
ãã®ããã«ã require æã« ('http') ã¨ããã®ã¢ã¸ã¥ã¼ã«ã®åå空éãæå®ãã
debug() ã«åºåããããã°ãä»è¾¼ãã¨
$ DEBUG=fuga node index.js
ãªã©ã¨ããæã«ãä»è¾¼ã¾ãããããã°ãåºåã§ãã¾ãã
fuga ã * ã«ããã°ãå
¨ã¦ã®ã¢ã¸ã¥ã¼ã«ã®ãããã°ãåºã¾ãã
Socket.IO ãã¡ããªã¼ã¯ããã使ã£ã¦ãæ¥ç¶ã®ç¢ºç«ãã¡ãã»ã¼ã¸éä¿¡ãªã©ã®
æ
å ±ãåºããããã«ãã¦ãã®ã§ãéçºæã« DEBUG=engine.io ãªã©ã¨ããã°ã
è²ã
è¦ããããã«ãªã£ã¦ä¾¿å©ã§ãã
ã¾ãã debug ã«ã¯æãã£ãæéãåºããããbrowserify ã使ã£ã¦ãã©ã¦ã¶ã§ä½¿ã(åºåå
㯠localstrage)
ãã§ãããããªã®ã§ãã¯ã©ã¤ã¢ã³ãã®ãããã°ãæãããã§ãã
ã¾ã¨ã
ã¨ãããã¨ã§ã Socket.IO ã®æ´æ°é »åº¦ã¯ä¸ãã£ã¦ãããã
1.0 ãåºãåºãã¨ãã£ã¦ãããªãããªããªãåºãªãã§ããããããã¦ãæ¹ãå¤ãã¨æãã¾ããã
è£ã§ã¯ engine.ioã websocket.io ãé²ãã§ããã®ã§ããã¡ããåããã¦è¦ã¦ã¿ãã¨ã
ä»å¾ã®ååãåããããã¨ãããã¨ã§ããã
ã¡ãªã¿ã«ã http://realtimeconf.com/ ã¯ä»ã«ãè²ã
ãããããã»ãã·ã§ã³ããã£ããã
é²ç»ããããããªã®ã§ãèå³ã®ããæ¹ã¯è¦ã¦ã¿ãã¨é¢ç½ããã¨æãã¾ãã