RabbitMQでメッセージング

チャットのメッセージのやりとりをするのに、STOMP
(Streaming_Text_Oriented_Messaging_Protocol)というプロトコルを使いますっていうのをやった事ないのでやってみようかと思います。
簡単にできすぎて怖いです。
ぜひとも上手に使いたいですね。

[RabbitMQ](https://www.rabbitmq.com/tutorials/tutorial-one-javascript.html)を使ってやってみましょう。

# 1.NPMのインストール
```
[root]$ brew install node
==> Downloading https://homebrew.bintray.com/bottles/node-0.12.0.yosemite.bottle.1.tar.gz
######################################################################## 100.0%
==> Pouring node-0.12.0.yosemite.bottle.1.tar.gz
==> Caveats
If you update npm itself, do NOT use the npm update command.
The upstream-recommended way to update npm is:
  npm install -g npm@latest

Bash completion has been installed to:
  /usr/local/etc/bash_completion.d
==> Summary
🍺  /usr/local/Cellar/node/0.12.0: 2196 files, 25M
```

# 2.RabbitMQのインストール
brewで[インストール](https://www.rabbitmq.com/install-homebrew.html)して起動

```javascript:receive.js
[root]$ brew install rabbitmq
==> Downloading https://homebrew.bintray.com/bottles/rabbitmq-3.4.4.yosemite.bottle.tar.gz
######################################################################## 100.0%
==> Pouring rabbitmq-3.4.4.yosemite.bottle.tar.gz
==> Caveats
Management Plugin enabled by default at http://localhost:15672

Bash completion has been installed to:
  /usr/local/etc/bash_completion.d

To have launchd start rabbitmq at login:
    ln -sfv /usr/local/opt/rabbitmq/*.plist ~/Library/LaunchAgents
Then to load rabbitmq now:
    launchctl load ~/Library/LaunchAgents/homebrew.mxcl.rabbitmq.plist
Or, if you don't want/need launchctl, you can just run:
    rabbitmq-server
==> Summary
🍺  /usr/local/Cellar/rabbitmq/3.4.4: 1030 files, 27M

[root]$ /usr/local/sbin/rabbitmq-server&
[1] 40841

 /Users/murotanimari
[root]$
              RabbitMQ 3.4.4. Copyright (C) 2007-2014 GoPivotal, Inc.
  ##  ##      Licensed under the MPL.  See http://www.rabbitmq.com/
  ##  ##
  ##########  Logs: /usr/local/var/log/rabbitmq/rabbit@localhost.log
  ######  ##        /usr/local/var/log/rabbitmq/rabbit@localhost-sasl.log
  ##########
              Starting broker... completed with 10 plugins.
```

# 3.amqplibライブラリのインストール
```
[root]$ npm install amqplib
amqplib@0.4.0 node_modules/amqplib
├── buffer-more-ints@0.0.2
├── bitsyntax@0.0.4
├── when@3.6.4
└── readable-stream@1.1.13 (isarray@0.0.1, inherits@2.0.1, string_decoder@0.10.31, core-util-is@1.0.1)
```

# 4.送信用のスクリプト作成
[send.js](https://github.com/rabbitmq/rabbitmq-tutorials/blob/master/javascript-nodejs/src/send.js)

```javascript:send.js
#!/usr/bin/env node

var amqp = require('amqplib/callback_api');

amqp.connect('amqp://localhost', function(err, conn) {
  conn.createChannel(function(err, ch) {
    var q = 'hello';

    ch.assertQueue(q, {durable: false});
    ch.sendToQueue(q, new Buffer('Hello World!'));
    console.log(" [x] Sent 'Hello World!'");
  });
  setTimeout(function() { conn.close(); process.exit(0) }, 500);
});
```
# 5.受信用のスクリプト作成
[receive.js](https://github.com/rabbitmq/rabbitmq-tutorials/blob/master/javascript-nodejs/src/receive.js)

```javascript:receive.js
#!/usr/bin/env node

var amqp = require('amqplib/callback_api');

amqp.connect('amqp://localhost', function(err, conn) {
  conn.createChannel(function(err, ch) {
    var q = 'hello';

    ch.assertQueue(q, {durable: false});
    console.log(" [*] Waiting for messages in %s. To exit press CTRL+C", q);
    ch.consume(q, function(msg) {
      console.log(" [x] Received %s", msg.content.toString());
    }, {noAck: true});
  });
});
```

#6. 実行
```text:
[murotanimari]$ ./send.js
 [x] Sent 'Hello World!'

 ~
[murotanimari]$ ./send.js
 [x] Sent 'Hello World!'

[murotanimari]$ ./receive.js
 [*] Waiting for messages in hello. To exit press CTRL+C
 [x] Received Hello World!
 [x] Received Hello World!
```