Tuesday, June 24, 2014

node.js x WebSocket

node.js x WebSocket

node.jsでWebSocketを使った通信を実装しているアプリを試してみたくて色々と調べ回っている.

stomp-websocketというのが使い勝手が良さそうな感じ. 少し調べてみるとSTOMPというテキストベースのメッセージ交換プロトコルに少し手を加えてWebSocketプロトコルをさばけるようにしたとか.

http://jmesnil.net/stomp-websocket/doc/

作者の方がgithubでstomp.js(node.jsのstomp用コンポーネント)とそのサンプルを公開していたのでこれを使ってみる.

RabbitMq Web-Stomp Plugin

ゆかりさん的にRabbitMqを使わざるを得ないと思った. 後悔はしない.

RabbitMqを公式サイトからダウンロード.

https://www.rabbitmq.com/install-standalone-mac.html

解凍してとりあえず${home-directory}/Applicationsに展開.
Web-Stompプラグインを有効にする.

yukaary$ sbin/rabbitmq-plugins enable rabbitmq_web_stomp
The following plugins have been enabled:
  amqp_client
  rabbitmq_stomp
  cowboy
  sockjs
  rabbitmq_web_stomp
Plugin configuration has changed. Restart RabbitMQ for changes to take effect.

RabbitMQ起動, ステータスを確認.

yukaary$ sbin/rabbitmq-server -detached
Warning: PID file not written; -detached was passed.
yukaary$ sbin/rabbitmqctl status
Status of node rabbit@yukaary ...
[{pid,2080},
 {running_applications,
     [{rabbitmq_web_stomp,"Rabbit WEB-STOMP - WebSockets to Stomp adapter",
          "3.3.3"},
      {rabbitmq_stomp,"Embedded Rabbit Stomp Adapter","3.3.3"},
      {rabbit,"RabbitMQ","3.3.3"},
      {os_mon,"CPO  CXC 138 46","2.2.13"},
      {mnesia,"MNESIA  CXC 138 12","4.10"},
      {amqp_client,"RabbitMQ AMQP Client","3.3.3"},
      {xmerl,"XML parser","1.3.4"},
      {cowboy,"Small, fast, modular HTTP server.","0.5.0-rmq3.3.3-git4b93c2d"},
      {sockjs,"SockJS","0.3.4-rmq3.3.3-git3132eb9"},
      {sasl,"SASL  CXC 138 11","2.3.3"},
      {stdlib,"ERTS  CXC 138 10","1.19.3"},
      {kernel,"ERTS  CXC 138 10","2.16.3"}]},
 {os,{unix,darwin}},
 {erlang_version,
     "Erlang R16B02 (erts-5.10.3) [source] [64-bit] [smp:2:2] [async-threads:30] [hipe] [kernel-poll:true]\n"},
 {memory,
     [{total,38444128},
      {connection_procs,5616},
      {queue_procs,5616},
      {plugins,212184},
      {other_proc,14334736},
      {mnesia,61488},
      {mgmt_db,0},
      {msg_index,29816},
      {other_ets,1168456},
      {binary,22440},
      {code,17366130},
      {atom,654217},
      {other_system,4583429}]},
 {alarms,[]},
 {listeners,[{clustering,25672,"::"},{amqp,5672,"::"},{stomp,61613,"::"}]},
 {vm_memory_high_watermark,0.4},
 {vm_memory_limit,1463938252},
 {disk_free_limit,50000000},
 {disk_free,373913059328},
 {file_descriptors,
     [{total_limit,156},{total_used,4},{sockets_limit,138},{sockets_used,2}]},
 {processes,[{limit,1048576},{used,250}]},
 {run_queue,0},
 {uptime,19}]
...done.

STOMPプラグンは有効になっているかな?ポートはデフォルトだと15674. http://127.0.0.1:15674/stompにブラウザでアクセスしてみるとWelcome to SockJS!と出てくる.

node.js側からここにアクセスできればいいのかな?

この後頑張ってなんとか動作するようにしたいわゆるチャットプログラム(若干変だけど)…後日まとめる. いわゆる「stomp over websocket」にする必要があった.

コマンド覚え書き

RabbitMqに専用のアカウントを追加.

yukaary$sbin/rabbitmqctl add_user yukaary yukaary
yukaary$sbin/rabbitmqctl set_permissions -p / yukaary  ".*" ".*" ".*"

参考サイト

No comments:

Post a Comment