LoginSignup
11
11

More than 5 years have passed since last update.

使用したモジュール覚書

Last updated at Posted at 2015-05-14

使用したモジュール覚書

pm2

https://github.com/Unitech/pm2

P(rocess) M(anager) 2
PM2 is a production process manager for Node.js / io.js applications with a built-in load balancer. It allows you to keep applications alive forever, to reload them without downtime and to facilitate common system admin tasks.

pm2はnode.jsアプリプロセスの管理パッケージです、ロードバランサー機能もビルドインされています。

  • 機能

    • ロードバランサー機能をビルドイン=クラスタリング(native clusterモジュール利用)
    • デーモンスクリプト
    • node.jsアプリを0sリロード Ubuntu/CentOS起動スクリプトも提供(Ubuntuはupdaterc.d、ほかのLinuxはchkconfig)
    • 不安定プロセスを停止させる(無限ループ回避)
    • コンソール監視
    • HTTP API
    • リモートコントロールとリアルタイムAPIインターフェース pm2 - node.js プロセス管理パッケージ
  • io.jsとは何か

  • Node.jsでのクラスタリング

    node.jsの問題点として、シングルプロセスで処理をしていて、あるリクエストの処理が長くかかる場合、ほかのすべてのリクエストをブロックする可能性があります。 httpモジュールで、ウェブサーバを起動したとき、同時に処理できるアクセスは、1つだけなので、処理がつまると、後ろに並んでいるリクエストの待ち行列は、ブロックされた状態になります。それらを解決するためにclusterを用いた例がある。
    伝統的なUnixプログラミングのforkモデルと同じと捉えて差し支えない。Unixのfork()と異なるのは、Unixのforkは、forkを呼び出した直後のプログラムが親プロセスと子プロセスで実行されますが、node.jsのclusterの場合は、forkを実行すると、同じプログラムが最初から起動されます。
    node.js clusterでHTTPサーバをマルチプロセス化する

express

webアプリケーションフレームワーク

  • Web Applications. Express is a minimal and flexible Node.js web application framework that provides a robust set of features for web and mobile applications.
    • 小さい柔軟なwebアプリケーションフレームワーク。webアプリまたはモバイルアプリに様々な堅牢な機能を提供します。
  • APIs. With a myriad of HTTP utility methods and middleware at your disposal, creating a robust API is quick and easy.

Webアプリを構成する上で必要な機能がそろっており、ルーティングやビューヘルパー、セッション管理の機能もデフォルトで提供しています(※永続的なセッション管理には「Redis」などのデータストアが必要な場合もある)。また、ミドルウエアとしての機能を使用し、Expressの機能を独自に拡張することもできます。

express middleware

  • An Express application is essentially a series of middleware calls.Middleware is a function with access to the request object (req), the response object (res), and the next middleware in line in the request-response cycle of an Express application, commonly denoted by a variable named next. Middleware can:

    • Execute any code.
    • Make changes to the request and the response objects.
    • End the request-response cycle.
    • Call the next middleware in the stack.
  • リクエストがやってきたときにこれらを順次適用していきなさいというもの。順次定義した順番に実行されるので注意。

  • 本家

body-parser

  • expressは通常GET値しか解析できない。POST値やJSONを解析するために使用。
    • https://www.npmjs.com/package/body-parser
    • express3までは付express本体に付属していたが、express4からなくなってthird partyになった。Express入門をexpress 4.xで学ぶ場合のメモ
      • express/connectミドルウェアbodyParserを読む
      • bodyParser.urlencoded(options)
        • Returns middleware that only parses urlencoded bodies. This parser accepts only UTF-8 encoding of the body and supports automatic inflation of gzip and deflate encodings. A new body object containing the parsed data is populated on the request object after the middleware (i.e. req.body). This object will contain key-value pairs, where the value can be a string or array (when extended is false), or any type (when extended is true).

express-session (node-module)

https://github.com/expressjs/session

connect-redis (node module)

https://github.com/tj/connect-redis

scoket.io

http://socket.io/

var app = require('express')();
var http = require('http').Server(app);
var io = require('socket.io')(http);

app.get('/', function(req, res){
  res.sendfile('index.html');
});

io.on('connection', function(socket){
  console.log('a user connected');
});

http.listen(3000, function(){
  console.log('listening on *:3000');
});

socket.io-redis

  • Adapter to enable broadcasting of events to multiple separate socket.io server nodes.
  • By running socket.io with the socket.io-redis adapter you can run multiple socket.io instances in different processes or servers that can all broadcast and emit events to and from each other.
    • socket.io-redisを用いてsocket.ioを利用することによって、異なるプロセス間、サーバ間でもブロードキャスト、イベントを共有することができる。
  • If you need to emit events to socket.io instances from a non-socket.io process, you should use socket.io-emitter.

pm2 + socket.IO

pm2でクラスタリングしたsocketサーバーに接続すると、xml-pollingになったり、websocketになったりと接続が不安定。原因はSocket.IO は接続確立までに 2 回リクエストを投げるということです。 そしてこの最初の要求と、 WS の接続要求は、「同じインスタンス」にアクセスする必要があります。 二つの接続がバランスされて別々のサーバに行くと、ハンドシェイクがエラーになり、リトライが発生します。 たまたま二回同じサーバにいけば、接続が確立できるので、バランスするサーバが増えるほど成功確率が減 り、確立までの時間が長くなります。

pm2 + socket.io + socket.io-redis

  • scoket.ioのデフォルトはオンメモリなのでプロセス間での共有ができない。pm2でクラスタリングしていると複数プロセスを動かすことになるので、共有できない問題をsocket.io-redisで解決する 。Redisのpub/subを利用して解決しているらしい。

参考になるレポート
* socket.io + redis(pub/sub)(nodeのモジュールを使わない) + cluster + を使用した例
* Socket.IO, Redisを使用し各ゲーム間でプッシュ通知するシステム

11
11
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
11
11