{"id":3571,"date":"2012-10-29T21:16:04","date_gmt":"2012-10-29T12:16:04","guid":{"rendered":"http:\/\/vitalify.jp\/blog\/?p=3571"},"modified":"2012-11-13T10:52:13","modified_gmt":"2012-11-13T01:52:13","slug":"websocket%e3%81%a7%e3%83%81%e3%83%a3%e3%83%83%e3%83%88%e3%82%a2%e3%83%97%e3%83%aa%e3%82%92%e4%bd%9c%e3%82%8bwith-redisnode-js%e3%80%80%ef%bd%9e%e7%b6%9a%e3%81%8d%ef%bd%9e","status":"publish","type":"post","link":"https:\/\/vitalify.jp\/blog\/2012\/10\/websocket%e3%81%a7%e3%83%81%e3%83%a3%e3%83%83%e3%83%88%e3%82%a2%e3%83%97%e3%83%aa%e3%82%92%e4%bd%9c%e3%82%8bwith-redisnode-js%e3%80%80%ef%bd%9e%e7%b6%9a%e3%81%8d%ef%bd%9e.html","title":{"rendered":"WebSocket\u3067\u30c1\u30e3\u30c3\u30c8\u30a2\u30d7\u30ea\u3092\u4f5c\u308b(with Redis&amp;Node.js)\u3000\uff5e\u7d9a\u304d\uff5e"},"content":{"rendered":"<p>\u3069\u3046\u3082\u3002\u55b6\u696d\u90e8\u306e\u4f0a\u85e4\u3067\u3059\u3002<\/p>\n<p><a href=\"https:\/\/vitalify.jp\/blog\/2012\/10\/websocket%E3%81%A7%E3%83%81%E3%83%A3%E3%83%83%E3%83%88%E3%82%A2%E3%83%97%E3%83%AA%E3%82%92%E4%BD%9C%E3%82%8Bwith-redisnode-js.html\">\u5148\u65e5\u306e\u30d6\u30ed\u30b0<\/a>\u306e\u7d9a\u304d\u3067\u3059\u3002<br \/>\nRedis\u306e\u6e96\u5099\u304c\u7d42\u308f\u308a\u307e\u3057\u305f\u306e\u3067\u3001\u6b21\u306fWebSocket\u3092\u5229\u7528\u3059\u308b\u305f\u3081\u306e\u74b0\u5883\u3092\u7528\u610f\u3057\u307e\u3059\u3002<\/p>\n<p>WebSocket\u3092\u5229\u7528\u3059\u308b\u305f\u3081\u3001\u4eca\u56de\u306f\u30b5\u30fc\u30d0\u30fc\u5074\u306e\u30c6\u30af\u30ce\u30ed\u30b8\u30fc\u3068\u3057\u3066Node.js\u3092\u5229\u7528\u3059\u308b\u4e8b\u306b\u306a\u3057\u307e\u3057\u305f\u3002<br \/>\n<a href=\"http:\/\/ja.wikipedia.org\/wiki\/Jetty\" target=\"_blank\">Jetty<\/a>\u306a\u3069\u4ed6\u306b\u3082\u9078\u629e\u80a2\u306f\u3042\u3063\u305f\u306e\u3067\u3059\u304c\u3001\u53c2\u8003\u6587\u732e\u304c\u591a\u3044\u3068\u3044\u3046\u7406\u7531\u3067node\u306b\u3057\u307e\u3057\u305f\u3002<\/p>\n<p>\u3067\u306f\u3001node\u3092\u30a4\u30f3\u30b9\u30c8\u30fc\u30eb\u3057\u307e\u3059\u3002<br \/>\n\u30a4\u30f3\u30b9\u30c8\u30fc\u30eb\u306b\u306f\u3001node\u3092\u5c0e\u5165\u3059\u308b\u305f\u3081\u306e\u30b7\u30a7\u30eb\u30b9\u30af\u30ea\u30d7\u30c8\u3067\u30a4\u30f3\u30b9\u30c8\u30fc\u30eb\u3092\u7c21\u7565\u5316\u3057\u3066\u304f\u308c\u308b\u300cnave\u300d(\u8907\u6570\u306e\u30d0\u30fc\u30b8\u30e7\u30f3\u7ba1\u7406\u3082\u53ef\u80fd)\u3084git\u4e0a\u306b\u3082\u69d8\u3005\u306a\u30aa\u30fc\u30d7\u30f3\u30bd\u30fc\u30b9\u304c\u3042\u308a\u307e\u3059\u304c\u3001\u4eca\u56de\u306f\u8907\u6570\u30d0\u30fc\u30b8\u30e7\u30f3\u3092\u7ba1\u7406\u3059\u308b\u5fc5\u8981\u3082\u306a\u3044\u305f\u3081\u3001\u666e\u901a\u306b\u30bd\u30fc\u30b9\u3092\u30c0\u30a6\u30f3\u30ed\u30fc\u30c9\u3057\u3066\u30b3\u30f3\u30d1\u30a4\u30eb\u3059\u308b\u4e8b\u306b\u3057\u307e\u3057\u305f\u3002<br \/>\n\u30d0\u30fc\u30b8\u30e7\u30f3\u306f\u30a4\u30f3\u30b9\u30c8\u30fc\u30eb\u5f53\u6642(2012\u5e749\u6708\u9803)\u306estable\u3060\u3063\u305f0.8.6\u306b\u3057\u307e\u3057\u305f\u3002<\/p>\n<div style=\"background-color: #222222\"># cd \/usr\/local\/src<br \/>\n# wget http:\/\/nodejs.org\/dist\/v0.8.6\/node-v0.8.6.tar.gz<br \/>\n# tar xf node-v0.8.6.tar.gz<br \/>\n# cd node-v0.8.6<br \/>\n# .\/configure<br \/>\n# make install<\/div>\n<p>\u6b21\u306bnode\u5411\u3051\u306e\u30d1\u30c3\u30b1\u30fc\u30b8\u30de\u30cd\u30fc\u30b8\u30e3\u3067\u3042\u308bnpm(Node Package Manager)\u3092\u30a4\u30f3\u30b9\u30c8\u30fc\u30eb\u3057\u307e\u3059\u3002<br \/>\nnpm\u3067\u7ba1\u7406\u3055\u308c\u3066\u3044\u308b\u30e9\u30a4\u30d6\u30e9\u30ea\u306f\u4ee5\u4e0bURL\u3092\u3054\u89a7\u304f\u3060\u3055\u3044\u3002<br \/>\n<a href=\"https:\/\/npmjs.org\/\" target=\"_blank\">https:\/\/npmjs.org\/<\/a><br \/>\nnode\u30b3\u30de\u30f3\u30c9\u304c\u5229\u7528\u3067\u304d\u308b\u304b\u3092\u78ba\u8a8d\u5f8c\u3001\u4ee5\u4e0b\u3092\u5b9f\u884c\u3057\u307e\u3059\u3002<\/p>\n<div style=\"background-color: #222222\"># node -v<br \/>\nv0.8.6<br \/>\n# curl http:\/\/npmjs.org\/install.sh | sh<\/div>\n<p>\u5bfe\u8a71\u30e2\u30fc\u30c9\u3092&#8221;yes&#8221;\u3068\u7b54\u3048\u3066\u3044\u304f\u3068\u30a4\u30f3\u30b9\u30c8\u30fc\u30eb\u5b8c\u4e86\u3067\u3059\u3002<br \/>\n\u3053\u308c\u3067node\u304c\u5229\u7528\u3067\u304d\u308b\u72b6\u614b\u3067\u3042\u308c\u3070\u3001npm\u30b3\u30de\u30f3\u30c9\u3082\u5229\u7528\u3067\u304d\u308b\u3088\u3046\u306b\u306a\u308a\u307e\u3059\u3002<br \/>\n\u65e9\u901f\u30d0\u30fc\u30b8\u30e7\u30f3\u3060\u3051\u78ba\u8a8d\u3057\u3066\u307f\u307e\u3059\u3002<\/p>\n<div style=\"background-color: #222222\"># npm -v<br \/>\n1.1.48<\/div>\n<p>\u6b21\u306b\u30d1\u30c3\u30b1\u30fc\u30b8\u3092\u30c0\u30a6\u30f3\u30ed\u30fc\u30c9\u3059\u308b\u306e\u3067\u3059\u304c\u3001\u305d\u306e\u524d\u306bnode\u306e\u52d5\u304d\u3092\u4e00\u5ea6\u78ba\u8a8d\u3057\u3066\u304a\u304d\u307e\u3059\u3002<br \/>\n\u738b\u9053\u306eHello World\u3092\u8868\u793a\u3059\u308b\u305f\u3081\u3001node\u30b3\u30f3\u30bd\u30fc\u30eb\u3092\u958b\u304d\u4ee5\u4e0b\u30b9\u30af\u30ea\u30d7\u30c8\u3092\u5b9f\u884c\u3057\u307e\u3059\u3002<\/p>\n<div style=\"background-color: #222222\"># node<br \/>\n&gt; var http = require(&#8220;http&#8221;);<br \/>\n&gt; http.createServer(function(req, res){<br \/>\n&gt; res.writeHead(200, {&#8220;Content-Type&#8221;: &#8220;text\/plain&#8221;});<br \/>\n&gt; res.end(&#8220;Hello World&#8221;);<br \/>\n&gt; }).listen(3000, &#8220;127.0.0.1&#8221;);<\/div>\n<p>\u30d6\u30e9\u30a6\u30b6\u3067\u4e0a\u8a18URL(http:\/\/127.0.0.1:3000\/)\u306b\u30a2\u30af\u30bb\u30b9\u3059\u308b\u3068\u3001\u753b\u9762\u4e0a\u306b\u300cHello World\u300d\u304c\u8868\u793a\u3055\u308c\u307e\u3059\u3002<br \/>\n\u5358\u7d14\u306b\u30c6\u30ad\u30b9\u30c8\u3092\u8868\u793a\u3059\u308b\u3060\u3051\u306a\u306e\u306b\u3001\u30d8\u30c3\u30c0\u30fc\u30b9\u30c6\u30fc\u30bf\u30b9\u30b3\u30fc\u30c9200\uff1f\u3001listen\uff1f<br \/>\n\u666e\u6bb5\u306fTomcat\u3084Apache(+CGI)\u3068\u3044\u3063\u305f\u30b3\u30f3\u30c6\u30ca\u4e0a\u3067\u30b5\u30fc\u30d6\u30ec\u30c3\u30c8\u3084PHP\u3067\u30a2\u30d7\u30ea\u30b1\u30fc\u30b7\u30e7\u30f3\u3092\u69cb\u7bc9\u3059\u308b\u4e8b\u304c\u591a\u3044\u3068\u601d\u3044\u307e\u3059\u304c\u3001node\u306e\u5834\u5408\u306f\u30a2\u30d7\u30ea\u30b1\u30fc\u30b7\u30e7\u30f3\u3060\u3051\u3067\u306a\u304f\u3001\u4e0a\u8a18\u30b5\u30f3\u30d7\u30eb\u306e\u3088\u3046\u306bnode\u81ea\u4f53\u304c\u30b3\u30f3\u30c6\u30ca\u3068\u306a\u308a\u307e\u3059\u3002<br \/>\n\u305d\u306e\u70ba\u3001node\u3067\u4f8b\u5916\u304c\u767a\u751f\u3059\u308b\u3068\u30b5\u30fc\u30d0\u6a5f\u80fd\u3054\u3068\u30d7\u30ed\u30bb\u30b9\u304c\u7d42\u4e86\u3057\u3066\u3057\u307e\u3044\u307e\u3059\u306e\u3067\u3001\u30b5\u30fc\u30d0\u6a5f\u80fd\u307e\u3067\u5171\u5012\u308c\u3057\u306a\u3044\u3088\u3046\u4f8b\u5916\u51e6\u7406\u306b\u306f\u6c17\u3092\u4ed8\u3051\u308b\u5fc5\u8981\u304c\u3042\u308a\u307e\u3059\u3002<br \/>\n\u203bnode\u30b3\u30f3\u30bd\u30fc\u30eb\u4e0a\u3067\u5b9f\u884c\u3057\u3066\u3044\u307e\u3059\u304c\u3001\u4e0a\u8a18\u30b9\u30af\u30ea\u30d7\u30c8\u3092\u30d5\u30a1\u30a4\u30eb\u306b\u4fdd\u5b58\u3057(\u4f8b\u3048\u3070test.js)\u3001node\u30b3\u30de\u30f3\u30c9\u306e\u30d1\u30e9\u30e1\u30fc\u30bf\u3068\u3057\u3066\u5b9f\u884c\u3059\u308b\u4e8b\u3082\u53ef\u80fd\u3067\u3059\u3002<\/p>\n<p>\u3067\u306fwebsocket\u3092\u5229\u7528\u3059\u308b\u305f\u3081\u3001\u5e7e\u3064\u304b\u306e\u30d1\u30c3\u30b1\u30fc\u30b8\u3092npm\u3092\u5229\u7528\u3057\u3066\u30a4\u30f3\u30b9\u30c8\u30fc\u30eb\u3057\u307e\u3059\u3002<\/p>\n<p>1. <a href=\"http:\/\/expressjs.com\/\" target=\"_blank\">Express<\/a><br \/>\nExpress\u306fnode\u7528\u306eWeb\u30d5\u30ec\u30fc\u30e0\u30ef\u30fc\u30af\u3067\u3059\u3002<br \/>\n\u4e0a\u8ff0\u3057\u305f\u3088\u3046\u306bnode\u306e\u5834\u5408\u306fnode\u81ea\u8eab\u304c\u30b3\u30f3\u30c6\u30ca\u306e\u5f79\u5272\u3092\u679c\u305f\u3059\u5fc5\u8981\u304c\u3042\u308b\u305f\u3081\u3001\u9759\u7684\u306a\u30d5\u30a1\u30a4\u30eb\u306e\u914d\u4fe1\u306b\u3064\u3044\u3066\u3082node\u5185\u3067\u5bfe\u5fdc\u3057\u306a\u3051\u308c\u3070\u306a\u308a\u307e\u305b\u3093\u3002\u3053\u306e\u624b\u9593\u3092Web\u30a2\u30d7\u30ea\u30b1\u30fc\u30b7\u30e7\u30f3\u30d5\u30ec\u30fc\u30e0\u30ef\u30fc\u30af\u3067\u3042\u308bExpress\u3092\u5229\u7528\u3059\u308b\u3053\u3068\u3067\u5bb9\u6613\u306b\u89e3\u6c7a\u3067\u304d\u307e\u3059\u3002<br \/>\n\u4ee5\u4e0b\u30b3\u30de\u30f3\u30c9\u3067\u30d1\u30c3\u30b1\u30fc\u30b8\u3092\u30b0\u30ed\u30fc\u30d0\u30eb\u30a4\u30f3\u30b9\u30c8\u30fc\u30eb\u3057\u307e\u3059\u3002<br \/>\n\u203b\u30b0\u30ed\u30fc\u30d0\u30eb\u30a4\u30f3\u30b9\u30c8\u30fc\u30eb\u306b\u3057\u306a\u3044\u3068\u30d1\u30b9\u304c\u901a\u3089\u306a\u3044\u30b1\u30fc\u30b9\u304c\u3042\u308b\u305f\u3081\u3001\u4ee5\u4e0b\u30b3\u30de\u30f3\u30c9\u3067\u30a4\u30f3\u30b9\u30c8\u30fc\u30eb\u3059\u308b\u3053\u3068\u3092\u304a\u52e7\u3081\u3057\u307e\u3059\u3002<\/p>\n<div style=\"background-color: #222222\">$ npm install -g express<\/div>\n<p>\u30a4\u30f3\u30b9\u30c8\u30fc\u30eb\u30c7\u30a3\u30ec\u30af\u30c8\u30ea\u306b\u3042\u308bapp.js\u3092\u5b9f\u884c\u3057\u307e\u3059\u3002<\/p>\n<div style=\"background-color: #222222\">$ node app.js<\/div>\n<p>http:\/\/127.0.0.1:3000\/ \u306b\u30a2\u30af\u30bb\u30b9\u3057\u3066\u300cWelcome to Express\u300d\u3068\u8868\u793a\u3055\u308c\u3066\u3044\u308c\u3070\u30a4\u30f3\u30b9\u30c8\u30fc\u30eb\u5b8c\u4e86\u3067\u3059\u3002<\/p>\n<p>2. <a href=\"http:\/\/socket.io\/\" target=\"_blank\">Socket.IO<\/a><br \/>\nSocket.IO\u306f\u4eca\u56de\u5229\u7528\u3059\u308bWebSocket\u3092\u30d6\u30e9\u30a6\u30b6\u306e\u4e92\u63db\u6027\u3092\u8003\u616e\u305b\u305a\u306b\u5229\u7528\u3067\u304d\u308b\u62bd\u8c61\u5316\u30e9\u30a4\u30d6\u30e9\u30ea\u3067\u3059\u3002<br \/>\nWebSocket\u3092\u5229\u7528\u3067\u304d\u306a\u3044\u30d6\u30e9\u30a6\u30b6\u3067\u306fAjax\u306e\u30ed\u30f3\u30b0\u30dd\u30fc\u30ea\u30f3\u30b0\u3092\u5229\u7528\u3059\u308b\u306a\u3069\u3001\u30d6\u30e9\u30a6\u30b6\u9593\u306e\u4e92\u63db\u6027\u3092\u6c17\u306b\u305b\u305a\u306b\u958b\u767a\u304c\u53ef\u80fd\u306b\u306a\u308a\u307e\u3059\u3002<br \/>\n\u4ee5\u4e0b\u30b3\u30de\u30f3\u30c9\u3067\u30d1\u30c3\u30b1\u30fc\u30b8\u3092\u30a4\u30f3\u30b9\u30c8\u30fc\u30eb\u3057\u307e\u3059\u3002<\/p>\n<div style=\"background-color: #222222\">$ npm install socket.io<\/div>\n<p>\u3053\u308c\u3067\u4e00\u901a\u308a\u306e\u6e96\u5099\u304c\u7d42\u308f\u308a\u307e\u3057\u305f\u306e\u3067\u3001\u6b21\u306f\u30b9\u30af\u30ea\u30d7\u30c8\u3092\u7528\u610f\u3057\u307e\u3059\u3002<br \/>\npublic\u30c7\u30a3\u30ec\u30af\u30c8\u30ea\u5185\u306b\u79fb\u52d5\u3057\u3001chat.html\u30d5\u30a1\u30a4\u30eb\u3092\u4f5c\u6210\u3057\u307e\u3059\u3002<\/p>\n<div style=\"background-color: #222222\">&lt;head&gt;<br \/>\n&lt;script src=&#8221;\/socket.io\/socket.io.js&#8221;&gt;&lt;\/script&gt;<br \/>\n&lt;script src=&#8221;\/javascripts\/chat.js&#8221;&gt;&lt;\/script&gt;<br \/>\n&lt;\/head&gt;<br \/>\n&lt;body&gt;<br \/>\n&lt;input type=&#8221;text&#8221; id=&#8221;comment&#8221; placeholder=&#8221;\u5165\u529b\u3057\u3066\u304f\u3060\u3055\u3044&#8221; value=&#8221;&#8221; size=20 &gt;&lt;\/input&gt;<br \/>\n&lt;button onclick=javascript:send() &gt;\u9001\u4fe1&lt;\/button&gt;<br \/>\n&lt;p id=&#8221;list&#8221;&gt;&lt;\/p&gt;<br \/>\n&lt;\/body&gt;<\/div>\n<p>\u6b21\u306b\u3001javascript\u30c7\u30a3\u30ec\u30af\u30c8\u30ea\u306b\u4ee5\u4e0b\u30d5\u30a1\u30a4\u30eb(chat.js)\u3092\u4f5c\u6210\u3057\u307e\u3059\u3002<br \/>\n\u30af\u30e9\u30a4\u30a2\u30f3\u30c8\u30b5\u30a4\u30c9\u306eJavaScript\u3067\u3059\u3002<\/p>\n<div style=\"background-color: #222222\">var postList;<br \/>\nwindow.onload=function(){<br \/>\npostList = document.getElementById(&#8220;list&#8221;);<br \/>\n}var listleft = function(){<br \/>\npostList.innerHTML+=&#8221;&lt;div align=&#8217;left&#8217;&gt;&lt;img src=&#8217;\/images\/left.gif&#8217;&gt;&#8221;+arguments[0]+&#8221;&lt;\/div&gt;&#8221;;<br \/>\n}var listright = function(){<br \/>\npostList.innerHTML+=&#8221;&lt;div align=&#8217;right&#8217;&gt;&#8221;+arguments[0]+&#8221;&lt;img src=&#8217;\/images\/right.gif&#8217;&gt;&lt;\/div&gt;&#8221;;<br \/>\n}<\/p>\n<p>var socket = io.connect(&#8220;http:\/\/127.0.0.1&#8221;, {port:3000});<br \/>\nsocket.on(&#8220;connect&#8221;, function() {<br \/>\nlistleft(&#8220;\u63a5\u7d9a\u958b\u59cb&#8221;); \/\/\u63a5\u7d9a\u3057\u305f\u5f53\u4eba\u306b\u3060\u3051\u8868\u793a<br \/>\nsocket.emit(&#8220;msg send&#8221;, &#8220;\u7686\u3055\u3093\u5b9c\u3057\u304f\u304a\u9858\u3044\u3057\u307e\u3059\u3002&#8221;);<\/p>\n<p>\/\/ \u81ea\u5206\u306e\u30b3\u30e1\u30f3\u30c8\u3092\u8868\u793a<br \/>\nsocket.on(&#8220;msg mycommnet&#8221;, function(commnet){<br \/>\nlistleft(commnet);<br \/>\n});<\/p>\n<p>\/\/ \u4ed6\u4eba\u306e\u30b3\u30e1\u30f3\u30c8\u3092\u8868\u793a<br \/>\nsocket.on(&#8220;msg elsecommnet&#8221;, function(commnet){<br \/>\nlistright(commnet);<br \/>\n});<\/p>\n<p>socket.on(&#8220;disconnect&#8221;, function(){<br \/>\nlistleft(&#8220;\u63a5\u7d9a\u304c\u5207\u308c\u307e\u3057\u305f\u3002&#8221;);<br \/>\n});<br \/>\n});<\/p>\n<p>function send(){<br \/>\nvar comment = document.getElementById(&#8220;comment&#8221;).value;<br \/>\nsocket.emit(&#8220;commnet send&#8221;, comment);<br \/>\n}<\/p>\n<\/div>\n<p>\u81ea\u5206\u304c\u6295\u7a3f\u3057\u305f\u5185\u5bb9\u306f\u5de6\u5074\u306b\u8868\u793a\u3057\u3001\u81ea\u5206\u4ee5\u5916\u306e\u6295\u7a3f\u306f\u53f3\u5074\u306b\u8868\u793a\u3059\u308b\u3088\u3046\u306b\u3057\u307e\u3059\u3002<br \/>\n\u6700\u5f8c\u306b\u5148\u307b\u3069\u52d5\u4f5c\u78ba\u8a8d\u3057\u305fapp.js\u306e\u4e0b\u6bb5\u306bsocket\u30a4\u30f3\u30b9\u30bf\u30f3\u30b9\u4f5c\u6210\u51e6\u7406\u3092\u8ffd\u8a18\u3057\u307e\u3059\u3002<\/p>\n<div style=\"background-color: #222222\">var express = require(&#8220;express&#8221;);<br \/>\nvar app = module.exports = express.createServer();app.configure(function(){<br \/>\napp.set(&#8220;views&#8221;, __dirname + &#8220;\/views&#8221;);<br \/>\napp.set(&#8220;view engine&#8221;, &#8220;ejs&#8221;);<br \/>\napp.use(express.bodyParser());<br \/>\napp.use(express.methodOverride());<br \/>\napp.use(express.cookieParser());<br \/>\napp.use(express.session({ secret: &#8220;your secret here&#8221; }));<br \/>\napp.use(app.router);<br \/>\napp.use(express.static(__dirname + &#8220;\/public&#8221;));<br \/>\n});app.configure(&#8220;development&#8221;, function(){<br \/>\napp.use(express.errorHandler({ dumpExceptions: true, showStack: true }));<br \/>\n});<\/p>\n<p>app.configure(&#8220;production&#8221;, function(){<br \/>\napp.use(express.errorHandler());<br \/>\n});<\/p>\n<p>app.get(&#8220;\/&#8221;, function(req, res){<br \/>\nres.render(&#8220;index&#8221;, {<br \/>\ntitle: &#8220;Express&#8221;<br \/>\n});<br \/>\n});<\/p>\n<p>app.listen(3000);<br \/>\nconsole.log(&#8220;Express server listening on port %d in %s mode&#8221;, app.address().port, app.settings.env);<\/p>\n<p>\/\/socket\u30a4\u30f3\u30b9\u30bf\u30f3\u30b9\u3092\u4f5c\u6210<br \/>\nvar io = require(&#8220;socket.io&#8221;).listen(app);<br \/>\nio.sockets.on(&#8220;connection&#8221;, function(socket){<br \/>\nsocket.on(&#8220;commnet send&#8221;, function (commnet) {<br \/>\nsocket.emit(&#8220;msg mycommnet&#8221;&#8221;, commnet); \/\/\u81ea\u5206\u81ea\u8eab\u306b\u901a\u77e5<br \/>\nsocket.broadcast.emit(&#8220;msg elsecommnet&#8221;, commnet); \/\/\u81ea\u5206\u4ee5\u5916\u3067\u540c\u4e00\u30bd\u30b1\u30c3\u30c8\u3068\u30cf\u30f3\u30c9\u30b7\u30a7\u30a4\u30af\u4e2d\u306e\u4eba\u306b\u30ad\u30e3\u30b9\u30c8<br \/>\n});<br \/>\nsocket.on(&#8220;disconnect&#8221;, function() {<br \/>\nlog(\u63a5\u7d9a\u304c\u5207\u308c\u307e\u3057\u305f\u3002&#8221;);<br \/>\n});<br \/>\n});<\/p>\n<\/div>\n<p>\u30b9\u30af\u30ea\u30d7\u30c8\u306e\u6e96\u5099\u304c\u6574\u3044\u307e\u3057\u305f\u306e\u3067\u3001\u518d\u5ea6\u30b5\u30fc\u30d0\u30fc\u3092\u8d77\u52d5\u3057\u307e\u3059\u3002<\/p>\n<div style=\"background-color: #222222\">$ node app.js<\/div>\n<p>\u305d\u306e\u5f8c\u3001public\u30c7\u30a3\u30ec\u30af\u30c8\u30ea\u5185\u306b\u65b0\u898f\u4f5c\u6210\u3057\u305fchat.html\u306b\u30a2\u30af\u30bb\u30b9\u3057\u307e\u3059\u3002<br \/>\nhttp:\/\/127.0.0.1:3000\/chat.html<\/p>\n<p><a href=\"https:\/\/vitalify.jp\/blog\/wp-content\/uploads\/2012\/10\/chat.jpg\"><img decoding=\"async\" loading=\"lazy\" class=\"aligncenter size-large wp-image-3572\" src=\"https:\/\/vitalify.jp\/blog\/wp-content\/uploads\/2012\/10\/chat-1024x618.jpg\" alt=\"\" width=\"584\" height=\"352\" srcset=\"https:\/\/vitalify.jp\/blog\/wp-content\/uploads\/2012\/10\/chat-1024x618.jpg 1024w, https:\/\/vitalify.jp\/blog\/wp-content\/uploads\/2012\/10\/chat-300x181.jpg 300w, https:\/\/vitalify.jp\/blog\/wp-content\/uploads\/2012\/10\/chat-496x300.jpg 496w, https:\/\/vitalify.jp\/blog\/wp-content\/uploads\/2012\/10\/chat.jpg 1100w\" sizes=\"(max-width: 584px) 85vw, 584px\" \/><\/a><\/p>\n<p>\u307e\u305a\u59cb\u3081\u306b\u3001\u30a2\u30a4\u30b3\u30f3\u304c\u30c0\u30b5\u904e\u304e\u308b\u70b9\u306b\u3064\u3044\u3066\u306f\u7121\u8996\u3057\u3066\u304f\u3060\u3055\u3044\u3002\u3002\u3002<br \/>\n\u30af\u30e9\u30a4\u30a2\u30f3\u30c8\u5074\u304b\u3089\u30cf\u30f3\u30c9\u30b7\u30a7\u30a4\u30af\u8981\u6c42\u3092\u9001\u4fe1\u3057\u3001\u30b5\u30fc\u30d0\u30fc\u5074\u304b\u3089\u30cf\u30f3\u30c9\u30b7\u30a7\u30a4\u30af\u306e\u5fdc\u7b54\u304c\u3042\u308c\u3070\u81ea\u8eab\u306e\u30d6\u30e9\u30a6\u30b6\u3060\u3051\u306b\u300c\u63a5\u7d9a\u958b\u59cb\u300d\u3068\u3044\u3046\u30e1\u30c3\u30bb\u30fc\u30b8\u304c\u8868\u793a\u3055\u308c\u307e\u3059\u3002<br \/>\n\u305d\u306e\u5f8c\u3001\u65e2\u306b\u540c\u4e00\u30bd\u30b1\u30c3\u30c8\u306b\u30a2\u30af\u30bb\u30b9\u4e2d\u306e\u30e6\u30fc\u30b6\u30fc\u306b\u63a5\u7d9a\u304c\u78ba\u7acb\u3057\u305f\u65e8\u3092\u4f1d\u3048\u308b\u305f\u3081\u3001\u300c\u7686\u3055\u3093\u5b9c\u3057\u304f\u304a\u9858\u3044\u3057\u307e\u3059\u3002\u300d\u3068\u3046\u30e1\u30c3\u30bb\u30fc\u30b8\u3092\u767a\u4fe1(emit)\u3057\u307e\u3059\u3002<br \/>\n\u4e0a\u8a18\u753b\u50cf\u306e\u5834\u5408\u3001\u5de6\u5074\u306e\u30d6\u30e9\u30a6\u30b6\u304c\u5148\u306b\u30a2\u30af\u30bb\u30b9\u3057\u305f\u30d6\u30e9\u30a6\u30b6\u3067\u3001\u53f3\u5074\u306e\u30d6\u30e9\u30a6\u30b6\u304c\u5f8c\u306b\u30a2\u30af\u30bb\u30b9\u3057\u305f\u30d6\u30e9\u30a6\u30b6\u3067\u3059\u3002<br \/>\n\u53f3\u5074\u306e\u30d6\u30e9\u30a6\u30b6\u3067\u63a5\u7d9a\u5f8c\u306b\u30b3\u30e1\u30f3\u30c8\u3092\u9001\u4fe1\u3059\u308b\u3068\u3001\u30ea\u30a2\u30eb\u30a2\u30a4\u30e0\u306b\u5de6\u5074\u306e\u30d6\u30e9\u30a6\u30b6\u306b\u6295\u7a3f\u5185\u5bb9\u304c\u53cd\u6620\u3055\u308c\u307e\u3057\u305f\u3002\u3044\u3044\u611f\u3058\u266a<br \/>\n\u4f8b\u5916\u51e6\u7406\u3001DB\u3092\u5229\u7528\u3057\u305f\u6a5f\u80fd(\u904e\u53bb\u30c7\u30fc\u30bf\u53d6\u5f97\u306a\u3069)\u306a\u3069\u306f\u672a\u5b9f\u88c5\u306e\u72b6\u614b\u3067\u3059\u304c\u3001\u4e00\u65e6\u3053\u308c\u3067\u30c1\u30e3\u30c3\u30c8Web\u30a2\u30d7\u30ea\u306e\u57fa\u76e4\u306f\u3067\u304d\u307e\u3057\u305f\u3002<br \/>\n\u6b21\u306fRedis\u3092\u5229\u7528\u3057\u305f\u904e\u53bb\u30c7\u30fc\u30bf\u53d6\u5f97\u3001Publish\/Subscribe\u578b(Pub\/Sub\u578b)\u3068node\u3092\u9023\u643a\u3057\u305f\u5197\u9577\u5316\u3092\u9032\u3081\u3066\u3044\u304d\u307e\u3059\u3002<br \/>\n\u3053\u3061\u3089\u306b\u3064\u3044\u3066\u306f\u9032\u5c55\u304c\u3042\u308c\u3070\u307e\u305f\u30d6\u30ed\u30b0\u306b\u6295\u7a3f\u3055\u305b\u3066\u3044\u305f\u3060\u304d\u307e\u3059\u3002<\/p>\n<p>\u305d\u308c\u3067\u306f\u3002<\/p>\n<div class='wp_social_bookmarking_light'>\n            <div class=\"wsbl_facebook_like\"><div id=\"fb-root\"><\/div><fb:like href=\"https:\/\/vitalify.jp\/blog\/2012\/10\/websocket%e3%81%a7%e3%83%81%e3%83%a3%e3%83%83%e3%83%88%e3%82%a2%e3%83%97%e3%83%aa%e3%82%92%e4%bd%9c%e3%82%8bwith-redisnode-js%e3%80%80%ef%bd%9e%e7%b6%9a%e3%81%8d%ef%bd%9e.html\" layout=\"button_count\" action=\"like\" share=\"false\" show_faces=\"false\" ><\/fb:like><\/div>\n            <div class=\"wsbl_google_plus_one\"><g:plusone size=\"medium\" annotation=\"none\" href=\"https:\/\/vitalify.jp\/blog\/2012\/10\/websocket%e3%81%a7%e3%83%81%e3%83%a3%e3%83%83%e3%83%88%e3%82%a2%e3%83%97%e3%83%aa%e3%82%92%e4%bd%9c%e3%82%8bwith-redisnode-js%e3%80%80%ef%bd%9e%e7%b6%9a%e3%81%8d%ef%bd%9e.html\" ><\/g:plusone><\/div>\n            <div class=\"wsbl_hatena_button\"><a href=\"\/\/b.hatena.ne.jp\/entry\/https:\/\/vitalify.jp\/blog\/2012\/10\/websocket%e3%81%a7%e3%83%81%e3%83%a3%e3%83%83%e3%83%88%e3%82%a2%e3%83%97%e3%83%aa%e3%82%92%e4%bd%9c%e3%82%8bwith-redisnode-js%e3%80%80%ef%bd%9e%e7%b6%9a%e3%81%8d%ef%bd%9e.html\" class=\"hatena-bookmark-button\" data-hatena-bookmark-title=\"WebSocket\u3067\u30c1\u30e3\u30c3\u30c8\u30a2\u30d7\u30ea\u3092\u4f5c\u308b(with Redis&amp;Node.js)\u3000\uff5e\u7d9a\u304d\uff5e\" data-hatena-bookmark-layout=\"standard\" title=\"\u3053\u306e\u30a8\u30f3\u30c8\u30ea\u30fc\u3092\u306f\u3066\u306a\u30d6\u30c3\u30af\u30de\u30fc\u30af\u306b\u8ffd\u52a0\"> <img src=\"\/\/b.hatena.ne.jp\/images\/entry-button\/button-only@2x.png\" alt=\"\u3053\u306e\u30a8\u30f3\u30c8\u30ea\u30fc\u3092\u306f\u3066\u306a\u30d6\u30c3\u30af\u30de\u30fc\u30af\u306b\u8ffd\u52a0\" width=\"20\" height=\"20\" style=\"border: none;\" \/><\/a><script type=\"text\/javascript\" src=\"\/\/b.hatena.ne.jp\/js\/bookmark_button.js\" charset=\"utf-8\" async=\"async\"><\/script><\/div>\n            <div class=\"wsbl_tumblr\"><a href=\"\/\/www.tumblr.com\/share?v=3&u=https%3A%2F%2Fvitalify.jp%2Fblog%2F2012%2F10%2Fwebsocket%25e3%2581%25a7%25e3%2583%2581%25e3%2583%25a3%25e3%2583%2583%25e3%2583%2588%25e3%2582%25a2%25e3%2583%2597%25e3%2583%25aa%25e3%2582%2592%25e4%25bd%259c%25e3%2582%258bwith-redisnode-js%25e3%2580%2580%25ef%25bd%259e%25e7%25b6%259a%25e3%2581%258d%25ef%25bd%259e.html&t=WebSocket%E3%81%A7%E3%83%81%E3%83%A3%E3%83%83%E3%83%88%E3%82%A2%E3%83%97%E3%83%AA%E3%82%92%E4%BD%9C%E3%82%8B%28with%20Redis%26amp%3BNode.js%29%E3%80%80%EF%BD%9E%E7%B6%9A%E3%81%8D%EF%BD%9E\" title=\"Share on Tumblr\" style=\"display:inline-block; text-indent:-9999px; overflow:hidden; width:81px; height:20px; background:url('\/\/platform.tumblr.com\/v1\/share_1.png') top left no-repeat transparent;\">Share on Tumblr<\/a><\/div>\n            <div class=\"wsbl_twitter\"><a href=\"https:\/\/twitter.com\/share\" class=\"twitter-share-button\" data-url=\"https:\/\/vitalify.jp\/blog\/2012\/10\/websocket%e3%81%a7%e3%83%81%e3%83%a3%e3%83%83%e3%83%88%e3%82%a2%e3%83%97%e3%83%aa%e3%82%92%e4%bd%9c%e3%82%8bwith-redisnode-js%e3%80%80%ef%bd%9e%e7%b6%9a%e3%81%8d%ef%bd%9e.html\" data-text=\"WebSocket\u3067\u30c1\u30e3\u30c3\u30c8\u30a2\u30d7\u30ea\u3092\u4f5c\u308b(with Redis&amp;Node.js)\u3000\uff5e\u7d9a\u304d\uff5e\" data-lang=\"ja\">Tweet<\/a><\/div>\n    <\/div>\n<br class='wp_social_bookmarking_light_clear' \/>\n","protected":false},"excerpt":{"rendered":"<p>\u3069\u3046\u3082\u3002\u55b6\u696d\u90e8\u306e\u4f0a\u85e4\u3067\u3059\u3002 \u5148\u65e5\u306e\u30d6\u30ed\u30b0\u306e\u7d9a\u304d\u3067\u3059\u3002 Redis\u306e\u6e96\u5099\u304c\u7d42\u308f\u308a\u307e\u3057\u305f\u306e\u3067\u3001\u6b21\u306fWebSocket\u3092\u5229\u7528\u3059\u308b\u305f\u3081\u306e\u74b0\u5883\u3092\u7528\u610f\u3057\u307e\u3059\u3002 WebSocket\u3092\u5229\u7528\u3059\u308b\u305f\u3081\u3001\u4eca\u56de\u306f\u30b5\u30fc\u30d0\u30fc\u5074\u306e\u30c6\u30af\u30ce\u30ed\u30b8\u30fc\u3068\u3057\u3066N &hellip; <a href=\"https:\/\/vitalify.jp\/blog\/2012\/10\/websocket%e3%81%a7%e3%83%81%e3%83%a3%e3%83%83%e3%83%88%e3%82%a2%e3%83%97%e3%83%aa%e3%82%92%e4%bd%9c%e3%82%8bwith-redisnode-js%e3%80%80%ef%bd%9e%e7%b6%9a%e3%81%8d%ef%bd%9e.html\" class=\"more-link more_btn\"><span class=\"screen-reader-text\">&#8220;WebSocket\u3067\u30c1\u30e3\u30c3\u30c8\u30a2\u30d7\u30ea\u3092\u4f5c\u308b(with Redis&amp;Node.js)\u3000\uff5e\u7d9a\u304d\uff5e&#8221; \u306e<\/span>\u7d9a\u304d\u3092\u8aad\u3080<\/a><\/p>\n","protected":false},"author":5,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_mi_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0},"categories":[44],"tags":[],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/vitalify.jp\/blog\/wp-json\/wp\/v2\/posts\/3571"}],"collection":[{"href":"https:\/\/vitalify.jp\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/vitalify.jp\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/vitalify.jp\/blog\/wp-json\/wp\/v2\/users\/5"}],"replies":[{"embeddable":true,"href":"https:\/\/vitalify.jp\/blog\/wp-json\/wp\/v2\/comments?post=3571"}],"version-history":[{"count":18,"href":"https:\/\/vitalify.jp\/blog\/wp-json\/wp\/v2\/posts\/3571\/revisions"}],"predecessor-version":[{"id":5620,"href":"https:\/\/vitalify.jp\/blog\/wp-json\/wp\/v2\/posts\/3571\/revisions\/5620"}],"wp:attachment":[{"href":"https:\/\/vitalify.jp\/blog\/wp-json\/wp\/v2\/media?parent=3571"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/vitalify.jp\/blog\/wp-json\/wp\/v2\/categories?post=3571"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/vitalify.jp\/blog\/wp-json\/wp\/v2\/tags?post=3571"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}