とみふらの明るく楽しい日記

日記や俺用メモなどなんでもあり。

「そろそろnode.jsについて一言書いておくか」のTwitter Streaming APIのデモを試そうとしたらちょくちょくハマったのでメモ

ネタ元記事:そろそろnode.jsについて一言書いておくか

実験環境はUbuntu 11.04

まず"動かすのにhttp-basic-authが必要です。"とのことだが、

npm install http-basic-auth

としても以下のエラーが出てしまう。

tomifla@ubuntu:~$ npm install http-basic-auth
npm ERR! couldn't unpack /tmp/npm-1307697052148/1307697052148-0.6016574911773205
/tmp.tgz to /tmp/npm-1307697052148/1307697052148-0.6016574911773205/contents
npm ERR! Failed to parse json
npm ERR! Unexpected token ILLEGAL
npm ERR! File: /tmp/npm-1307697052148/1307697052148-0.6016574911773205/contents/
package/package.json
npm ERR! JSON.parse Failed to parse package.json data.
npm ERR! JSON.parse package.json must be actual JSON, not just JavaScript.
npm ERR! JSON.parse
npm ERR! JSON.parse This is not a bug in npm.
npm ERR! JSON.parse Tell the package author to fix their package.json file.
npm ERR!
npm ERR! System Linux 2.6.38-8-generic
npm ERR! command "node" "/home/tomifla/.nvm/v0.4.8/bin/npm" "install" "http-basic-
auth"
npm ERR!
npm ERR! Additional logging details can be found in:
npm ERR!     /home/tomifla/npm-debug.log
npm not ok

とりあえずよくわからんが、"/tmp/npm-...(略).../contents"フォルダ内に"http-basic-auth.js"が展開されていたため、それをstreaming.jsと同じフォルダに持ってきた。(もっとうまいやり方があるはず・・・)

そしてstreaming.jsの14行目を
var http = require('./http-basic-auth.js');

var http = require('http-basic-auth');
に変更。
streaming.js

var sys = require('sys');
var ws = require('websocket-server');
var wsServer = ws.createServer();

wsServer.addListener("connection", function(connection) {
    sys.puts("connect: " + connection._req.socket.remoteAddress);
});

wsServer.addListener("close", function(connection) {
    sys.puts("close: " + connection._req.socket.remoteAddress);
});
wsServer.listen(8000);

var http = require('./http-basic-auth.js');
var account = {
    username: 'username',
    password: 'password'
};
var client = http.createClient(80, 'stream.twitter.com', false, false, account);
var request = client.request('GET', '/1/statuses/sample.json', {'host': 'stream.twitter.com'});

request.end();
request.on('response', function (response) {
    response.on('data', function(chunk) {
        wsServer.broadcast(chunk);
    });
});

そして起動

node streaming.js

するとbase64のモジュールが見つからないという以下のようなエラーが

node.js:134
        throw e; // process.nextTick error, or 'error' event on first tick
        ^
Error: Cannot find module 'base64'   
    at Function._resolveFilename (module.js:322:11)
    at Function._load (module.js:267:25)
    at require (module.js:351:19)
    at Object.<anonymous> (/home/tomifla/github/streamingjs/http-basic-auth.js:1:76)
    at Module._compile (module.js:407:26)
    at Object..js (module.js:413:10) 
    at Module.load (module.js:339:31)
    at Function._load (module.js:298:12)
    at require (module.js:351:19)
    at Object.<anonymous> (/home/tomifla/github/streamingjs/streaming.js:14:12)

というわけでbase64のモジュールをインストール

npm install base64

そしたらもう一度起動

node streaming.js

とりあえずこれで正常に起動できるようになったようだ。

クライアントは元記事のやつそのまんまでおk(元記事が消えたときのために一応まるまる引用しておく)
client.html

<html> 
<head> 
<title>demo</title>
</head>
<body>
<div id="main"></div>
<script>
var main = document.getElementById('main');
var connection = new WebSocket("ws://localhost:8000");

connection.onopen = function(event) {
    main.innerHTML = '';
}

connection.onmessage = function(event) {
    try {
        var json =JSON.parse(event.data);
        var name = json['user']['name'];
        var text = json['text'];
        main.innerHTML = name + ': ' + text + '<br>' + main.innerHTML;
    } catch (e) {}
}
</script> 
</body> 
</html>

そしてウェブブラウザでclient.htmlを開く。("http://localhost:8000"を開くのではなく、client.htmlというファイルを開く。注意)
Firefox4.0.1で開いてみたのだが、何も流れてこない。
"WebSocket対応ブラウザじゃないと動きませんので注意して下さい。"
とのことなので、Firefox4.0.1はWebSocketに対応していないということだろうか。
Chromium11.0.696.71 (86024)でclient.htmlを開いてみたところ、うまくいった。ヾ(@⌒ー⌒@)ノ